resultMap處理欄位和屬性的映射關系
如果欄位名與物體類中的屬性名不一致,該如何處理映射關系?
-
第一種方法:為查詢的欄位設定別名,和屬性名保持一致
下面是物體類中的屬性名:
private Integer empId; private String empName; private Integer age; private String gender;這是建表時設定的欄位名:
emp_id emp_name age gender我們只需要在Mapper.xml中在寫sql陳述句時,對欄位名進行設定別名,使得與屬性名一致:
select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId} -
第二種方法:當欄位符合Mysql要求使用下劃線,而屬性名符合Java要求使用駝峰,此時可以在Mybatis的核心組態檔中設定一個全域配置資訊mapUnderscoreToCamelCase,就可以在查詢表中資料時,自動將下劃線型別的欄位名轉換為駝峰,
<settings> <!--將下劃線映射為駝峰--> <setting name="mapUnderscoreToCamelCase" value="https://www.cnblogs.com/zzzjian/p/true"/> </settings> -
第三種方法:使用resultMap處理
<!-- resultMap:設定自定義的映射關系 id:唯一標識 type:處理映射關系的物體類的型別 常用標簽: id:處理主鍵和物體類中屬性的映射關系 result:處理普通欄位和物體類中屬性的映射關系 column:設定映射關系中的欄位名,必須是sql查詢出的某個欄位 property:設定映射關系中的屬性的屬性名,必須是處理物體型別型別中的屬性名 --> <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 emId);--> <select id="getEmpByEmpId" resultMap="empResultMap"> select * from t_emp where emp_id = #{empId} </select>
多對一的映射關系
1.級聯方式處理映射關系
當Emp物體類中具有Dept物件,但是欄位中不存在這個屬性,我們需要將Dept物件中的屬性與查詢的欄位名建立映射關系,
<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>
<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>
2.使用association處理映射關系
- association:處理多對一的映射關系(處理物體型別別的屬性)
- property:設定需要處理映射關系的屬性的屬性名
- javaType:設定要處理的屬性的型別
<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>
3.分步查詢
-
首先查詢員工的資訊
/** * 通過分步查詢員工的資訊 * @param empId * @return */ Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);<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> <!-- select:設定分步查詢,查詢某個屬性的值的sql標識(namespace.sqlId) column:將sql以及查詢結果中的某個欄位設定為分步查詢的條件 --> <association property="dept" select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="dept_id"></association> </resultMap> <!-- Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);--> <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap"> select * from t_emp where emp_id = #{empId} </select> -
根據員工所對應的部門id查詢部門資訊
/** * 分步查詢第二步:根據員工所對應的id查詢部門資訊 * @param deptId * @return */ Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);<!-- Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);--> <select id="getEmpAndDeptByStepTwo" resultType="Dept"> select * from t_dept where depy_id = #{deptId} </select> -
分步查詢的優點:可以實作延遲加載,但是必須在核心組態檔中設定全域配置資訊:
lazyLoadingEnabled:延遲加載的全域開關,當開啟時,所有關聯物件都會延遲加載,
aggressiveLazyLoading:當開啟時,任何方法的呼叫都會加載該物件的所有屬性,否則,每個屬性會按需加載
此時就可以實作按需加載,獲取的資料是什么,就會執行相應的sql,此時可通過association和collection中的fetchType屬性設定當前的分步查詢是否使用延遲加載,
一對多的映射關系
1.collection
/**
* 根據部門id查部門中員工的資訊
* @param deptId
* @return
*/
Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);
<resultMap id="deptAndEmpResultMap" type="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
<!--
ofType:設定collection標簽所處理的集合屬性中存盤資料的型別
-->
<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>
2.分步查詢
-
查詢部門資訊
/** * 分步查詢部門以及部門中的員工資訊第一步 * @param id * @return */ Dept getDeptAndEmpByStepOne(@Param("id") Integer id);<resultMap id="deptAnEmpResultMapByStep" type="Dept"> <id column="dept_id" property="depyId"></id> <result column="dept_name" property="deptName"></result> <collection property="emps" select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo" column="dept_id"></collection> </resultMap><!-- Dept getDeptAndEmpByStepOne(@Param("id") Integer id);--> <select id="getDeptAndEmpByStepOne" resultMap=""> select * from t_dept where dept_id = #{deptId} </select> -
根據部門id查詢部門中的員工資訊
/** * 分步查詢部門以及部門中的員工資訊第二步 * @param dept_id * @return */ List<Emp> getDeptAndEmpByStepTwo(@Param("dept_id") Integer dept_id);<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><!--List<Emp> getDeptAndEmpByStepTwo(@Param("dept_id") Integer dept_id);--> <select id="getDeptAndEmpByStepTwo" resultType="Emp"> select * from t_emp where dept_id = #{deptId} </select>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/510843.html
標籤:Java
上一篇:HTTP缺點有哪些,如何解決
下一篇:回溯法實作全排序Ⅰ
