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