在OrdersExt类中添加以下属性,并提供get/set方法:
// 订单明细 private List<Orderdetail> detailList; |
//查询订单信息,包括用户名称、用户地址,订单商品信息(嵌套结果) public List<OrdersExt> findOrdersResultMapOfCollection(); |
<!-- 定义orderUserItemResultMap(嵌套结果) --> <!-- extends:继承已有的ResultMap,输入值为继承的ResultMap的唯一标示 --> <resultMap type="ordersExt" id="orderUserItemResultMap" extends="ordersUserResultMap"> <!-- 映射关联关系(一对多) --> <!-- collection标签:定义一个一对多关系 ofType:指定该集合参数所映射的类型 --> <collection property="detailList" ofType="OrderdetailExt"> <id column="detail_id" property="id" /> <result column="items_id" property="itemsId" /> <result column="items_num" property="itemsNum" /> </collection> </resultMap> <!-- 查询订单信息,包括用户名称、用户地址,订单商品信息(嵌套结果) --> <select id="findOrdersResultMapOfCollection" resultMap="orderUserItemResultMap"> Select <include refid="select_orders" />, <include refid="select_user" />, orderdetail.id detail_id, orderdetail.items_id, orderdetail.items_num from orders,user,orderdetail where orders.user_id = user.id and orders.id = orderdetail.orders_id </select> |
/** * 进行订单信息查询,包括用户的名称和地址信息(resultMap之嵌套结果) */ @Test public void testFindOrdersResultMapOfCollection() { SqlSession sqlSession = sqlSessionFactory.openSession(); OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class); // 测试 List<OrdersExt> list = ordersMapper.findOrdersResultMapOfCollection(); System.out.println(list); sqlSession.close(); } |
// 查询订单信息,包括用户名称、用户地址,订单商品信息(嵌套查询) public List<OrdersExt> findOrdersResultMapOfCollectionSelect(); |
<!-- 定义orderUserItemResultMap(嵌套查询) --> <!-- extends:继承已有的ResultMap,输入值为继承的ResultMap的唯一标示 --> <resultMap type="ordersExt" id="orderUserItemSelectResultMap" extends="ordersUserResultMap"> <!-- 映射关联关系(一对多) --> <!-- collection标签:定义一个一对多关系--> <collection property="detailList" column="id" select="selectDetailById"></collection> </resultMap> <select id="selectDetailById" parameterType="int" resultType="Orderdetail"> select * from orderdetail where id = #{id} </select> <!-- 查询订单信息,包括用户名称、用户地址,订单商品信息(嵌套查询) --> <select id="findOrdersResultMapOfCollectionSelect" resultMap="orderUserItemSelectResultMap"> SELECT <include refid="select_orders" />, <include refid="select_user" /> FROM orders, user WHERE orders.user_id = user.id </select> |
/** * 查询订单信息,包括用户名称、用户地址,订单商品信息(嵌套查询) */ @Test public void testFindOrdersResultMapOfCollectionSelect() { SqlSession sqlSession = sqlSessionFactory.openSession(); OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class); // 测试 List<OrdersExt> list = ordersMapper.findOrdersResultMapOfCollectionSelect(); System.out.println(list); sqlSession.close(); } |
原文地址:http://liuzedong.blog.51cto.com/8906170/1664073