mybatis深入講解resultMap的定義及用法
我們知道 ,mybatis框架存在pojo對象映射 , 直接將查詢到的結果封裝到對象中給我們返回, 但如果數(shù)據(jù)庫的中的列和java中類屬性名就是不一致,或者如果我們實際返回的對象需要去關聯(lián)其他的對象(也就是說,其他類的對象作為我們這個類的成員變量),那么這時候使用resultType肯定是不行的
這里我們則需要去定義 resultMap來完成我們的需求
定義resultMap的過程就是描述如何從數(shù)據(jù)庫結果集中去加載對象
resultMap多用于多表查詢間的映射關系, 例如 :
我們以部門和員工為例 , 一個部門有多個員工 , 一個員工屬于一個部門
建立部門表和員工表
CREATE TABLE dept( -- 部門表 id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(10) ) CREATE TABLE employee( -- 員工表 id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(10), age INT, deptId INT )
在java中創(chuàng)建 Dept(部門)類 和 Employee(員工)類
public class Employee { //員工類 private Integer id; private String name; private Integer age; // 一個員工關聯(lián)一個部門 private Dept dept; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", dept=" + dept + '}'; } }
public class Dept { private Integer id; private String name; // 一個部門有多個員工 ,使用List集合存儲 private List<Employee> list; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Employee> getList() { return list; } public void setList(List<Employee> list) { this.list = list; } @Override public String toString() { return "Dept{" + "id=" + id + ", name='" + name + '\'' + ", list=" + list + '}'; } }
首先我們查詢員工 , 定義dao層接口 :
public interface EmployeeDao { // 查詢所有員工 List<Employee> selectAllEmployee(); }
由于我們在對象中關聯(lián)了其他對象, 所以已經不是普通映射 ,這里我們定義 resultMap
<resultMap id="employeeMap" type="Employee"> <!--主鍵列--> <id column="id" property="id"/> <!--其他屬性映射--> <result property="name" column="ename"/> <result property="age" column="age"/> <!--一對一關聯(lián)--> <association property="dept" javaType="Dept"> <!--需要映射的對象屬性--> <result property="name" column="dname"/> </association> </resultMap> <select id="selectAllEmployee" resultMap="employeeMap"> SELECT e.id,e.name ename,e.age,d.name dname FROM employee e LEFT JOIN dept d ON e.deptId = d.id </select>
resultMap 中的id 是唯一標識 , 相當于名字 , type類型是我們要返回的類型
<select>中使用resultMap , 傳入剛定義的id即可
這樣在java代碼中我們就可以得到我們想要的映射格式
查詢部門 :
public interface DeptDao { //查詢部門 List<Dept> selectDept(); }
定義與使用resultMap
<resultMap id="deptMap" type="Dept"> <id column="id" property="id"/> <result column="dname" property="name"/> <!--collection為一對多 , 這里一個部門包含多個員工--> <collection property="list" javaType="List" ofType="Employee"> <result property="name" column="ename"/> </collection> </resultMap> <select id="selectDept" resultMap="deptMap"> SELECT d.id,d.name dname,e.name ename FROM dept d LEFT JOIN employee e ON d.id = e.deptId </select>
這里 JavaType我們選擇list , 因為用list集合來存儲多個員工信息, ofType是list集合中實際包含的對象名,這里是員工 Employee
通過resultMap 我們就可以得到自己想要的映射關系
到此這篇關于mybatis深入講解resultMap的定義及用法的文章就介紹到這了,更多相關mybatis resultMap內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Cloud Config 使用本地配置文件方式
這篇文章主要介紹了Spring Cloud Config 使用本地配置文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07關于spring項目中無法加載resources下文件問題及解決方法
在學習Spring過程中,TestContext框架試圖檢測一個默認的XML資源位置,再resources下創(chuàng)建了一個com.example的文件夾,執(zhí)行時,報錯,本文給大家介紹spring項目中無法加載resources下文件,感興趣的朋友跟隨小編一起看看吧2023-10-10如何在IDEA啟動多個Spring Boot工程實例(圖文)
這篇文章主要介紹了如何在IDEA啟動多個Spring Boot工程實例(圖文),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09