Mybatis聯(lián)合查詢的實(shí)現(xiàn)方法
數(shù)據(jù)庫表結(jié)構(gòu)
department
employee
要求一
現(xiàn)在的要求是輸入 id 把 employee 表的對(duì)應(yīng)員工數(shù)據(jù)查詢出來,并且查詢出該員工的所處部門信息
public class Employee { private Integer id; private String lastName; private String email; private String gender; private Department dept; setter和getter....... }
public class Department { private Integer id; private String departmentName; setter和getter....... }
1、級(jí)聯(lián)屬性封裝結(jié)果集
實(shí)現(xiàn)
這個(gè)要求很明顯就要用到兩個(gè)表,想要把部門信息封裝到Employee
對(duì)象的dept字段需要用到resultMap
屬性
方法一
<!-- public Employee getEmployee(int id); --> <select id="getEmployee" resultMap="emp1"> select e.*, d.id did, d.department_name from employee e, department d where e.d_id = d.id and e.id = #{id} </select> <resultMap id="emp1" type="employee"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <result column="did" property="dept.id"/> <result column="department_name" property="dept.departmentName"/> </resultMap>
方法二
<!-- public Employee getEmployee(int id); --> <select id="getEmployee" resultMap="emp2"> select e.*, d.id did, d.department_name from employee e, department d where e.d_id = d.id and e.id = #{id} </select> <resultMap id="emp2" type="employee"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <association property="dept" javaType="department"> <id column="did" property="id"/> <result column="department_name" property="departmentName"/> </association> </resultMap>
測(cè)試
@Test public void test1() { SqlSession sqlSession = MyTest.getSqlSession(); EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); System.out.println(mapper.getEmployee(1)); }
結(jié)果
2、分步查詢
方法
DepartmentMapper.xml
<!-- public Department getDepartment2(int id); --> <select id="getDepartment2" resultType="department"> select * from department where id = #{id} </select>
EmployeeMaper.xml
<!-- public Employee getEmployee2(int id); --> <!-- 分步查詢 --> <select id="getEmployee2" resultMap="emp3"> select * from employee where id = #{id} </select> <resultMap id="emp3" type="employee"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <association property="dept" select="com.workhah.mapper.department.DepartmentMapper.getDepartment2" column="d_id"/> </resultMap>
測(cè)試
@Test public void test1() { SqlSession sqlSession = MyTest.getSqlSession(); EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); System.out.println(mapper.getEmployee2(1)); }
結(jié)果
要求二
現(xiàn)在的要求是輸入 id 把 department 表對(duì)應(yīng)的部門信息查詢出來,并且查詢?cè)摬块T下的所有員工信息
public class Employee { private Integer id; private String lastName; private String email; private String gender; setter和getter....... }
public class Department { private Integer id; private String departmentName; private List<Employee> employees; setter和getter....... }
3、級(jí)聯(lián)屬性封裝結(jié)果集
方法
<!-- public Department getDepartment(int id); --> <select id="getDepartment" resultMap="dep1"> select d.*, e.id eid, e.last_name, e.email, e.gender from department d left join employee e on d.id = e.d_id where d.id = #{id} </select> <resultMap id="dep1" type="department"> <id column="id" property="id"/> <result column="department_name" property="departmentName"/> <collection property="employees" ofType="employee"> <id column="eid" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection> </resultMap>
測(cè)試
@Test public void test2() { SqlSession sqlSession = MyTest.getSqlSession(); DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class); System.out.println(mapper.getDepartment(1)); }
結(jié)果
4、分步查詢
EmployeeMaper.xml
<!-- public List<Employee> getEmployeeByDid(int did); --> <select id="getEmployeeByDid" resultType="employee"> select * from employee where d_id = #{did} </select>
DepartmentMapper.xml
<!-- public Department getDepartment3(int id); --> <select id="getDepartment3" resultMap="dep2"> select * from department where id = #{id} </select> <resultMap id="dep2" type="department"> <id column="id" property="id"/> <result column="depart_name" property="departName"/> <collection property="employees" ofType="employee" select="com.workhah.mapper.employee.EmployeeMapper.getEmployeeByDid" column="id"/> </resultMap>
測(cè)試
@Test public void test2() { SqlSession sqlSession = MyTest.getSqlSession(); DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class); System.out.println(mapper.getDepartment3(1)); }
結(jié)果
到此這篇關(guān)于 Mybatis聯(lián)合查詢的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān) Mybatis聯(lián)合查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java定時(shí)任務(wù)ScheduledThreadPoolExecutor示例詳解
這篇文章主要介紹了Java定時(shí)任務(wù)ScheduledThreadPoolExecutor示例詳解,這里使用scheduleAtFixedRate方法安排一個(gè)任務(wù),該任務(wù)是一個(gè) Runnable 匿名類,其run方法中調(diào)用了new LoginViewTimeTask().loginStatisticsHandle()方法,需要的朋友可以參考下2023-11-11SpringBoot如何讀取xml配置bean(@ImportResource)
這篇文章主要介紹了SpringBoot如何讀取xml配置bean(@ImportResource),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java中ShardingSphere 數(shù)據(jù)分片的實(shí)現(xiàn)
其實(shí)很多人對(duì)分庫分表多少都有點(diǎn)恐懼,我們今天用ShardingSphere 給大家演示數(shù)據(jù)分片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09SpringBoot開發(fā)詳解之Controller接收參數(shù)及參數(shù)校驗(yàn)
數(shù)據(jù)校驗(yàn)是為了使系統(tǒng)更完整,數(shù)據(jù)更精確,同時(shí)也有利于維護(hù)數(shù)據(jù)的安全性,下面這篇文章主要給大家介紹了關(guān)于SpringBoot開發(fā)詳解之Controller接收參數(shù)及參數(shù)校驗(yàn)的相關(guān)資料,需要的朋友可以參考下2022-03-03java多線程數(shù)據(jù)分頁處理實(shí)例講解
在本篇內(nèi)容里小編給大家分享了一篇關(guān)于java多線程數(shù)據(jù)分頁處理實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01如何使用Idea中的 Deployment 實(shí)現(xiàn)打包自動(dòng)部署
這篇文章主要介紹了使用Idea中的 Deployment 實(shí)現(xiàn)打包自動(dòng)部署,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08Java基礎(chǔ)之Bean的創(chuàng)建、定位和使用
這篇文章主要介紹了Java基礎(chǔ)之Bean的創(chuàng)建、定位和使用,文中有非常詳細(xì)的圖文示例及代碼,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05IntelliJ IDEA2023中運(yùn)行Spring Boot找不到VM options進(jìn)
這篇文章主要介紹了IntelliJ IDEA2023中運(yùn)行Spring Boot找不到VM options進(jìn)行端口的修改的問題解決,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11Java中的動(dòng)態(tài)代理實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Java中的動(dòng)態(tài)代理實(shí)現(xiàn)代碼實(shí)例,jdk動(dòng)態(tài)代理本質(zhì)上是使用被代理對(duì)象的類加載器,通過被代理類實(shí)現(xiàn)的接口在運(yùn)行時(shí)動(dòng)態(tài)構(gòu)造出代理類來增強(qiáng)原始類的功能的方法,需要的朋友可以參考下2023-12-12