Mybatis中的關係對映及懶載入機制
關係對映可分為:一對一、一對多、多對多
對映多對一、一對一的關係
1).使用列的別名
①.若不關聯資料表,則可以得到關聯物件的id屬性
②.若還希望得到關聯物件的其它屬性。則必須關聯其它的資料表
舉例說明:
1.建立資料庫,建立一個員工表和部門表,員工表tbl_employee的欄位有主鍵id,username,gender,email和外來鍵d_id;部門表tbl_dept有主鍵id和dept_name;程式碼如下(要先建立部門表,否則員工表有外來鍵,沒有部門表無法建立員工表)
CREATE TABLE tbl_dept(
id INT (11) PRIMARY KEY AUTO_INCREMENT,
dept_name VARCHAR(255)
)
CREATE TABLE `tbl_employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`d_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_emp_dept` (`d_id`),
FOREIGN KEY (`d_id`) REFERENCES `tbl_dept` (`id`)
)
2.建立相應的實體類和Mapper介面!
3.寫關聯的SQL語句
SELECT e.`id` id , e.`user_name` user_name, e.`gender` gender,e.`d_id` d_id,d.`id` did,d.`dept_name` dept_name
FROM `t bl_employee` e, tbl_dept d
WHERE e.`d_id` = d.`id` AND e.id = 1
4.在sql對映檔案中寫對映sql語句
方法一:【聯合查詢:級聯屬性封裝結果集】
<!-- 聯合查詢:級聯屬性封裝結果集 -->
<resultMap type="com.neuedu.entity.Employee" id="getEmployeeAndDeptMap">
<id column="id" property="id"/>
<result column="user_name" property="userName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="did" property="depart.id"/>
<result column="dept_name" property="depart.deptName"/>
</resultMap>
<!-- public Employee getEmployeeAndDept(Integer id); -->
<select id="getEmployeeAndDept" resultMap="getEmployeeAndDeptMap">
SELECT e.`id` id , e.`user_name` user_name, e.`gender` gender,e.`email` email,e.`d_id` d_id,d.`id` did,d.`dept_name` dept_name
FROM `tbl_employee` e, tbl_dept d
WHERE e.`d_id` = d.`id` AND e.id = #{id}
</select>
注意:即使使用resultMap來對映,對於“對一”關聯關係可以不使用association
方法二:【使用association來定義關聯物件的規則,[比較正規的,推薦的方式]】
<!-- 聯合查詢:使用association封裝結果集 -->
<resultMap type="com.neuedu.entity.Employee" id="getEmployeeAndDeptMap">
<id column="id" property="id"/>
<result column="user_name" property="userName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!--
association可以指定聯合的javaBean物件
property="depart":指定哪個屬性是聯合的物件
javaType:指定這個屬性物件的型別【不能省略】
-->
<association property="depart" javaType="com.neuedu.entity.Department">
<id column="did" property="id"/>
<result column="dept_name" property="deptName"/>
</association>
</resultMap>
<!-- public Employee getEmployeeAndDept(Integer id); -->
<select id="getEmployeeAndDept" resultMap="getEmployeeAndDeptMap">
SELECT e.`id` id , e.`user_name` user_name, e.`gender` gender,e.`email` email,e.`d_id` d_id,d.`id` did,d.`dept_name` dept_name
FROM `tbl_employee` e, tbl_dept d
WHERE e.`d_id` = d.`id` AND e.id = #{id}
</select>
相當於使用巢狀結果集的形式。
方法三:使用Association進行分步查詢
使用association進行分步查詢的步驟:
1.先按照員工id查詢員工資訊
2.根據查詢員工資訊中d_id值取部門表查出部門資訊
3.部門設定到員工中:
<select id="getDepartById" resultType="com.neuedu.entity.Department">
SELECT id ,dept_name deptName FROM tbl_dept WHERE id = #{id}
</select>
<resultMap type="com.neuedu.entity.Employee" id="myEmpByStep">
<id column="id" property="id"/>
<result column="user_name" property="userName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!--
association定義關聯物件的封裝規則
select:表明當前屬性是呼叫指定的方法查出的結果
column:指定將哪一列的值傳給這個方法
流程:使用select指定的方法(傳入column指定的這列引數的值)查出物件,並封裝給property指定的屬性。
-->
<association property="depart" select="getDepartById" column="d_id"></association>
</resultMap>
<!-- public Employee getEmpAndDept(Integer id); -->
<select id="getEmpAndDept" resultMap="myEmpByStep">
select * from tbl_employee where id =#{id}
</select>
對映多對多的關聯關係
舉例說明:查詢部門的時候,將部門對應的所有員工資訊也查詢出來。
1.修改Department實體類【新增Employee集合,併為該集合提供getter/setter方法】
public class Department {
private Integer id;
private String deptName;
private List<Employee> list;
public List<Employee> getList() {
return list;
}
public void setList(List<Employee> list) {
this.list = list;
}
......
}
2.建立DepartmentMapper介面檔案,並新增如下方法:
public Department getDeptByIdPlus(Integer id);
3.在sql對映檔案中寫對映sql語句
方法一:【collection:巢狀結果集的方式:使用collection標籤定義關聯的集合型別元素的封裝規則】
<!-- public Department getDeptByIdPlus(Integer id); -->
<resultMap type="com.neuedu.entity.Department" id="getDeptByIdPlusMap">
<id column="did" property="id"/>
<result column="dept_name" property="deptName"/>
<!--
collection:定義關聯集合型別的屬性的封裝規則
property:指定實體類的屬性集合
ofType:指定集合裡面元素的型別
-->
<collection property="list" ofType="com.neuedu.entity.Employee">
<!-- 定義這個集合中元素的封裝規則 -->
<id column="eid" property="id"/>
<result column="user_name" property="userName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</collection>
</resultMap>
<select id="getDeptByIdPlus" resultMap="getDeptByIdPlusMap">
SELECT d.`id` did, d.`dept_name` dept_name,e.`id` eid,e.`user_name` user_name,e.`email` email,e.`gender` gender
FROM `tbl_dept` d
LEFT JOIN tbl_employee e
ON e.`d_id` = d.`id`
WHERE d.`id` = #{id}
</select>
方法二:使用分步查詢結果集的方式:
1.如果使用分步查詢的話,我們的sql語句就應該為:
SELECT * FROM `tbl_dept` WHERE id = 2;
SELECT * FROM `tbl_employee` WHERE d_id = 2;
2.在DepartmentMapper介面檔案中新增方法,如下所示:
public Department getDeptWithStep(Integer id);
3.再從EmployeeMapper介面中新增一個方法,如下所示:
public List<Employee> getEmployeeByDeptId(Integer deptId);
並在響應的sql對映檔案中新增相應的sql語句
<select id="getEmployeeByDeptId" resultType="com.neuedu.entity.Employee">
select * from tbl_employee where d_id = #{departId}
</select>
4.在DepartmentMapper對映檔案中:
<resultMap type="com.neuedu.entity.Department" id="getDeptWithStepMap">
<id column="id" property="id"/>
<result column="dept_name" property="deptName"/>
<collection property="list" select="com.neuedu.mapper.EmployeeMapper.getEmployeeByDeptId" column="id"></collection>
</resultMap>
<select id="getDeptWithStep" resultMap="getDeptWithStepMap">
SELECT id ,dept_name FROM tbl_dept WHERE id = #{id}
</select>
注意:
1.必須使用collection節點進行對映
2.collection標籤
對映多的一端的關聯關係,使用ofType指定集合中的元素型別
columnprefix:指定列的字首
使用情境:若關聯的資料表和之前的資料表有相同的列名,此時就需要給關聯的列其”別名”.
若有多個列需要起別名,可以為所有關聯的資料表的列都加上相同的字首,然後再對映時指定字首。
補充:懶載入機制(按需載入)
在分步查詢這裡
我們每次查詢Employee物件的時候,都將關聯的物件查詢出來了。
而我們想能不能我在需要部門資訊的時候,再去查詢,不需要的時候就不用查詢了。
答案:可以的
我們只需要在分步查詢的基礎之上加上兩個配置:
1.在mybatis的全域性配置檔案中加入兩個屬性:
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 開啟懶載入機制 ,預設值為false-->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 開啟的話,每個屬性都會直接全部加載出來;禁用的話,只會按需加載出來,預設值為true -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
這時當只需員工基礎資訊(不需要部門資訊)時,系統只會傳送執行一條SQL語句。
注意:一定是要在分步查詢的基礎上!