查詢Emp的同時,查出emp對應的部門Department
方法1:聯合查詢,使用級聯屬性封裝結果集
<!-- 聯合查詢,使用級聯屬性封裝結果集 type:要自定義規則的javaBean類型 id:唯一標識,方便引用 column:指的是數據庫表中的列名 property:列名對應的javaBean中的屬性名稱 -->
<resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifemp"> <id column="id" property="id" /> <result column="last_name" property="last_name" /> <result column="gender" property="gender" /> <result column="did_id" property="dept.id" /> <result column="dept_name" property="dept.departmentName" /> </resultMap> <!--public Emp getEmpAndDept(Integer id); --> <select id="getEmpAndDept" resultMap="myDifEmp2"> SELECT e.id AS id,e.last_name AS last_name,e.gender AS gender,e.email AS email,d.id AS d_id,d.`dept_name` AS dept_name FROM emp e,dept d WHERE e.`d_id`=d.id and e.id=#{id} </select> |
方法2:association來實現多表查詢
<!-- association來實現多表查詢 association標簽的property屬性來指定javaBean中的哪個屬性是我們自定義的(即:哪個屬性是聯合的對象), javaType是用來指定這個屬性的類型(不能省略) association標簽中繼續使用id和result標簽來封裝相應的封裝規則
--> <resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifEmp2"> <id column="id" property="id" /> <result column="last_name" property="last_name" /> <result column="gender" property="gender" /> <result column="email" property="email"/> <association property="dept" javaType="cn.bdqn.mybatis.been.Department"> <id column="did_id" property="id"/> <result column="dept_name" property="departmentName"/> </association> </resultMap> |
方法3:association進行分步式多表查詢
01.先按照員工id查詢員工信息
02.根據查出員工信息中的d_id值去部門表查詢部門信息
03.把查出的部門設置到員工中;
創建Department的javaBean,編寫對應的映射文件,
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"> <!--namespace與接口進行綁定 --> <mapper namespace="cn.bdqn.mybatis.dao.DeptMapper"> <!--public Department getDepartmentById(Integer id); --> <select id= "getDepartmentById" resultType="cn.bdqn.mybatis.been.Department"> <!--我們數據表中部門名稱的字段的名字是dept_name 而我們javaBean中對應的 名字是departmentName,不一致,所以我們要將查詢的列一一寫出,並且將名稱不相同的,起別名 --> select id,dept_name as departmentName from dept where id=#{id} </select> </mapper> |
編寫emp對應的映射文件
<?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"> <!--namespace:名稱空間 --> <mapper namespace="cn.bdqn.mybatis.dao.EmpMapperPlus">
<!-- association來實現多表查詢 association標簽的property屬性來指定javaBean中的哪個屬性是我們自定義的(即:哪個屬性是聯合的對象), javaType是用來指定這個屬性的類型(不能省略) association標簽中繼續使用id和result標簽來封裝相應的封裝規則 --> <resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifEmp2"> <id column="id" property="id" /> <result column="last_name" property="last_name" /> <result column="gender" property="gender" /> <result column="email" property="email" /> <association property="dept" javaType="cn.bdqn.mybatis.been.Department"> <id column="did_id" property="id" /> <result column="dept_name" property="departmentName" /> </association> </resultMap> <!-- 使用association進行分步式查詢 1.先按照員工id查詢員工信息 2.根據查出員工信息中的d_id值去部門表查詢部門信息 3.把查出的部門設置到員工中; --> <resultMap type="cn.bdqn.mybatis.been.Emp" id="myEmpBy"> <id column="id" property="id" /> <result column="last_name" property="last_name" /> <result column="gender" property="gender" /> <result column="email" property="email" /> <!--association定義關聯對象的封裝規則 select:select的屬性值是部門的映射文件的名稱空間+對應的方法名稱 表明當前屬性是調用select指定的方法查出來的結果 column:指定將哪一列的值傳給這個方法 流程:使用select指定的方法(傳入column指定的這列參數的值)查出對象,並封裝給property指定的屬性 --> <association property="dept" select="cn.bdqn.mybatis.dao.DeptMapper.getDepartmentById" column="d_id"> </association> </resultMap>
<!-- public Emp getEmpByIdStep(Integer id); --> <select id="getEmpByIdStep" resultMap="myEmpBy"> select * from emp where id=#{id} </select> </mapper>
|
注意:這兩個映射文件寫好后,必須都要在全局配置文件mybatis-config.xml中進行注冊
<mappers> <mapper resource="EmpMapperPlus.xml" /> <mapper resource="DepartmentMapper.xml"/> </mappers> |
分步查詢,可以使用延遲加載(懶加載/按需加載)
emp==>Dept
使用聯合查詢和其他復雜查詢時,emp的信息和Dept的信息都會同時查出,這樣很浪費資源
而我們希望部門信息在我們使用的時候再去查詢,這樣就很節省數據庫了
只需要在分段查詢的基礎之上加上兩個配置:
只需要在全局配置文件中開啟懶加載模式
<setting name="lazyLoadingEnabled" value="true"/>
和按需加載
<setting name="aggressiveLazyLoading" value="false"/>
便可以實現