1.一对一查询
1.1需求
查询订单信息,关联查询创建订单的用户信息
1.2resultType
1.2.1sql语句
确定查询的主表:订单表
确定查询的关联表:用户表
关联查询使用内连接?还是外连接?
由于order表中有一个外键(user_id),通过外键关联查询用户表只能查询出一条记录,可以使用内连接
select order.*
user.username,
user/sex,
user.address
from order ,user where order.user_id=user.i
d
1.2.2创建pojo
将上边sql查询的结果集映射到pojo中,pojo中必须包括所有的查询列名
原始的order.java不能完全映射全部字段,需要新创建的pojo
创建一个pojo继承包括查询字段较多的pojo类
public orderCustom extends order{
priavte String username;
private String sex;;
private String addresss;
}
1.2.3mapper.xml(与OrderMapper.java在同一目录下 名字必须相同)
OrderMapper.xml
<!--查询订单,关联查询用户-->
<select id="" resultTRype="">
select order.*
user.username,
user/sex,
user.address
from order ,user where order.user_id=user.id
</select>
1.2.4OrderMapper.java
public List<OrderCustom> findOrderUser() throws Exception;
1.2.5
//此方法在testFINDByID之前先执行
private SqlSessionFactory sqlSessionFactory=null;
@Before
public void setUp() throws Exception{
//创建SqlSessionFactory
//mybatis配置文件
String resource="SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂传入mybatis的配置文件信息
sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
}
public void testFindUserByResultMap() throws Exception{
SqlSession sqlSession=sqlSessionFactory.openSession();
//创建UserMapper对象,mybatis自动生成mapper代理对象
OrderMapperCustom oderMapperCustom=sqlSession.getMapper(OrderMapperCustom.class))
//调用mapper的方法
List<OrderCustom> list=orderMapperCusto.finOrderUser()
;
System.out.print(luser);
sqlSession.close();
1.3resultMap
1.3.1sql语句
同resultType实现的sql
1.3.2使用resultMap映射的思路
使用resultMap将查询结果中的订单信息映射到Orders对象中,在order类中添加User属性,将关联查询出来的
用户信息映射到orders对象中的user属性中
1.3.3 需要Orders中添属性信息
public class Orders{
private Integer id;
private Integer userId;
private Srtring number;
peiavte Date createtime;
private String note;
//用户信息
private User user;
}
1.3.4mapper.xml
定义resultMap
<!--订单查询关联用户的resultMap
将整个查询结果映射到mybatis.po.order中
-->
<resultMap type="mybatis.po.Orders" id="orderUserResultMap">
<!--配置映射信息的订单信息-->
<!--id:指定查询列中的唯一标识,订单信息的中的唯一标识,如果有多个列组成唯一标设计哦配置多个id-->
<!--
column:订单信息的唯一标识列
property:订单信息的唯一标识,列反映射到Orders中那个属性
-->
<id column="id‘ property="id"/>
<result column="user_id" property="userId"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<!--配置映射的关联用户信息
association:用于映射关联查询吧单个对象的信息
property:要将关联查询的用户信息映射到Orders中那个属性
-->
<association property="user" javaType="mybatis.po.User">
<!--id:关联查询用户信息的唯一标识,
column:指定唯一标识的用户信息的列
JavaTYpe: 映射到use的那个属性
-->
<id column="user_id" property=’id“/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
</association>
</resultMap>
<select id="findOrderUserResultMap" resultMap="OrderUserResultMap">
select order.*
user.username,
user/sex,
user.address
from order ,user where order.user_id=user.id
</select>
1.3.5mapper接口
public List<Orders> findOrderUserResultMap() throws Exception;
1.3.6测试
public void testFindUserByResultMap() throws Exception{
SqlSession sqlSession=sqlSessionFactory.openSession();
//创建UserMapper对象,mybatis自动生成mapper代理对象
OrderMapperCustom oderMapperCustom=sqlSession.getMapper(OrderMapperCustom.class))
//调用mapper的方法
List<Orders> list=orderMapperCusto.finOrderUser()
;
System.out.print(luser);
sqlSession.close();
1.4resultType 和 resultMap实现一对一查询小结
实现一对一查询:
resultType:使用resultType实现较为简单,如果pojo中没有包括查询出来的徐亚增加列名对应的属性,既可完成映射
如果没有查询结果的特殊要求建议使用resultType
resultMap需要单独定义resultMap,实现有点儿麻烦,如果对查询结果有特殊的要求,使用resultMap可以完成
将关联查询映射pojo的属性中
resultMap可以实现延迟加载,resultType无法实现延迟加载
2一对多查询
2.1需求
查询订单及订单明细的信息
2.2 sql语句
确定主查询表:订单表
确定关联查询表:订单明细表
在一对一查询基础之上添加订单明细表关联即可
select
orders.*,
User.username,
user.sex,
user.address,
orderdetail.id orderfetail_id,
orderdetail.items_id,
orderdetail/items_num,
orderdetail.orders_id
from
orders,users,orderdetail
where order.user_id=user.id and orderdetailo.orders_id=orders.id
2.3分析
使用resultType将上边的查询结果映射到pojo中,订单信息的就是重复
要求:
对orders映射不能出现重复的记录
在orders.java类中添加List<orderDetail> orderDeatils属性
最终会将订单信息全部映射到orders中,订单所对应的订单明细映射到orders中的orderDetail集合中
映射成的orders记录数为俩条(orders信息不重复)
每个orders中的orderDetail属性存储了该订单所对应的订单明细。
2.4在orders中添加list订单明细
public class Orders{
private Integer id;
private Integer userId;
private String number;
private Date createtime;
private String note;
//用户信息
private User user;
//订单明细
private List<Orderdetail> orderdetail;
}
2.5mapper.xml
<select id="findOrderAndOrderDetailResultMap" resultMap=’‘OrderAndOrderdetailResultMap“>
select
orders.*,
User.username,
user.sex,
user.address,
orderdetail.id orderfetail_id,
orderdetail.items_id,
orderdetail/items_num,
orderdetail.orders_id
from
orders,users,orderdetail
where order.user_id=user.id and orderdetailo.orders_id=orders.id
</select>
2.6resultMap定义
<!--订单及订单明细的resultMap
使用extends,不用在配置订单信息和用户信息的映射
-->
<resultMap type="mybatis.po.Order" id=‘OrderAndOrderdetailResultMap“ extends="orderUserResultMap">
<!--
订单信息
用户信息
使用extends继承,不用在中国配置订单信息和用户信息的映射--》
<!--订单明细信息
一个订单关联查询出了多条明细,要使用collection进行映射
collection:对关联查询到多条记录映射到集合对象中
property:将关联查询到的多条记录映射到mybatis.po.Order那个属性
ofType:指定映射到list集合属性中pojo的类型
-->
<collection property="orderdetail" ofType="mybatis.po.Orderdetail">
<!--idL:订单明细唯一标识
property:要将订单明细的唯一标识,映射到mybatis.po.Orderdetail的那个属性
-->
<id colum="orderdetail_id" property="id"/>
<result colum-="items_id" property="itemsId"/>
2.7mapper.java
//查询订单(关联用户)及订单明细
public List<Orders> findOrderAndOrderDetailResultMap() throws xception;
2.8测试
public void tesfindOrderAndOrderDetailResultMap() throws Exception{
SqlSession sqlSession=sqlSessionFactory.openSession();
//创建UserMapper对象,mybatis自动生成mapper代理对象
OrderMapperCustom oderMapperCustom=sqlSession.getMapper(OrderMapperCustom.class))
//调用mapper的方法
List<Orders> list=orderMapperCusto.findOrderAndOrderDetailResultMap() ;
;
System.out.print(luser);
sqlSession.close();
2.9小结
mybatis使用collection对关联查询的多条记录映射到一个list集合属性中
使用resultType实现
将订单明细映射到orders中orderdetail中,需要自己处理,使用双重循环遍历,去掉重复记录。
将订单明细放在orderdetail中
3.多对多查询:
3.1需求
查询用户及用户所购买的商品信息
3.2sql语句
查询主表是:用户表
关联表:由于用户表和商品没有直接的关联,通过订单和订单明细进行关联,所以关联表
orders orderdetail items
select
orders.*,
User.username,
user.sex,
user.address,
orderdetail.id orderfetail_id,
orderdetail.items_id,
orderdetail/items_num,
orderdetail.orders_id,
items.name itesms_name,
items.detail items_detail,
items.price items_price
from
orders,users,orderdetail ,items
where order.user_id=user.id and orderdetailo.orders_id=orders.id and orderdetail.items_id=items.id
3.3映射思路
将用户信息映射到user中
在user类中添加订单列表的属性List<Orders>orderlist 将用户所创建的订单映射到orderlist
在order中添加订单明细表属性List<OrderDetail>orderdetail 将订单的明细映射到orderdetail
在orderdetail中添加items属性 晶订单明细所对应的商品映射到items
3.4mapper.xml
<!--查询用户及购买的商品信息,使用resultMap-->
<select id="findUserAndItemsResultMap" resultMap=‘UserAndItemsResultMap“>
select
orders.*,
User.username,
user.sex,
user.address,
orderdetail.id orderfetail_id,
orderdetail.items_id,
orderdetail/items_num,
orderdetail.orders_id,
items.name itesms_name,
items.detail items_detail,
items.price items_price
from
orders,users,orderdetail ,items
where order.user_id=user.id and orderdetailo.orders_id=orders.id and orderdetail.items_id=items.id
</select>
3.5resultMap
<!--查询用户集和商品信息-->
<resultMap type="mybatis.po.User" id="UserAndItemsResultMap">
<!--用户信息-->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="adress"/>
<!--订单信息
一个用户对应多个订单 使用collection映射
<collection property="orderslist" ofType="mybatis.po.Orders">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="numbr"/>
<result column="note" property="note"/>
<!--订单明细
一个订单包括多个明细
-->
<collection property="orderfetail" ofType="mybatis.po.Orderdetail">
<id column="orderdetail_id" property="id"/>
<result column="items_id" property="itemId"/>
<result column="items_num" property=-"itemnum"/>
<result column="orders_id" property=-"ordersId"/>
<!--商品信息
一个订单明细对应一个商品
-->
<association proprty=’items“ JavaType=”mybatis.po.items“>
<id column="items_id" property="id"/>
<result column="items_name" property="name"/>
Mresult column="items_detail" property="detail"/>
<result column="items_price" property="price"/>
<association>
</collection>
</collection>
</resultMap>
3.6mapper接口
public List<User> findUserAndItemsResultMap() throws Exception;
3.7测试
public void tes findUserAndItemsResultMap() throws Exception{
SqlSession sqlSession=sqlSessionFactory.openSession();
//创建UserMapper对象,mybatis自动生成mapper代理对象
OrderMapperCustom oderMapperCustom=sqlSession.getMapper(OrderMapperCustom.class))
//调用mapper的方法
List<User> user=orderMapperCusto findUserAndItemsResultMap() ;
;
System.out.print(luser);
sqlSession.close();
2.8多对多查询总结
将查询用户购买的商品信息的明细清单(用户名,用户地址,购买商品的名称,购买商品的时间购买商品的数量)
针对上边的需求就使用resultType将查询到的记录映射到一个扩展的pojo中。,很简单实现明细清单的功能
一对多是多对多的特例:如下需求
查询用户购买的商品信息,用户和商品信息的关系是多对多关系
需求1:
查询字段:用户账号,用户名称,用户性别,商品名称。商品价格
企业开发中常见明细列表,用户购买商品明细列表
使用resultType将上边查询映射到pojo输出
需求2
查询字段,用户账号,用户名称,购买商品的数量。商品明细,(鼠标移上显示明细)
使用resultMap将用户购买的商品明细列表映射到user对象中
总结:
使用resulMap是针对那些查询结果映射有特殊的要求的功能:比如特殊要求映射成list中包括多个list
3.resultType
作用:将查询结果按照 sql 列名和 pojo 属性名一致性映射到 pojo 中。
场合:常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,
此时可直接使用resultType将每一条记录映射到扩展的pojo中,在前端遍历list即可。
、resultMap:使用 association 和 collection 完成一对一和一对多的高级映射。
1、association:
作用:将关联查询到的信息映射到一个 pojo 对象中
场合:为了方便查询关联信息可以使用 association 将关联的订单信息映射为用户
对象的pojo属性中,比如:查询订单及关联用户信息
若是使用 resultType ,则无法将查询结果映射到 pojo 对象的 pojo 属性中,根据
查询结果查询遍历的需要选择使用 resultType 还是 resultMap
2、collection:
作用:将关联查询信息映射到一个 list 集合中
场合:为了方便查询遍历关联信息可以使用 collection 将关联信息映射到 list 集合中,
比如:查询用户权限范围模块及模块下的二级菜单,可使用 collection 将模块映射到
模块 list 中,将菜单列表映射到模块对象的菜单 list 属性中,这样做的目的也是方便
查询结果集进行遍历查询。
如果使用 resultType 则无法将查询结果映射到 list 集合中。