Mybatis3.1-[tp_34-35]-_對映檔案_select_resultMap關聯查詢_collection定義關聯集合封裝規則_collection分步查詢_延遲載入
阿新 • • 發佈:2018-12-04
筆記要點
出錯分析與總結
工程組織
1.定義介面
interface DepartmentMapper
package com.dao; import com.bean.Department; public interface DepartmentMapper { public Department getDeptByIdStep(Integer id); //使用Collection,執行分步查詢 }
interface EmployeeMapperPlus
package com.dao; import com.bean.*;import java.util.List; public interface EmployeeMapperPlus { public List<Employee> getEmpsByDeptId(Integer deptId); //按照部門的id,返回employee一個列表 }
2.定義XML對映檔案
DepartmentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dao.DepartmentMapper"> <!--publicView CodeDepartment getDeptById(Integer id);--> <select id="getDeptById" resultType="com.bean.Department"> select id,dept_name departmentName from tbl_dept where id=#{id} </select> <!--==========================================================================================--> <!-- publicclass Department { private Integer id; private String departmentName; private List<Employee> emps; } JavaBean中: did dept_name || eid last_name email gender --> <!--public Department getDeptByIdPlus(Integer id);--> <!--進行collection的聯合查詢/ 分步查詢和延遲查詢 --> <resultMap id="MyDept" type="com.bean.Department"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> <!--collection 用於定義關聯集合型別的屬性的封裝規則! ofType用於指定集合中的型別;--> <collection property="emps" ofType="com.bean.Employee"> <id column="eid" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection> </resultMap> <select id="getDeptByIdPlus" resultMap="MyDept"> SELECT d.id did,d.dept_name dept_name,e.id eid,e.last_name last_name,e.email email,gender FROM tbl_dept d LEFT JOIN tbl_employee e ON d.id=e.d_id WHERE d.id=#{id}; </select> <!--========================================--> <!--Department中有屬性: private List<Employee> emps; public Department getDeptByIdStep(Integer id); //執行Collection 的分步查詢--> <resultMap id="MyDeptStep" type="com.bean.Department"> <id column="id" property="id"/> <result column="detp_name" property="departmentName"/> <collection property="emps" select="com.dao.EmployeeMapperPlus.getEmpsByDeptId" column="id"> </collection> </resultMap> <select id="getDeptByIdStep" resultMap="MyDeptStep"> select id,dept_name departmentName from tbl_dept where id=#{id} </select> </mapper>
EmployeeMapperPlus.xml c新增內容
<select id="getEmpsByDeptId" resultType="com.bean.Employee"> select * from tbl_employee where d_id = #{DeptId} </select>
3.編寫測試程式碼
@Test public void test07() throws Exception{ SqlSession openSession = getSqlSessionFactory().openSession(); try{ DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class); // System.out.println("---tp_34---多表關聯查詢,使用collection 定義關聯集合封裝規則-----"); // Department department = mapper.getDeptByIdPlus(1); // List<Employee> emps = department.getEmps(); // for(Employee e:emps) // System.out.println(e); System.out.println("---tp_35---多表關聯查詢,使用collection分步查詢&延遲載入--"); Department dept = mapper.getDeptByIdStep(1); System.out.println(dept.getDepartmentName()); //只有一行時,進行按需載入 // System.out.println(dept); //當要載入全部的時候,就不會延遲載入了 openSession.commit();//預設是不自動提交資料的,需要我們自己手動提交 }finally { openSession.close(); } }
測試結果 (全部呼叫時的結果)
---tp_35---多表關聯查詢,使用collection分步查詢&延遲載入-- DEBUG 12-04 12:01:58,977 ==> Preparing: select id,dept_name departmentName from tbl_dept where id=? (BaseJdbcLogger.java:145) DEBUG 12-04 12:01:58,997 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145) DEBUG 12-04 12:01:59,069 <== Total: 1 (BaseJdbcLogger.java:145) 開發部 DEBUG 12-04 12:01:59,069 ==> Preparing: select * from tbl_employee where d_id = ? (BaseJdbcLogger.java:145) DEBUG 12-04 12:01:59,070 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145) DEBUG 12-04 12:01:59,072 <== Total: 2 (BaseJdbcLogger.java:145) Department{id=1, departmentName='開發部'}