MyBatis中ResultMap與多表查詢的處理方法
ResultMap與多表查詢的處理
當(dāng)字段名與實(shí)類名不一致時(shí)
使用別名進(jìn)行處理
字段名:emp_name
實(shí)體類名:empName
映射文件中寫法:
<select id="getAllEmp" resultType="Emp"> select eid, emp_name empName, age, sex, email, did from t_emp </select>
使用全局配置將下劃線命名映射為駝峰
在mybatis-config.xml文件的properties標(biāo)簽和typeAlias標(biāo)簽之間添加settings標(biāo)簽如下,可以將下劃線式命名映射為駝峰:
<settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
使用resultMap創(chuàng)建自定義的映射關(guān)系
在mapper.xml文件中進(jìn)行定義:
定義resultMap:
<!-- 就算是自定義映射關(guān)系,也需要相對(duì)應(yīng)的實(shí)體類 --> <resultMap id="empResultMap" type="Emp"> <!-- id用來(lái)聲明主鍵,property用來(lái)表示實(shí)體類中的屬性名、column用來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)表中的字段名 --> <id property="eid" column="eid"></id> <!-- result用來(lái)聲明普通字段 --> <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以用來(lái)使用自定義映射
<select id="getAllEmp" resultMap="empResultMap"> select * from t_emp </select>
注意:resultMap一般用于處理多對(duì)一、一對(duì)多的關(guān)系
一對(duì)多情況的處理
對(duì)于多對(duì)一的情況(一個(gè)員工只會(huì)在一個(gè)部門中)
只需要在員工實(shí)體類中添加一個(gè)部門屬性:
private Integer eid; private String empName; private Integer age; private String sex; private String email; private Integer did; private Dept dept;
通過(guò)級(jí)聯(lián)屬性賦值resultMap解決多對(duì)一問(wèn)題
在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>
再將這個(gè)傳入語(yǔ)句進(jìn)行調(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>
級(jí)聯(lián)屬性賦值的方式一般不使用
使用association解決多對(duì)一問(wèn)題
在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>
再將之傳入即可
通過(guò)分步查詢解決多對(duì)一問(wèn)題
在mapper.xml建立如下:
<!-- 注意在分步查詢的過(guò)程中,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)點(diǎn):延遲加載
當(dāng)mybatis的查詢語(yǔ)句由多步查詢構(gòu)成時(shí),我們可以開(kāi)啟mybatis的懶加載,此時(shí)若我們只需要第一步的某個(gè)屬性,mybatis就不會(huì)去調(diào)用第二步的sql語(yǔ)句,這樣在多種場(chǎng)景下最大限度的保證了性能。
在全局配置(mybatis-config.xml)中添加如下配置
<!-- 全局配置:自動(dòng)將下劃線轉(zhuǎn)為駝峰,懶加載--> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="lazyLoadingEnabled" value="true"></setting> </settings>
同時(shí),我們也可以在association中使用fetchType標(biāo)簽來(lái)使延遲加載變得可控,eager代表立即加載、lazy代表延遲加載
<!-- 注意在分步查詢的過(guò)程中,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>
多對(duì)一情況的處理
在dept實(shí)體類中添加多的的那個(gè)List:
private List<Emp> empList;
使用collection標(biāo)簽進(jìn)行一口氣的處理
在deptMapper下進(jìn)行如下操作:
<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>
通過(guò)分步查詢進(jìn)行處理
在DeptMapper中建立第一步的接口:
/** * 通過(guò)分步查詢部門以及部門中的員工信息 */ 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>
到此這篇關(guān)于MyBatis中ResultMap與多表查詢的處理的文章就介紹到這了,更多相關(guān)ResultMap多表查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis-Plus多表關(guān)聯(lián)查詢的使用案例解析
- MyBatis多表查詢和注解開(kāi)發(fā)案例詳解
- mybatis-plus多表分頁(yè)查詢最佳實(shí)現(xiàn)方法(非常簡(jiǎn)單)
- Mybatis-plus實(shí)現(xiàn)join連表查詢的示例代碼
- MybatisPlus多表連接查詢的具體實(shí)現(xiàn)
- mybatis于xml方式和注解方式實(shí)現(xiàn)多表查詢的操作方法
- mybatis-plus多表查詢操作方法
- MyBatis?實(shí)現(xiàn)動(dòng)態(tài)排序的多表查詢
- 深入解析MybatisPlus多表連接查詢
- Mybatis分頁(yè)查詢主從表的實(shí)現(xiàn)示例
- mybatis連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)雙表查詢
相關(guān)文章
Java并發(fā)教程之Callable和Future接口詳解
Java從發(fā)布的第一個(gè)版本開(kāi)始就可以很方便地編寫多線程的應(yīng)用程序,并在設(shè)計(jì)中引入異步處理,這篇文章主要給大家介紹了關(guān)于Java并發(fā)教程之Callable和Future接口的相關(guān)資料,需要的朋友可以參考下2021-07-07Spring IOC創(chuàng)建對(duì)象的兩種方式
這篇文章主要給大家介紹了關(guān)于Spring IOC創(chuàng)建對(duì)象的兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Java利用httpclient通過(guò)get、post方式調(diào)用https接口的方法
這篇文章主要介紹了Java利用httpclient通過(guò)get、post方式調(diào)用https接口的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list
這篇文章主要介紹了SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02springboot對(duì)接微信支付的完整流程(附前后端代碼)
最近在做支付平臺(tái)的項(xiàng)目,承接公司業(yè)務(wù)系統(tǒng)與第三方支付平臺(tái)的對(duì)接任務(wù),主要涉及微信支付、支付寶支付以及理房通支付等第三方平臺(tái),這篇文章主要給大家介紹了關(guān)于springboot對(duì)接微信支付的完整流程,需要的朋友可以參考下2021-08-08