一對多套一對多java
Items類:sql
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代碼調用:
mybatis
//一對多套一對多:用戶以及用戶的訂單和訂單明細信息 @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); } }
執行結果:
app
多對多(manyToMany)ide
<!-- 多對多:查詢用戶,用戶下的訂單和訂單明細以及商品信息 --> <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代碼調用同上,只需修改:this
List<User> list = orderMapper.findUserAndOrdersDetailsAndItems();
執行結果:
spa