Java自定義映射resultMap定義及用法
搭建Mybatis框架
①引入相關(guān)的依賴
②一些配置文件給復(fù)制到我們的resources文件夾下
例如:jdbc.properties;log4j.xml
③創(chuàng)建mybatis-config核心配置文件
④創(chuàng)建utils包和pojo包和mapper包以及映射文件所對(duì)應(yīng)的包
⑤創(chuàng)建數(shù)據(jù)庫(kù)的表
t_emp
t_dept
⑥在pojo創(chuàng)建數(shù)據(jù)庫(kù)對(duì)應(yīng)的實(shí)體類
也就是創(chuàng)建屬性,getset方法和構(gòu)造器,還有重寫toString方法
⑦mapper包下創(chuàng)建接口,看對(duì)表有什么操作需求
⑧在配置文件中創(chuàng)建mybatis-mapper,對(duì)應(yīng)mapper接口
一、resultMap處理字段和屬性的映射關(guān)系
①mapper接口:
/** * 根據(jù)id查詢員工信息 * @param empId * @return */ Emp getEmpByeEmpId(@Param("empId") Integer empId);
②mapper映射文件:
<!-- Emp getEmpByEmpId(@Param("empId") Integer empId);--> <select id="getEmpByEmpId" resultMap="empResultMap"> select * from t_emp where emp_id = #{empId} </select>
③ 測(cè)試類
@Test public void testGetEmpByEmpId() { SqlSession sqlSession = SqlSessionUtil.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = mapper.getEmpByEmpId(1); System.out.println(emp); }
結(jié)果:
由于字段名和屬性名不一致,而且沒有創(chuàng)建映射關(guān)系,java中是駝峰的命名方式,而我們mysql中是下劃線的命名方式,所以這時(shí),我們就需要一些操作來(lái)將它們進(jìn)行對(duì)應(yīng)
Emp{empId=null,empName='null',age=20,gender='男'}
字段名和屬性名不一致的情況,如何處理映射關(guān)系
1.為查詢的字段設(shè)置別名,和屬性名保持一致
2.當(dāng)字段符合MySQL的要求使用_,而屬性符合java的要求使用駝峰
此時(shí)可以在Mybatis的核心配置文件中設(shè)置一個(gè)全局配置,可以自動(dòng)將下劃線映射為駝峰
emp_id--》empId ,emp_name--》empName
3.使用resultMap自定義映射處理
方式一:起別名
select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId}
方式二: 使用標(biāo)簽進(jìn)行配置
<settings> <!--將下劃線映射為駝峰--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
<select id="getEmpByEmpId" resultType="Emp"> select * from t_emp where emp_id = #{empId} </select>
方式三:設(shè)置自定義映射
resultMap:設(shè)置自定義映射
resultMap:設(shè)置自定義映射
屬性:
id:表示自定義映射的唯一標(biāo)識(shí)
type:查詢的數(shù)據(jù)要映射的實(shí)體類的類型
子標(biāo)簽:
id:設(shè)置主鍵的映射關(guān)系
result:設(shè)置普通字段的映射關(guān)系
association:設(shè)置多對(duì)一的映射關(guān)系(處理集合類型的屬性)
collection:設(shè)置一對(duì)多的映射關(guān)系(處理集合類型的屬性)
屬性:
property:設(shè)置映射關(guān)系中實(shí)體類中的屬性名,必須是處理的實(shí)體類類型中的屬性名
column:設(shè)置映射關(guān)系中表中的字段名,必須是sql查詢出的某個(gè)字段
<resultMap id="empResultMap" 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> </resultMap> <!-- Emp getEmpByEmpId(@Param("empId") Integer empId);--> <select id="getEmpByEmpId" resultMap="empResultMap"> select * from t_emp where emp_id = #{empId} </select>
二、多對(duì)一映射處理
處理多對(duì)一的映射關(guān)系:
1.級(jí)聯(lián)方式處理
2.association :處理多對(duì)一的映射關(guān)系(處理實(shí)體類類型的屬性)
property:設(shè)置需要處理映射關(guān)系的屬性和屬性名
javaType:設(shè)置要處理的屬性的類型
3.分布查詢
/** * 獲取員工以及對(duì)應(yīng)的部門信息 * @param empId * @return */ Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);
方式一:級(jí)聯(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> <result column="dept_id" property="dept.deptId"></result> <result column="dept_name" property="dept.deptName"></result> </resultMap> <!-- Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);--> <select id="getEmpAndDeptByEmpId" 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>
方式二:使用association處理映射關(guā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> <association property="dept" javaType="Dept"> <id column="dept_id" property="deptId"></id> <result column="dept_name" property="deptName"></result> </association> </resultMap> <!-- Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);--> <select id="getEmpAndDeptByEmpId" 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>
測(cè)試類:
@Test public void testGetEmpAndDeptByEmpId(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = mapper.getEmpAndDeptByEmpId(2); System.out.println(emp); sqlSession.close(); }
方式三:分布查詢
EmpMapper接口中添加方法
/** * 通過(guò)分布查詢查詢員工以及所對(duì)應(yīng)的部門信息的第一步 * @param empId * @return */ Emp getEmpAndDeptByStep(@Param("empId") Integer empId);
EmpMapper映射配置文件中添加操作
property:設(shè)置需要處理映射關(guān)系的屬性和屬性名
select:設(shè)置分布查詢的sql的唯一標(biāo)識(shí)
column:將查詢出的某個(gè)字段作為分布查詢的sql的條件
fetchType:在開啟了延遲加載的環(huán)境中,通過(guò)該屬性設(shè)置當(dāng)前的分布查詢是否使用延遲加載
fetchType="eager(立即加載)\lazy(延遲加載)"
<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.getEmpAndDeptByStepTwo" column="dept_id"> </association> </resultMap> <!-- Emp getEmpAndDeptByStep(@Param("empId") Integer empId);--> <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap"> select * from t_emp where emp_id = #{empId} </select>
因?yàn)樾枰獌蓚€(gè)sql來(lái)查,所以我們把DeptMapper接口和對(duì)應(yīng)的映射文件創(chuàng)建出來(lái)
DeptMapper接口
/** * 通過(guò)分布查詢查詢員工以及所對(duì)應(yīng)的部門信息的第二步 * @param deptId * @return */ Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
DeptMapper映射文件
<!-- Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);--> <select id="getEmpAndDeptByStepTwo" resultType="Dept"> select * from t_dept where dept_id = #{deptId} </select>
分步查詢的優(yōu)點(diǎn):可以實(shí)現(xiàn)延遲加載
但是必須在核心配置文件中設(shè)置全局配置信息:
lazyLoadingEnabled:延遲加載的全局開關(guān)。當(dāng)開啟時(shí),所有關(guān)聯(lián)對(duì)象都會(huì)延遲加載
aggressiveLazyLoading:當(dāng)開啟時(shí),任何方法的調(diào)用都會(huì)加載該對(duì)象的所有屬性。否則,每個(gè)屬性會(huì)按需加載
此時(shí)就可以實(shí)現(xiàn)按需加載,獲取的數(shù)據(jù)是什么,就只會(huì)執(zhí)行相應(yīng)的sql。此時(shí)可通過(guò)association和collection中的fetchType屬性設(shè)置當(dāng)前的分步查詢是否使用延遲加載, fetchType="lazy(延遲加 載)|eager(立即加載)"
引入,延遲加載:
<!--開啟延遲加載--> <setting name="lazyLoadingEnabled" value="true"/> <!--按需加載--> <setting name="aggressiveLazyLoading" value="false"/>
因?yàn)樵谂渲梦募渲檬侨旨虞d,所以是針對(duì)于所有的分布查詢,如果我們要想讓某一個(gè)分布查詢來(lái)實(shí)現(xiàn)一個(gè)完整的加載,這個(gè)時(shí)候就需要去association標(biāo)簽,也就是實(shí)現(xiàn)分布查詢的地方,添加一個(gè)屬性fatchType。
三、一對(duì)多映射處理
處理一對(duì)多的映射關(guān)系:
1.collection
2.分布查詢
方式一:collection
例如一個(gè)部門對(duì)應(yīng)多個(gè)員工
private List<Emp> emps;
①DeptMapper接口
/** * 查詢部門以及部門中的員工信息 * @param deptId * @return */ Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);
②DeptMapper映射配置文件
collection:用于處理一對(duì)多映射關(guān)系
ofType:設(shè)置集合類型的屬性中存儲(chǔ)的數(shù)據(jù)的類型
<resultMap id="deptAndEmpResultMap" 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> <!-- Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);--> <select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap"> select * from t_dept left join t_emp on t_dept.dept_id = t_emp.dept_id where t_dept.dept_id = #{deptId} </select>
③測(cè)試類:
@Test public void testGetDeptAndEmpByDeptId(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); DeptMapper mapper = sqlSession.getMapper(DeptMapper.class); Dept dept = mapper.getDeptAndEmpByDeptId(1); System.out.println(dept); sqlSession.close(); }
方式二:分布查詢
DeptMapper接口
/** * 通過(guò)分布查詢查詢部門以及部門中的員工信息的第一步 * @param deptId * @return */ Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);
DeptMapper映射文件
<resultMap id="deptAndEmpResultMapByStep" type="Dept"> <id column="dept_id" property="deptId"></id> <result column="dept_name" property="deptName"></result> <collection property="emps" select="" column=""> </collection> </resultMap> <!-- Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);--> <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapByStep"> select * from t_dept where dept_id = #{deptId} </select>
EmpMapper接口
/** * 通過(guò)分布查詢查詢部門以及部門中的員工信息的第二步 * @param deptId * @return */ List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);
EmpMapper映射文件
<!-- List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);--> <select id="getDeptAndEmpByStepTwo" resultType="Emp"> select * from t_emp where dept_id = #{deptId} </select>
然后把EmpMapper寫的引入到DeptMapper中
<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" fetchType="eager" select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="dept_id"> </association> </resultMap>
測(cè)試類:
@Test public void testGetDeptAndEmpByStep(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); DeptMapper mapper = sqlSession.getMapper(DeptMapper.class); Dept dept = mapper.getDeptAndEmpByStepOne(1); // System.out.println(dept); System.out.println(dept.getDeptName()); sqlSession.close(); }
到此這篇關(guān)于Java自定義映射resultMap使用方法詳解的文章就介紹到這了,更多相關(guān)Java resultMap內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot ThreadLocal 簡(jiǎn)單介紹及使用詳解
ThreadLocal 叫做線程變量,意思是 ThreadLocal 中填充的變量屬于當(dāng)前線程,該變量對(duì)其他線程而言是隔離的,也就是說(shuō)該變量是當(dāng)前線程獨(dú)有的變量,這篇文章主要介紹了SpringBoot ThreadLocal 的詳解,需要的朋友可以參考下2024-01-01模擬Spring的簡(jiǎn)單實(shí)現(xiàn)
本文的主要內(nèi)容就是學(xué)習(xí)Spring的開端,模擬一下Spring的實(shí)現(xiàn),感興趣的小伙伴可以參考一下2015-10-10SpringBoot項(xiàng)目的logback日志配置(包括打印mybatis的sql語(yǔ)句)
這篇文章主要介紹了SpringBoot項(xiàng)目的logback日志配置(包括打印mybatis的sql語(yǔ)句),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Java長(zhǎng)字符串加密的實(shí)現(xiàn)
在Java中,字符串是一種常見的數(shù)據(jù)類型,我們經(jīng)常需要對(duì)其進(jìn)行加密和解密,本文主要介紹了Java長(zhǎng)字符串加密的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02java?集合工具類Collections及Comparable和Comparator排序詳解
這篇文章主要介紹了java集合工具類Collections及Comparable和Comparator排序詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06IDEA2019.2.2配置Maven3.6.2打開出現(xiàn)Unable to import Maven project
這篇文章主要介紹了IDEA2019.2.2配置Maven3.6.2打開出現(xiàn)Unable to import Maven project,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringBoot利用EasyExcel實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)
EasyExcel是一個(gè)基于Java的、快速、簡(jiǎn)潔、解決大文件內(nèi)存溢出的Excel處理工具,它能讓你在不用考慮性能、內(nèi)存的等因素的情況下,快速完成Excel的讀、寫等功能看,本文就將介紹如何利用EasyExcel實(shí)現(xiàn)導(dǎo)出數(shù)據(jù),需要的朋友可以參考下2023-07-07