您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Mybatis聯(lián)合查詢怎么實(shí)現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Mybatis聯(lián)合查詢怎么實(shí)現(xiàn)”吧!
數(shù)據(jù)庫表結(jié)構(gòu)
department
employee
要求一
現(xiàn)在的要求是輸入 id 把 employee 表的對應(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....... }
這個(gè)要求很明顯就要用到兩個(gè)表,想要把部門信息封裝到Employee
對象的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>
測試
@Test public void test1() { SqlSession sqlSession = MyTest.getSqlSession(); EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); System.out.println(mapper.getEmployee(1)); }
結(jié)果
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>
測試
@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 表對應(yīng)的部門信息查詢出來,并且查詢該部門下的所有員工信息
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....... }
方法
<!-- 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>
測試
@Test public void test2() { SqlSession sqlSession = MyTest.getSqlSession(); DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class); System.out.println(mapper.getDepartment(1)); }
結(jié)果
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>
測試
@Test public void test2() { SqlSession sqlSession = MyTest.getSqlSession(); DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class); System.out.println(mapper.getDepartment3(1)); }
結(jié)果
到此,相信大家對“Mybatis聯(lián)合查詢怎么實(shí)現(xiàn)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。