标签:
一对多套一对多
Items类:
public class Items { private int itemId; private String itemName; private Float itemPrice; private String itemDetail; public int getItemId() { return itemId; } public void setItemId(int itemId) { this.itemId = itemId; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public Float getItemPrice() { return itemPrice; } public void setItemPrice(Float itemPrice) { this.itemPrice = itemPrice; } public String getItemDetail() { return itemDetail; } public void setItemDetail(String itemDetail) { this.itemDetail = itemDetail; } @Override public String toString() { return "Items [itemId=" + itemId + ", itemName=" + itemName + ", itemPrice=" + itemPrice + ", itemDetail=" + itemDetail + "]"; } }
<!-- 一对多套一对多:用户以及用户的订单和订单明细信息 --> <resultMap type="user" id="orderDetailInOrderInUserMap"> <!-- 查询结果集的主键 --> <id property="userId" column="userId"/> <result property="username" column="username"/> <result property="sex" column="sex"/> <result property="birthday" column="birthday"/> <collection property="orders" ofType="mybatis_b.model.Order"> <!-- 一对多的时候,id 不能缺省 --> <id property="orderId" column="orderId"/> <result property="orderNumber" column="orderNumber"/> <!-- collect中再嵌套 collect --> <collection property="orderDetails" ofType="mybatis_b.model.OrderDetail"> <id property="orderDetailId" column="orderDetailId"/> <result property="order_id" column="order_id"/> <result property="item_id" column="item_id"/> <result property="item_number" column="item_number"/> <result property="item_price" column="item_price"/> </collection> </collection> </resultMap> <!-- 一对多套一对多:用户以及用户的订单和订单明细信息 --> <select id="findUserAndOrdersAndDetails" resultMap="orderDetailInOrderInUserMap"> select users.userid, users.username, users.birthday, users.sex, orders.orderid, orders.ordernumber, orderdetail.* from users, orders, orderdetail where users.userid = orders.user_id and orders.orderid = orderdetail.order_id </select>
java代码调用:
//一对多套一对多:用户以及用户的订单和订单明细信息 @Test public void testFindUserAndOrdersAndDetails() throws Exception { //获取SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); //指定 mapper 接口的类型,MyBatis通过动态代理的方式实现mapper接口 OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class); List<User> list = orderMapper.findUserAndOrdersAndDetails(); System.out.println(list.size()); Iterator<User> iterator = list.iterator(); while (iterator.hasNext()) { User order = (User) iterator.next(); System.out.println(order); } }
执行结果:
多对多(manyToMany)
<!-- 多对多:查询用户,用户下的订单和订单明细以及商品信息 --> <resultMap type="user" id="itemInOrderDetailInOrderInUserMap"> <!-- 查询结果集的主键 --> <id property="userId" column="userId"/> <result property="username" column="username"/> <result property="sex" column="sex"/> <result property="birthday" column="birthday"/> <collection property="orders" ofType="mybatis_b.model.Order"> <!-- 一对多的时候,id 不能缺省 --> <id property="orderId" column="orderId"/> <result property="orderNumber" column="orderNumber"/> <collection property="orderDetails" ofType="mybatis_b.model.OrderDetail"> <id property="orderDetailId" column="orderDetailId"/> <result property="order_id" column="order_id"/> <result property="item_id" column="item_id"/> <result property="item_number" column="item_number"/> <result property="item_price" column="item_price"/> <association property="item" javaType="mybatis_b.model.Items"> <id property="itemId" column="itemId"/> <result property="itemName" column="itemName"/> <result property="itemPrice" column="itemPrice"/> <result property="itemDetail" column="itemDetail"/> </association> </collection> </collection> </resultMap> <!-- 多对多:查询用户,用户下的订单和订单明细以及商品信息 --> <select id="findUserAndOrdersDetailsAndItems" resultMap="itemInOrderDetailInOrderInUserMap"> select users.userid, users.username, users.birthday, users.sex, orders.orderid, orders.ordernumber, orderdetail.*, items.itemname, items.itemprice, items.itemdetail from users, orders, orderdetail, items where users.userid = orders.user_id and orders.orderid = orderdetail.order_id and items.itemid = orderdetail.item_id </select>
java代码调用同上,只需修改:
List<User> list = orderMapper.findUserAndOrdersDetailsAndItems();
执行结果:
标签:
原文地址:http://my.oschina.net/u/1757476/blog/505999