MyBatis中ResultMap與多表查詢的處理方法
ResultMap與多表查詢的處理
當字段名與實類名不一致時
使用別名進行處理
字段名:emp_name
實體類名:empName
映射文件中寫法:
<select id="getAllEmp" resultType="Emp"> select eid, emp_name empName, age, sex, email, did from t_emp </select>
使用全局配置將下劃線命名映射為駝峰
在mybatis-config.xml文件的properties標簽和typeAlias標簽之間添加settings標簽如下,可以將下劃線式命名映射為駝峰:
<settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
使用resultMap創(chuàng)建自定義的映射關系
在mapper.xml文件中進行定義:
定義resultMap:
<!-- 就算是自定義映射關系,也需要相對應的實體類 --> <resultMap id="empResultMap" type="Emp"> <!-- id用來聲明主鍵,property用來表示實體類中的屬性名、column用來標識數(shù)據(jù)庫表中的字段名 --> <id property="eid" column="eid"></id> <!-- result用來聲明普通字段 --> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="sex" column="sex"></result> <result property="email" column="email"></result> </resultMap>
傳入resultMap的id以用來使用自定義映射
<select id="getAllEmp" resultMap="empResultMap"> select * from t_emp </select>
注意:resultMap一般用于處理多對一、一對多的關系
一對多情況的處理
對于多對一的情況(一個員工只會在一個部門中)
只需要在員工實體類中添加一個部門屬性:
private Integer eid; private String empName; private Integer age; private String sex; private String email; private Integer did; private Dept dept;
通過級聯(lián)屬性賦值resultMap解決多對一問題
在mapper.xml文件中創(chuàng)建resultMap:
<resultMap id="empAndDeptResultMap" type="Emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="sex" column="sex"></result> <result property="email" column="email"></result> <result property="dept.did" column="did"></result> <result property="dept.deptName" column="dept_name"></result> </resultMap>
再將這個傳入語句進行調(diào)用
<!-- Emp getEmpAndDept(@Param("eid") Integer eid);--> <select id="getEmpAndDept" resultMap="empAndDeptResultMap"> select * from t_emp left join t_dept on t_emp.did = t_dept.did where eid=#{eid} </select>
級聯(lián)屬性賦值的方式一般不使用
使用association解決多對一問題
在mapper.xml文件中創(chuàng)建resultMap:
<resultMap id="empAndDeptResultMapAsscoiation" type="Emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="sex" column="sex"></result> <result property="email" column="email"></result> <association property="dept" javaType="Dept"> <id property="did" column="did"></id> <result property="deptName" column="dept_name"></result> </association> </resultMap>
再將之傳入即可
通過分步查詢解決多對一問題
在mapper.xml建立如下:
<!-- 注意在分步查詢的過程中,association中的column代表傳入的條件--> <resultMap id="empAndDeptByStepResultMap" type="emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="sex" column="sex"></result> <result property="email" column="email"></result> <association property="dept" select="com.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="did"></association> </resultMap>
Dept的mapper如下:
<!-- Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);--> <select id="getEmpAndDeptByStepTwo" resultType="Dept"> select * from t_dept where did = #{did} </select>
調(diào)用:
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap"> select * from t_emp where eid = #{eid} </select>
分布查詢的優(yōu)點:延遲加載
當mybatis的查詢語句由多步查詢構(gòu)成時,我們可以開啟mybatis的懶加載,此時若我們只需要第一步的某個屬性,mybatis就不會去調(diào)用第二步的sql語句,這樣在多種場景下最大限度的保證了性能。
在全局配置(mybatis-config.xml)中添加如下配置
<!-- 全局配置:自動將下劃線轉(zhuǎn)為駝峰,懶加載--> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="lazyLoadingEnabled" value="true"></setting> </settings>
同時,我們也可以在association中使用fetchType標簽來使延遲加載變得可控,eager代表立即加載、lazy代表延遲加載
<!-- 注意在分步查詢的過程中,association中的column代表傳入的條件--> <resultMap id="empAndDeptByStepResultMap" type="emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="sex" column="sex"></result> <result property="email" column="email"></result> <association property="dept" select="com.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="did" fetchType="eager"> </association> </resultMap>
多對一情況的處理
在dept實體類中添加多的的那個List:
private List<Emp> empList;
使用collection標簽進行一口氣的處理
在deptMapper下進行如下操作:
<resultMap id="deptAndEmpResultMap" type="Dept"> <id property="did" column="did"></id> <result property="deptName" column="dept_name"></result> <!-- 這里的ofType代表傳入的List的泛型--> <collection property="empList" ofType="Emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="sex" column="sex"></result> <result property="email" column="email"></result> </collection> </resultMap> <select id="getDeptAndEmp" resultMap="deptAndEmpResultMap"> select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_dept.did=#{did} </select>
通過分步查詢進行處理
在DeptMapper中建立第一步的接口:
/** * 通過分步查詢部門以及部門中的員工信息 */ Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
在EmpMapper中建立第二步的接口:
/** * 分步查詢第二步,根據(jù)did查詢員工信息 */ List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);
在EmpMapper.xml文件中定義xml:
<!-- List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);--> <select id="getDeptAndEmpByStepTwo" resultType="Emp"> select * from t_emp </select>
在DeptMapper.xml文件中定義xml:
<resultMap id="deptAndEmpByStepResultMap" type="Dept"> <id property="did" column="did"></id> <result property="deptName" column="dept_name"></result> <collection property="empList" select="com.qinghe.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo" column="did" ></collection> </resultMap> <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap"> select * from t_dept where did = #{did} </select>
到此這篇關于MyBatis中ResultMap與多表查詢的處理的文章就介紹到這了,更多相關ResultMap多表查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Mybatis-Plus多表關聯(lián)查詢的使用案例解析
- MyBatis多表查詢和注解開發(fā)案例詳解
- mybatis-plus多表分頁查詢最佳實現(xiàn)方法(非常簡單)
- Mybatis-plus實現(xiàn)join連表查詢的示例代碼
- MybatisPlus多表連接查詢的具體實現(xiàn)
- mybatis于xml方式和注解方式實現(xiàn)多表查詢的操作方法
- mybatis-plus多表查詢操作方法
- MyBatis?實現(xiàn)動態(tài)排序的多表查詢
- 深入解析MybatisPlus多表連接查詢
- Mybatis分頁查詢主從表的實現(xiàn)示例
- mybatis連接數(shù)據(jù)庫實現(xiàn)雙表查詢
相關文章
Java并發(fā)教程之Callable和Future接口詳解
Java從發(fā)布的第一個版本開始就可以很方便地編寫多線程的應用程序,并在設計中引入異步處理,這篇文章主要給大家介紹了關于Java并發(fā)教程之Callable和Future接口的相關資料,需要的朋友可以參考下2021-07-07Java利用httpclient通過get、post方式調(diào)用https接口的方法
這篇文章主要介紹了Java利用httpclient通過get、post方式調(diào)用https接口的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list
這篇文章主要介紹了SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02