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)文章
SpringBoot整合ELK實現(xiàn)日志監(jiān)控
這篇文章主要為大家詳細介紹了SpringBoot整合ELK實現(xiàn)日志監(jiān)控的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
MyBatis-Plus updateById不更新null值的方法解決
用Mybatis-Plus的updateById()來更新數(shù)據(jù)時,無法將字段設(shè)置為null值,更新后數(shù)據(jù)還是原來的值,本文就來詳細的介紹一下解決方法,具有一定的參考價值,感興趣的可以了解一下2023-08-08
java中java.util.Date和java.sql.Date之間的轉(zhuǎn)換的示例
java.util.Date是java.sql.Date的父類,有時候在和SqlServer數(shù)據(jù)庫打交道時,也會遇到,本文主要介紹了java中java.util.Date和java.sql.Date之間的轉(zhuǎn)換的示例,具有一定的參考價值,感興趣的可以了解一下2024-05-05
java使用elasticsearch分組進行聚合查詢過程解析
這篇文章主要介紹了java使用elasticsearch分組進行聚合查詢過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
利用?SpringBoot?在?ES?中實現(xiàn)類似連表查詢功能
這篇文章主要介紹了如何利用?SpringBoot?在?ES?中實現(xiàn)類似連表的查詢功能,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
IDEA自帶Maven插件找不到settings.xml配置文件
IDEA自帶了Maven插件,最近發(fā)現(xiàn)了一個問題,IDEA自帶Maven插件找不到settings.xml配置文件,本文就來詳細的介紹一下解決方法,感興趣的可以了解一下2023-11-11

