Mybatis中的自定義映射resultMap
前言
一、resultMap處理字段和屬性的映射關(guān)系
二、多對(duì)一映射處理
1、級(jí)聯(lián)方式處理映射關(guān)系
2、使用association處理映射關(guān)系
3、分步查詢
查詢員工信息查詢部門信息
三、一對(duì)多映射處理
1、collection
2、分步查詢查詢部門信息根據(jù)部門id查詢部門中的所有員工
四、延遲加載
一、resultMap處理字段和屬性的映射關(guān)系
- resultMap:設(shè)置自定義映射
- 屬性:
- id:表示自定義映射的唯一標(biāo)識(shí),不能重復(fù)
- type:查詢的數(shù)據(jù)要映射的實(shí)體類的類型
- 子標(biāo)簽:
- id:設(shè)置主鍵的映射關(guān)系
- result:設(shè)置普通字段的映射關(guān)系
- 子標(biāo)簽屬性:
- property:設(shè)置映射關(guān)系中實(shí)體類中的屬性名
- column:設(shè)置映射關(guān)系中表中的字段名
- 屬性:
- 若字段名和實(shí)體類中的屬性名不一致,則可以通過resultMap設(shè)置自定義映射,即使字段名和屬性名一致的屬性也要映射,也就是全部屬性都要列出來
<resultMap id="empResultMap" 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> </resultMap> <!--List<Emp> getAllEmp();--> <select id="getAllEmp" resultMap="empResultMap"> select * from t_emp </select>
若字段名和實(shí)體類中的屬性名不一致,但是字段名符合數(shù)據(jù)庫的規(guī)則(使用_),實(shí)體類中的屬性名符合Java的規(guī)則(使用駝峰)。此時(shí)也可通過以下兩種方式處理字段名和實(shí)體類中的屬性的映射關(guān)系
1.可以通過為字段起別名的方式,保證和實(shí)體類中的屬性名保持一致
<!--List<Emp> getAllEmp();--> <select id="getAllEmp" resultType="Emp"> select eid,emp_name empName,age,sex,email from t_emp </select>
2.可以在MyBatis的核心配置文件中的setting
標(biāo)簽中,設(shè)置一個(gè)全局配置信息mapUnderscoreToCamelCase,可以在查詢表中數(shù)據(jù)時(shí),自動(dòng)將_類型的字段名轉(zhuǎn)換為駝峰,例如:字段名user_name,設(shè)置了mapUnderscoreToCamelCase,此時(shí)字段名就會(huì)轉(zhuǎn)換為userName。
核心配置文件詳解
<settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
二、多對(duì)一映射處理
查詢員工信息以及員工所對(duì)應(yīng)的部門信息
public class Emp { private Integer eid; private String empName; private Integer age; private String sex; private String email; private Dept dept; //...構(gòu)造器、get、set方法等 }
1、級(jí)聯(lián)方式處理映射關(guān)系
<resultMap id="empAndDeptResultMapOne" 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> <!--Emp getEmpAndDept(@Param("eid")Integer eid);--> <select id="getEmpAndDept" resultMap="empAndDeptResultMapOne"> select * from t_emp left join t_dept on t_emp.eid = t_dept.did where t_emp.eid = #{eid} </select>
2、使用association處理映射關(guān)系
- association:處理多對(duì)一的映射關(guān)系
- property:需要處理多對(duì)的映射關(guān)系的屬性名
- javaType:該屬性的類型
<resultMap id="empAndDeptResultMapTwo" 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> <!--Emp getEmpAndDept(@Param("eid")Integer eid);--> <select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo"> select * from t_emp left join t_dept on t_emp.eid = t_dept.did where t_emp.eid = #{eid} </select>
3、分步查詢
1. 查詢員工信息
- select:設(shè)置分布查詢的sql的唯一標(biāo)識(shí)(namespace.SQLId或mapper接口的全類名.方法名)
- column:設(shè)置分步查詢的條件
//EmpMapper里的方法 /** * 通過分步查詢,員工及所對(duì)應(yīng)的部門信息 * 分步查詢第一步:查詢員工信息 * @param * @return com.atguigu.mybatis.pojo.Emp * @date 2022/2/27 20:17 */ Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
<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.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="did"></association> </resultMap> <!--Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);--> <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap"> select * from t_emp where eid = #{eid} </select>
2. 查詢部門信息
//DeptMapper里的方法 /** * 通過分步查詢,員工及所對(duì)應(yīng)的部門信息 * 分步查詢第二步:通過did查詢員工對(duì)應(yīng)的部門信息 * @param * @return com.atguigu.mybatis.pojo.Emp * @date 2022/2/27 20:23 */ Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
<!--此處的resultMap僅是處理字段和屬性的映射關(guān)系--> <resultMap id="EmpAndDeptByStepTwoResultMap" type="Dept"> <id property="did" column="did"></id> <result property="deptName" column="dept_name"></result> </resultMap> <!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);--> <select id="getEmpAndDeptByStepTwo" resultMap="EmpAndDeptByStepTwoResultMap"> select * from t_dept where did = #{did} </select>
三、一對(duì)多映射處理
public class Dept { private Integer did; private String deptName; private List<Emp> emps; //...構(gòu)造器、get、set方法等 }
1、collection
- collection:用來處理一對(duì)多的映射關(guān)系
- ofType:表示該屬性對(duì)飲的集合中存儲(chǔ)的數(shù)據(jù)的類型
<resultMap id="DeptAndEmpResultMap" type="Dept"> <id property="did" column="did"></id> <result property="deptName" column="dept_name"></result> <collection property="emps" 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> <!--Dept getDeptAndEmp(@Param("did") Integer did);--> <select id="getDeptAndEmp" resultMap="DeptAndEmpResultMap"> select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did = #{did} </select>
2、分步查詢
1. 查詢部門信息
/** * 通過分步查詢,查詢部門及對(duì)應(yīng)的所有員工信息 * 分步查詢第一步:查詢部門信息 * @param did * @return com.atguigu.mybatis.pojo.Dept * @date 2022/2/27 22:04 */ Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
<resultMap id="DeptAndEmpByStepOneResultMap" type="Dept"> <id property="did" column="did"></id> <result property="deptName" column="dept_name"></result> <collection property="emps" select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo" column="did"></collection> </resultMap> <!--Dept getDeptAndEmpByStepOne(@Param("did") Integer did);--> <select id="getDeptAndEmpByStepOne" resultMap="DeptAndEmpByStepOneResultMap"> select * from t_dept where did = #{did} </select>
2. 根據(jù)部門id查詢部門中的所有員工
/** * 通過分步查詢,查詢部門及對(duì)應(yīng)的所有員工信息 * 分步查詢第二步:根據(jù)部門id查詢部門中的所有員工 * @param did * @return java.util.List<com.atguigu.mybatis.pojo.Emp> * @date 2022/2/27 22:10 */ List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);
<!--List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);--> <select id="getDeptAndEmpByStepTwo" resultType="Emp"> select * from t_emp where did = #{did} </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í)可通過association和collection中的fetchType屬性設(shè)置當(dāng)前的分步查詢是否使用延遲加載,fetchType=“lazy(延遲加載)|eager(立即加載)”
<settings> <!--開啟延遲加載--> <setting name="lazyLoadingEnabled" value="true"/> </settings>
@Test public void getEmpAndDeptByStepOne() { SqlSession sqlSession = SqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = mapper.getEmpAndDeptByStepOne(1); System.out.println(emp.getEmpName()); }
關(guān)閉延遲加載,兩條SQL語句都運(yùn)行了
開啟延遲加載,只運(yùn)行獲取emp的SQL語句
@Test public void getEmpAndDeptByStepOne() { SqlSession sqlSession = SqlSessionUtils.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = mapper.getEmpAndDeptByStepOne(1); System.out.println(emp.getEmpName()); System.out.println("----------------"); System.out.println(emp.getDept()); }
開啟后,需要用到查詢dept的時(shí)候才會(huì)調(diào)用相應(yīng)的SQL語句
fetchType:當(dāng)開啟了全局的延遲加載之后,可以通過該屬性手動(dòng)控制延遲加載的效果,fetchType=“lazy(延遲加載)|eager(立即加載)”
<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.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="did" fetchType="lazy"></association> </resultMap>
總結(jié)
以上就是Mybatis之自定義映射resultMap的相關(guān)知識(shí)點(diǎn),希望對(duì)你有所幫助。
積跬步以至千里,積怠惰以至深淵。時(shí)代在這跟著你一起努力哦!
- MyBatis自定義映射resultMap的實(shí)現(xiàn)
- Mybatis的resultMap返回map問題
- Mybatis之@ResultMap,@Results,@Result注解的使用
- 解析mybatis-plus中的resultMap簡(jiǎn)單使用
- MyBatis-Plus中如何使用ResultMap的方法示例
- 詳解MyBatis resultType與resultMap中的幾種返回類型
- MyBatis注解開發(fā)之實(shí)現(xiàn)自定義映射關(guān)系和關(guān)聯(lián)查詢
- MyBatis自定義映射關(guān)系和關(guān)聯(lián)查詢實(shí)現(xiàn)方法詳解
相關(guān)文章
詳解用JWT對(duì)SpringCloud進(jìn)行認(rèn)證和鑒權(quán)
這篇文章主要介紹了詳解用JWT對(duì)SpringCloud進(jìn)行認(rèn)證和鑒權(quán),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03SpringMVC @GetMapping注解路徑?jīng)_突問題解決
MD5對(duì)密碼進(jìn)行加密存儲(chǔ)是常見的一種加密方式,本文主要介紹了Java雙重MD5加密實(shí)現(xiàn)安全登錄,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07SpringBoot集成thymeleaf瀏覽器404的解決方案
前后端不分離的古早 SpringMVC 項(xiàng)目通常會(huì)使用 thymeleaf 模板引擎來完成 html 頁面與后端接口之間的交互,如果要將項(xiàng)目架構(gòu)升級(jí)成 SpringBoot , thymeleaf 也可以照常集成,但有時(shí)候會(huì)踩到一些坑,所以本文給大家介紹了SpringBoot集成thymeleaf瀏覽器404的解決方案2024-12-12如何使用stream從List對(duì)象中獲取某列數(shù)據(jù)
這篇文章主要介紹了如何使用stream從List對(duì)象中獲取某列數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Java web項(xiàng)目啟動(dòng)Tomcat報(bào)錯(cuò)解決方案
這篇文章主要介紹了Java web項(xiàng)目啟動(dòng)Tomcat報(bào)錯(cuò)解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07詳解SpringBoot 解決攔截器注入Service為空問題
這篇文章主要介紹了詳解SpringBoot 解決攔截器注入Service為空問題的解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06SpringBoot實(shí)現(xiàn)自定義指標(biāo)監(jiān)控功能
本文主要介紹了SpringBoot實(shí)現(xiàn)自定義指標(biāo)監(jiān)控功能的實(shí)現(xiàn),,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,感興趣的小伙伴跟著著小編來一起來學(xué)習(xí)吧2024-01-01SpringBoot Data JPA 關(guān)聯(lián)表查詢的方法
這篇文章主要介紹了SpringBoot Data JPA 關(guān)聯(lián)表查詢的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07