MyBatis框架關聯(lián)映射實例詳解
關系映射
1. 關聯(lián)映射概述
在關系型數(shù)據庫中,多表之間存在著三種關聯(lián)關系,分別為一對一,一對多和多對多,如圖
- 一對一的關系:就是在本類中定義對方類型的對象,如A類中定義B類類型的屬性b,B類中定義A類類型的屬性a。
- 一對多的關系:就是一個A類類型對應多個B類類型的情況,需要在A類中以集合的方式引入B類類型的對象,在B類中定義A類類型的屬性a。
- 多對多的關系:在A類中定義B類類型的集合,在B類中定義A類類型的集合。
2. 環(huán)境搭建
創(chuàng)建t_emp表
t_dept表
實體類Dept
package com.atguigu.mybatis.pojo; import java.util.List; public class Dept { private Integer deptId; private String deptName; private List<Emp> emps; public Dept() { } public Dept(Integer deptId, String deptName) { this.deptId = deptId; this.deptName = deptName; } public Integer getDeptId() { return deptId; } public void setDeptId(Integer deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public List<Emp> getEmps() { return emps; } public void setEmps(List<Emp> emps) { this.emps = emps; } @Override public String toString() { return "Dept{" + "deptId=" + deptId + ", deptName='" + deptName + '\'' + ", emps=" + emps + '}'; } }
實體類Emp
package com.atguigu.mybatis.pojo; public class Emp { private Integer empId; private String empName; private Integer age; private String gender; private Dept dept; public Emp() { } public Emp(Integer empId, String empName, Integer age, String gender) { this.empId = empId; this.empName = empName; this.age = age; this.gender = gender; } public Integer getEmpId() { return empId; } public void setEmpId(Integer empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Emp{" + "empId=" + empId + ", empName='" + empName + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", dept=" + dept + '}'; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } }
3.處理字段名和屬性名不一致的情況
SQL語句
接口:
public interface EmpMapper { Emp getEmpById(@Param("empId") Integer empId); }
測試方法:
public void test(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp empById = mapper.getEmpById(1); System.out.println(empById.toString()); }
執(zhí)行測試方法后會得到如下結果:
可以看到,我們的SQl語句并沒有問題,但為什么查詢出的結果會有NUll出現(xiàn)呢,這就是因為我們的數(shù)據庫中的字段名于Emp
實體類的屬性名不一致,因此出現(xiàn)了無法對應的情況。
解決辦法:
1.可以通過為字段起別名的方式,保證和實體類中的屬性名保持一致
select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId};
再次執(zhí)行嘗試:
2.可以在MyBatis的核心配置文件中設置一個全局配置信息mapUnderscoreToCamelCase,
此屬性可以在查詢表中數(shù)據時,自動將_類型的字段名,即下劃線轉換為駝峰
舉個栗子:
例如:字段名user_id
,設置了mapUnderscoreToCamelCase
,此時字段名就會轉換為userId
<resultMap id="empResultMap" type="Emp"> <id property="empId" column="emp_id"></id> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="gender" column="gender"></result> </resultMap> <select id="getEmpById" resultMap="empResultMap"> select * from t_emp where emp_id = #{empId}; </select>
4. 處理一對一映射
調節(jié)數(shù)據庫字段與實體類的屬性對應需要標簽resultMap
,如上文那個簡單的查詢例子就可以這樣寫:
<resultMap id="empResultMap" type="Emp"> <id property="empId" column="emp_id"></id> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="gender" column="gender"></result> </resultMap> <select id="getEmpById" resultMap="empResultMap"> select * from t_emp where emp_id = #{empId}; </select>
屬性:
- id:表示自定義映射的唯一標識
- type:查詢的數(shù)據要映射的實體類的類型
子標簽:
- id:設置主鍵的映射關系
- result:設置普通字段的映射關系
- association :設置多對一的映射關系
- collection:設置一對多的映射關系
屬性:
- property:設置映射關系中實體類中的屬性名
- column:設置映射關系中表中的字段名
5. 處理多對一映射
5.1 級聯(lián)方式處理
<resultMap id="empAndDeptResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <!--部門中的字段dept_id與Emp實體類中的屬性dept中的deptId相對應 --> <!--部門中的字段dept_name與Emp實體類中的屬性dept中的deptName相對應 --> <result column="dept_id" property="dept.deptId"></result> <result column="dept_name" property="dept.deptName"></result> </resultMap> <select id="getEmpAndDeptById" resultMap="empAndDeptResultMap"> SELECT t_emp.*,t_dept.* FROM t_emp LEFT JOIN t_dept ON t_emp.dept_id=t_dept.dept_id where t_emp.emp_id=#{empId} </select>
接口:
測試方法:
public void test(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp empAndDeptById = mapper.getEmpAndDeptById(1); System.out.println(empAndDeptById); }
查詢結果:
5.2 使用association處理映射關系
<resultMap id="empAndDeptResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <association property="dept" javaType="Dept"> <id column="dept_id" property="deptId"></id> <result column="dept_name" property="deptName"></result> </association> </resultMap>
5.3 分步查詢
第一步:查詢員工信息
<resultMap id="empAndDeptByStepResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <association property="dept" select="com.atguigu.mybatis.mapper.DeptMapper.getDeptByStep" column="dept_id"> </association> </resultMap> <select id="getEmpAndDeptByStep" resultMap="empAndDeptByStepResultMap"> select * from t_emp where emp_id=#{empId}; </select>
注意:
select:設置分步查詢,查詢某個屬性的值的sql的標識(namespace.sqlid)
column:將sql以及查詢結果中的某個字段設置為分步查詢的條件
第二步:根據員工所對應的部門 id 查詢部門信息
<select id="getDeptByStep" resultType="com.atguigu.mybatis.pojo.Dept"> select * from t_dept where dept_id=#{deptId}; </select>
測試方法:
public void test(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp empAndDeptByStep = mapper.getEmpAndDeptByStep(1); System.out.println(empAndDeptByStep); }
執(zhí)行結果:
分步查詢的優(yōu)點:可以實現(xiàn)延遲加載
但是必須在核心配置文件中設置全局配置信息:
lazyLoadingEnabled
:延遲加載的全局開關。當開啟時,所有關聯(lián)對象都會延遲加載aggressiveLazyLoading
:當開啟時,任何方法的調用都會加載該對象的所有屬性。否則,每個屬性會按需加載- 此時就可以實現(xiàn)按需加載,獲取的數(shù)據是什么,就只會執(zhí)行相應的 sql 。此時可通 association和 collection 中的
fetchType
屬性設置當前的分步查詢是否使用延遲加載, fetchType=“lazy(延遲加載) | eager(立即加載)”
6. 處理一對多查詢
接口:
使用collection
處理
- collection :設置一對多的映射關系
- ofType :設置 collection 標簽所處理的集合屬性中存儲數(shù)據的類型
<resultMap id="DeptAndEmpByDeptIdResultMap" type="Dept"> <id column="dept_id" property="deptId"></id> <result column="dept_name" property="deptName"></result> <collection property="emps" ofType="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> </collection> </resultMap> <select id="getDeptAndEmpByDeptId" resultMap="DeptAndEmpByDeptIdResultMap"> SELECT t_emp.*,t_dept.* FROM t_dept LEFT JOIN t_emp ON t_emp.dept_id=t_dept.dept_id WHERE t_dept.dept_id=#{deptId} </select>
測試:
public void test4(){ SqlSessionUtils sqlSessionUtils = new SqlSessionUtils(); SqlSession sqlSession = sqlSessionUtils.getSqlSession(); DeptMapper mapper = sqlSession.getMapper(DeptMapper.class); Dept deptAndEmpByDeptId = mapper.getDeptAndEmpByDeptId(1); System.out.println(deptAndEmpByDeptId); }
執(zhí)行結果:
7. 小結
關系映射主要處理復雜的SQl查詢,如子查詢,多表聯(lián)查等復雜查詢,應用此種需求時可以考慮使用。
到此這篇關于MyBatis框架關聯(lián)映射的文章就介紹到這了,更多相關MyBatis關聯(lián)映射內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用java實現(xiàn)http多線程斷點下載文件(一)
Java 多線程斷點下載文件基本原理:利用URLConnection獲取要下載文件的長度、頭部等相關信息,并設置響應的頭部信息,本文將詳細介紹,需要了解更多的朋友可以參考下2012-12-12不調用方法實現(xiàn)hutool導出excel圖片示例詳解
這篇文章主要為大家介紹了不調用方法實現(xiàn)hutool導出excel圖片示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08使用Springboot打成jar包thymeleaf的問題
這篇文章主要介紹了使用Springboot打成jar包thymeleaf的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11