Mybatis中的自定義映射resultMap
前言
一、resultMap處理字段和屬性的映射關(guān)系
二、多對一映射處理
1、級聯(lián)方式處理映射關(guān)系
2、使用association處理映射關(guān)系
3、分步查詢
查詢員工信息查詢部門信息
三、一對多映射處理
1、collection
2、分步查詢查詢部門信息根據(jù)部門id查詢部門中的所有員工
四、延遲加載
一、resultMap處理字段和屬性的映射關(guān)系
- resultMap:設(shè)置自定義映射
- 屬性:
- id:表示自定義映射的唯一標識,不能重復(fù)
- type:查詢的數(shù)據(jù)要映射的實體類的類型
- 子標簽:
- id:設(shè)置主鍵的映射關(guān)系
- result:設(shè)置普通字段的映射關(guān)系
- 子標簽屬性:
- property:設(shè)置映射關(guān)系中實體類中的屬性名
- column:設(shè)置映射關(guān)系中表中的字段名
- 屬性:
- 若字段名和實體類中的屬性名不一致,則可以通過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ù)據(jù)庫的規(guī)則(使用_),實體類中的屬性名符合Java的規(guī)則(使用駝峰)。此時也可通過以下兩種方式處理字段名和實體類中的屬性的映射關(guān)系
1.可以通過為字段起別名的方式,保證和實體類中的屬性名保持一致
<!--List<Emp> getAllEmp();--> <select id="getAllEmp" resultType="Emp"> select eid,emp_name empName,age,sex,email from t_emp </select>
2.可以在MyBatis的核心配置文件中的setting
標簽中,設(shè)置一個全局配置信息mapUnderscoreToCamelCase,可以在查詢表中數(shù)據(jù)時,自動將_類型的字段名轉(zhuǎn)換為駝峰,例如:字段名user_name,設(shè)置了mapUnderscoreToCamelCase,此時字段名就會轉(zhuǎn)換為userName。
核心配置文件詳解
<settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
二、多對一映射處理
查詢員工信息以及員工所對應(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、級聯(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:處理多對一的映射關(guān)系
- property:需要處理多對的映射關(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的唯一標識(namespace.SQLId或mapper接口的全類名.方法名)
- column:設(shè)置分步查詢的條件
//EmpMapper里的方法 /** * 通過分步查詢,員工及所對應(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里的方法 /** * 通過分步查詢,員工及所對應(yīng)的部門信息 * 分步查詢第二步:通過did查詢員工對應(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>
三、一對多映射處理
public class Dept { private Integer did; private String deptName; private List<Emp> emps; //...構(gòu)造器、get、set方法等 }
1、collection
- collection:用來處理一對多的映射關(guān)系
- ofType:表示該屬性對飲的集合中存儲的數(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. 查詢部門信息
/** * 通過分步查詢,查詢部門及對應(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查詢部門中的所有員工
/** * 通過分步查詢,查詢部門及對應(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)點:可以實現(xiàn)延遲加載,但是必須在核心配置文件中設(shè)置全局配置信息:
- lazyLoadingEnabled:延遲加載的全局開關(guān)。當開啟時,所有關(guān)聯(lián)對象都會延遲加載
- aggressiveLazyLoading:當開啟時,任何方法的調(diào)用都會加載該對象的所有屬性。 否則,每個屬性會按需加載
- 此時就可以實現(xiàn)按需加載,獲取的數(shù)據(jù)是什么,就只會執(zhí)行相應(yīng)的sql。此時可通過association和collection中的fetchType屬性設(shè)置當前的分步查詢是否使用延遲加載,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語句都運行了
開啟延遲加載,只運行獲取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的時候才會調(diào)用相應(yīng)的SQL語句
fetchType:當開啟了全局的延遲加載之后,可以通過該屬性手動控制延遲加載的效果,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)知識點,希望對你有所幫助。
積跬步以至千里,積怠惰以至深淵。時代在這跟著你一起努力哦!
相關(guān)文章
詳解用JWT對SpringCloud進行認證和鑒權(quán)
這篇文章主要介紹了詳解用JWT對SpringCloud進行認證和鑒權(quán),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03SpringMVC @GetMapping注解路徑?jīng)_突問題解決
MD5對密碼進行加密存儲是常見的一種加密方式,本文主要介紹了Java雙重MD5加密實現(xiàn)安全登錄,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07SpringBoot集成thymeleaf瀏覽器404的解決方案
前后端不分離的古早 SpringMVC 項目通常會使用 thymeleaf 模板引擎來完成 html 頁面與后端接口之間的交互,如果要將項目架構(gòu)升級成 SpringBoot , thymeleaf 也可以照常集成,但有時候會踩到一些坑,所以本文給大家介紹了SpringBoot集成thymeleaf瀏覽器404的解決方案2024-12-12如何使用stream從List對象中獲取某列數(shù)據(jù)
這篇文章主要介紹了如何使用stream從List對象中獲取某列數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12詳解SpringBoot 解決攔截器注入Service為空問題
這篇文章主要介紹了詳解SpringBoot 解決攔截器注入Service為空問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06SpringBoot實現(xiàn)自定義指標監(jiān)控功能
本文主要介紹了SpringBoot實現(xiàn)自定義指標監(jiān)控功能的實現(xiàn),,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,感興趣的小伙伴跟著著小編來一起來學習吧2024-01-01SpringBoot Data JPA 關(guān)聯(lián)表查詢的方法
這篇文章主要介紹了SpringBoot Data JPA 關(guān)聯(lián)表查詢的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07