标签:
xml映射文件:一个用户对应多个订单,一个订单对应多个订单明细,一个订单明细对应一个商品 可以推断出用户和商品是多对多的关系用户和商品的关系分析
可以发现一层套一层...
<!-- #######################################多对多的关系映射####################################################### --> <resultMap type="cn.itcast.domain.User" id="UserAndItemsResultMap"> <!-- 用户表信息 --> <id column="id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <result column="address" property="address"/> <!-- 订单表信息 一个用户多个订单--> <collection property="ordersList" ofType="cn.itcast.domain.Orders"> <id column="id" property="id"/> <result column="number" property="number"/> <result column="createtime" property="createtime"/> <result column="note" property="note"/> <result column="user_id" property="userId"/> <!-- 一个 订单多个明细 --> <collection property="orderdetails" ofType="cn.itcast.domain.Orderdetail" > <id column="orderdetail_id" property="id"/> <result column="items_num" property="itemsNum"/> <result column="items_id" property="itemsId"/> <result column="orders_id" property="ordersId"/> <!-- 一个订单一个商品 --> <association property="items" javaType="cn.itcast.domain.Items"> <id column="items_id" property="id"/> <result column="items_name" property="name"/> <result column="items_price" property="price"/> <result column="items_detail" property="detail"/> </association> </collection> </collection> </resultMap> <select id="findUserAndItemsResultMap" resultMap="UserAndItemsResultMap"> select orders.*, user.username,user.sex,user.address, orderdetail.items_id,orderdetail.items_num,orderdetail.orders_id,orderdetail.id orderdetail_id, items.name items_name,items.price items_price,items.detail items_detail from orders,user,orderdetail,items where orders.user_id=user.id and orders.id=orderdetail.orders_id and orderdetail.items_id=items.id </select>接口中定义方法
public List<User> findUserAndItemsResultMap();
测试:
@Test public void testfindUserAndItemsResultMap(){ SqlSession sqlSession = sqlSessionFactory.openSession(); OrdersMapperUser ordersMapperUser = sqlSession.getMapper(OrdersMapperUser.class); List<User> list = ordersMapperUser.findUserAndItemsResultMap(); System.out.println(list); }运行结果分析:
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u014010769/article/details/47303171