上面两篇讲解了一对一和一对多查询的输出映射,这篇讲解一下多对多查询的输出映射。
实例需求讲解
查询用户及用户购买商品信息。由于用户和商品没有直接关联,通过订单和订单明细进行关联。一个用户对应多个订单,一个订单对用多个订单明细,一个订单明细对应一个商品信息。即用户与商品信息之间是间接的多对多关系。
创建pojo类( User类,Orders类,OrserDetail类)
public class Userimplements Serializable {
//属性名和数据库表的字段对应
privateint id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
//用户创建的订单列表
privateList<Orders> ordersList;
}
public class Orders{
private Integer id;
private Integer userId;
private String number;
private Date createtime;
private String note;
//订单明细
private List<Orderdetail>orderdetails;
}
public classOrderdetail {
private Integer id;
private Integer ordersId;
private Integer itemsId;
private Integer itemsNum;
//明细对应的商品信息
private Items items;
}
说明:在user类中添加订单列表属性List<Orders>orderslist,将用户创建的订单映射到orderslist。在Orders中添加订单明细列表属性List<OrderDetail>orderdetials,将订单的明细映射到orderdetials。在OrderDetail中添加Items属性,将订单明细所对应的商品映射到Items
创建mapper.xml
<mappernamespace="cn.itcast.mybatis.mapper.OrdersMapperCustom">
<!-- 查询用户及购买的商品 -->
<resultMaptype="cn.itcast.mybatis.po.User"id="UserAndItemsResultMap">
<!-- 用户信息-->
<idcolumn="user_id" property="id"/>
<resultcolumn="username" property="username"/>
<resultcolumn="sex" property="sex"/>
<resultcolumn="address" property="address"/>
<!-- 订单信息
一个用户对应多个订单,使用collection映射
-->
<collection property="ordersList"ofType="cn.itcast.mybatis.po.Orders">
<idcolumn="id" property="id"/>
<resultcolumn="user_id" property="userId"/>
<resultcolumn="number" property="number"/>
<resultcolumn="createtime" property="createtime"/>
<resultcolumn="note" property="note"/>
<!-- 订单明细
一个订单包括 多个明细
-->
<collectionproperty="orderdetails"ofType="cn.itcast.mybatis.po.Orderdetail">
<idcolumn="orderdetail_id" property="id"/>
<resultcolumn="items_id" property="itemsId"/>
<resultcolumn="items_num" property="itemsNum"/>
<resultcolumn="orders_id" property="ordersId"/>
<!--商品信息
一个订单明细对应一个商品
-->
<associationproperty="items" javaType="cn.itcast.mybatis.po.Items">
<idcolumn="items_id" property="id"/>
<resultcolumn="items_name" property="name"/>
<resultcolumn="items_detail" property="detail"/>
<resultcolumn="items_price" property="price"/>
</association>
</collection>
</collection>
</resultMap>
<!-- 查询用户及购买的商品信息,使用resultmap -->
<selectid="findUserAndItemsResultMap"resultMap="UserAndItemsResultMap">
SELECT
orders.*,
USER.username,
USER.sex,
USER.address,
orderdetail.id orderdetail_id,
orderdetail.items_id,
orderdetail.items_num,
orderdetail.orders_id,
items.name items_name,
items.detail items_detail,
items.price items_price
FROM
orders,
USER,
orderdetail,
items
WHEREorders.user_id = user.id AND orderdetail.orders_id=orders.id ANDorderdetail.items_id = items.id
</select>
</mapper>
说明:其实有了前面两次的映射,这次多对多的映射也很简单。只要根据pojo类所映射的属性,就可以创建出resultMap的映射。
创建mapper.java类
public interfaceOrdersMapperCustom {
//查询用户购买商品信息
publicList<User> findUserAndItemsResultMap()throws Exception;
}
总结
resultType
作用:将查询结果按照sql列名pojo属性名一致性映射到pojo中。
场合:常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用resultType将每一条记录映射到pojo中,在前端页面遍历list(list中是pojo)即可。
resultMap
使用association和collection完成一对一和一对多高级映射(对结果有特殊的映射要求)。
(1)association:
作用:将关联查询信息映射到一个pojo对象中。
场合:为了方便查询关联信息可以使用association将关联订单信息映射为用户对象的pojo属性中,比如:查询订单及关联用户信息。
(2)collection:
作用:将关联查询信息映射到一个list集合中。
场合:为了方便查询遍历关联信息可以使用collection将关联信息映射到list集合中,比如:查询用户权限范围模块及模块下的菜单,可使用collection将模块映射到模块list中,将菜单列表映射到模块对象的菜单list属性中,这样的作的目的也是方便对查询结果集进行遍历查询。