【SSM-MyBatis框架】關聯查詢--多對多查詢
1. 需求:
查詢使用者及使用者購買商品的資訊:
2.對映思路:
將使用者資訊對映到user中。
在user中新增List<Order> orderList屬性,將使用者建立的訂單對映到orderList屬性中。
在Order中新增List<Orderdetil> orderDetilList屬性,將訂單中的明細對映到orderDetilList屬性中。
在OrderDeti類中,新增Items item屬性,將商品資訊對映到Item中。
3.pojo類:
User:
public class User { private int id; private String username;// 使用者姓名 private String sex;// 性別 private Date birthday;// 生日 private String address;// 地址 private List<Orders> orders;
Orders:
public class Orders {
private Integer id;
private Integer userId;
private String number;
private Date createtime;
private String note;
//使用者資訊
private User user;
private List<Orderdetail> orderdetails;
Orderdetail:
public class Orderdetail { private Integer id; private Integer ordersId; private Integer itemsId; private Integer itemsNum; //明細對應的商品資訊 private Items items;
4.mapper.xml:
<select id="findUser" resultMap="userMapper" > SELECT orders.*, user.username, user.sex, user.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_id, items.`id`, items.`name`, items.`pic`, items.`price`, items.`createtime`, 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> <!-- 多對多關係的對映 --> <resultMap type="cn.edu.hpu.ssm.po.User" id="userMapper"> <id column="user_id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <result column="address" property="address"/> <collection property="orders" ofType="cn.edu.hpu.ssm.po.Orders"> <id column="id" property="id"/> <result column="user_id" property="userId"/> <result column="number" property="number"/> <result column="createtime" property="createtime"/> <result column="note" property="note"/> <collection property="orderdetails" ofType="cn.edu.hpu.ssm.po.Orderdetail"> <id column="orderdetail_id" property="id"/> <result column="orders_id" property="ordersId"/> <result column="items_id" property="itemsId"/> <result column="items_num" property="itemsNum"/> <association property="items" javaType="cn.edu.hpu.ssm.po.Items"> <id column="items_id" property="id"/> <result column="name" property="name"/> <result column="price" property="price"/> <result column="pic" property="pic"/> <result column="createtime" property="createtime"/> <result column="detail" property="detail"/> </association> </collection> </collection> </resultMap>
5.mapper.java 介面:
/**
* 多對多
*/
public List<User> findUser();
6.多對多總結:
將查詢使用者購買的商品資訊明細清單,(使用者名稱、使用者地址、購買商品名稱、購買商品時間、購買商品數量)
針對上邊的需求就使用resultType將查詢到的記錄對映到一個擴充套件的pojo中,很簡單實現明細清單的功能。
一對多是多對多的特例,如下需求:
查詢使用者購買的商品資訊,使用者和商品的關係是多對多關係。
需求1:
查詢欄位:使用者賬號、使用者名稱稱、使用者性別、商品名稱、商品價格(最常見)
企業開發中常見明細列表,使用者購買商品明細列表,
使用resultType將上邊查詢列對映到pojo輸出。
需求2:
查詢欄位:使用者賬號、使用者名稱稱、購買商品數量、商品明細(滑鼠移上顯示明細)
使用resultMap將使用者購買的商品明細列表對映到user物件中。
總結:
使用resultMap是針對那些對查詢結果對映有特殊要求的功能,,比如特殊要求對映成list中包括多個list。
7.ResultMap總結:
ResultType:
作用:
將查詢的結果按照pojo屬性名,一一對映到pojo的屬性中去。
場合:
常見一些明細資訊的展示,比如使用者購買的商品的明細,將關聯查詢的資訊全部展示在頁面上時,此時可是使用ResultType將每條記錄對映到pojo類中,在前端頁面遍歷list即可。
ResultMap:
使用association和collection完成一對一和一對多的高階對映(對結果集有特殊要求)。
association:
作用:
將關聯查詢表,對映到一個pojo物件中。
場合:
為了方便查詢關聯資訊可以使用association將關聯訂單資訊對映為pojo物件的pojo屬性中。如:查詢訂單和關聯使用者資訊。
使用ResultType無法將查詢結果對映到pojo物件中pojo屬性裡,根據對結果集查詢遍歷的需要選擇使用ResultType還是使用ResultMap。
collection:
作用:
將關聯查詢出來的資訊,對映到一個list集合中。
場合:
為了方便查詢遍歷關聯資訊,可是使用collection將關聯資訊對映到list集合中。如:查詢使用者許可權範圍模組及模組下的選單,可以使用collection將模組及模組下的選單,對映到list中。目的是為了方便查詢結果集進行遍歷查詢。
如果使用ResultType無法對映到list中。