目录
前言
一、Mybatis多表查询
2.一对一查询(多对一)
2.2 一对多查询
2.3 多对多查询
2.4 知识小结
前言
mybatis初学者记录每日学习日常
提示:以下是本篇文章正文内容,下面案例可供参考
一、Mybatis多表查询
内连接、左外连接、右外连接、交叉连接区别
有两个表A和表B,表A结构如下:
表B结构如下:
为了把Bid和Aid加以区分,不让大家有误解,所以把Bid的起始值设置为100
有sql基本知识的人都知道,两表要做连接,就必须有个连接字段,从上表中的数据可以看出,在A表中的Aid和B表中Bnameid就是两个连接字段。下图说明了连接的所有记录集之间的关系
现在我们对内连接和外连接一一讲解
1.内连接:
利用内连接可获取两表的公共部分的记录,即上图中记录集C
语句如下:
select * from A join B on A.Aid = B.Bnameid
或select * from A.B where A.Aid = B.Bnameid
运行结果:
2.外连接:
外连接分为两种,左连接和右连接
左连接,语句如下
select * from A left join B on A.Aid = B.Bnameid
运行结果如下:
右连接,语句如下
select * from A right join B on A.Aid = B.Bnameid
运行结果如下:
2.一对一查询(多对一)
2.1 一对一查询的模型
用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户
2.1.1 一对一查询语句
select * from orders o,user u where o.uid = u.id
结果如下
需求:查询所有订单关系,关系查询下单用户信息
注:
因为一个订单信息只能提供某个用户使用,所以从订单查询出发关联查询用户信息为一对一查询,如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以用有多个订单
2.1.2 配置OrderDap.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.dao.OrderDao"><!--查询所有--><select id="findAll" resultType="order">select * from orders</select></mapper>
测试结果
不难发现,结果集中user对象结果为null,与我们的需求差异很大
分析:通过订单查询用户,所以以订单为主,不能关联用户信息,因为订单里没有用户属性
因此我们需要使用resultMap,定义专门的resultMap用于映射一对一查询结果
修改OrderDao.xml:
<resultMap id="OrdersMap" type="Orders"><id column="id" property="id"></id><result column="ordertime" property="ordertime"></result><result column="total" property="total"></result><association property="user"><id column="id" property="id"></id><result column="username" property="username"></result><result column="password" property="password"></result><result column="birthday" property="birthday"></result></association></resultMap><select id="findAll" resultMap="OrdersMap">select * from orders o,user u where o.id = u.id</select>
查询结果:
2.2 一对多查询
2.2.1 一对多查询的模型
用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户一对多查询的要求:查询一个用户,于此同时查询出该用户具有的订单
2.2.2 一对多的查询语句
select * f,o.id oid from user u left join orders o on u.id=o.uid;
查询结果
需求:
查询所有用户信息及用户关联的订单信息
分析:
用户信息和他的订单信息为一对多的关系,并且查询的过程中如果没有订单信息,此时也要将用户信息查询出来
配置UserDao.xml
<resultMap id="userMap" type="user"><id property="id" column="id"></id><result property="username" column="username"></result><result property="password" column="psaaword"></result><result property="birthday" column="birthday"></result><collection property="ordersList" ofType="orders"><result property="id" column="id"></result><result property="ordertime" column="ordertime"></result><result property="total" column="total"></result></collection></resultMap><select id="findAll" resultMap="userMap">select * from user u,orders o where u.id = o.uid</select>
运行结果
2.3 多对多查询
2.3.1 多对多查询的语句:
对应的sql语句
SELECT * FROM `user` u ,sys_role r,sys_user_role ur WHERE u.id = ur.userid AND r.id = ur.roleid
配置UserMapper.xml
<resultMap id="UserAndRoleMap" type="User"><id column="id" property="id"></id><result column="username" property="username"></result><result column="password" property="password"></result><result column="birthday" property="birthday"></result><collection property="roleList" ofType="Role"><id property="id" column="id"></id><result property="rolename" column="rolename"></result><result property="roleDesc" column="roleDesc"></result></collection></resultMap><select id="findUserAndRole" resultMap="UserAndRoleMap">select * from `user` u ,sys_role r,sys_user_role ur where u.id = ur.userid and r.id = ur.roleid</select>
测试结果
2.4 知识小结
mybatis多表配置方式:
一对一配置:使用 做配置
一对多配置:使用+做配置
多对多配置:使用+做配置