MyBatic中一对多查询的使用
一.抛出问题,在数据库设计中,会出现一个订单关联多个订单详情,一个用户关联多个收货地址等。要求在查询订单时带出订单详细信息
二.解决方式,使用一对多查询(单查询,嵌套查询)
单查询
1.在订单表实体类中,添加一个订单详情List属性
public class Order { private int id; private int userId; private String userAccount; /* * */ //将订单详情作为List,保存在Order属性中 List<OrderDetils> orderDetilsList; }
2.使用单个Select查询
因为在数据表中不存在List<OrderDetils>列,所以用resultType无法进行映射,此时用resultMap,在映射赋值时由于是一个集合,所以用collection对List中每一个元素赋值
<select id="getById" resultMap="OrderResultMap"> SELECT o.*,u.account,od.id,g.id goodsid,g.picture,g.name,od.goodsprice,od.goodsnum FROM mall_order o,mall_orderdetils od,mall_goods g,mall_user u WHERE o.id=od.orderid AND o.userid=u.id AND od.goodsid=g.id AND o.id=#{id}; </select> <resultMap id="OrderResultMap" type="Order"> <id column="id" property="id"/> <result column="userid" property="userId"/> <result column="account" property="userAccount"/> <result column="orderno" property="orderNo"/> <result column="orderdate" property="orderDate"/> <result column="status" property="status"/> <result column="consignee" property="consignee"/> <result column="phonenum" property="phoneNum"/> <result column="recaddress" property="recAddress"/> <result column="paytype" property="payType"/> <result column="paydate" property="payDate"/> <result column="recdate" property="recDate"/> <result column="cost" property="cost"/> <!-collection,是复杂类型的集合标签,property代表映射的属性(一般是一个集合List),ofType代表映射的集合存储的类型> <collection property="orderDetilsList" ofType="OrderDetils"> <id column="odid" property="id"/> <result column="goodsid" property="goods.id"/> <result column="picture" property="goods.picture"/> <result column="name" property="goods.name"/> <result column="goodsprice" property="goodsPrice"/> <result column="goodsnum" property="goodsNum"/> </collection> </resultMap>
嵌套查询
1.同单查询
2.先有嵌套中的子查询,即要现有查询订单详情的查询语句
<select id="getByOrderId" resultMap="OrderDetilsMap"> select o.id,g.picture,g.name,o.goodsprice,o.goodsnum from mall_orderdetils o,mall_goods g where o.goodsid=g.id and o.orderid=#{id} </select> <!-- 一对多单查询方式--> <resultMap id="OrderDetilsMap" type="OrderDetils"> <id column="id" property="id"/> <result column="picture" property="goods.picture"/> <result column="name" property="goods.name"/> <result column="goodsprice" property="goodsPrice"/> <result column="goodsnum" property="goodsNum"/> </resultMap>
2.查询订单,其中collection标签中,fetchType字段设置影响到是否默认带出订单详细信息
<select id="getById" resultMap="OrderResultMap"> select * from mall_order where id=#{id} </select> <resultMap id="OrderResultMap" type="Order"> <id column="id" property="id"/> <result column="userid" property="userId"/> <result column="orderno" property="orderNo"/> <result column="orderdate" property="orderDate"/> <result column="status" property="status"/> <result column="consignee" property="consignee"/> <result column="phonenum" property="phoneNum"/> <result column="recaddress" property="recAddress"/> <result column="paytype" property="payType"/> <result column="paydate" property="payDate"/> <result column="recdate" property="recDate"/> <result column="cost" property="cost"/> <!--property,映射到结果的字段和属性,此处映射到订单实体类中的订单详情List--> <!--column,数据库中的列名,或者是列的别名,传递给嵌套 Select 查询语句的列名--> <!--fetchType,有效值为lazy 和 eager,置为lazy时,代表需要property中的值才调用嵌套select--> <!--select,指定嵌套子查询,namespace.id--> <collection property="orderDetilsList" column="id" fetchType="lazy" select="com.woniu.mall.dao.OrderDetilsDAO.getByOrderId"/> </resultMap>