您好,登錄后才能下訂單哦!
resultMap即自定義結(jié)果集映射規(guī)則,之前我們使用的resultType是MyBatis為我們提供的默認(rèn)映射規(guī)則,使用方法如下:
<mapper namespace="com.zgz.MyBatis.dao.DeptMapper">
<select id="getDeptById" resultType="com.zgz.MyBatis.bean.Dept">
select id, dept_name deptName from tbl_dept where id = #{id}
</select>
</mapper>
那么resultMap的作用是什么呢?之前我們?cè)谔幚頂?shù)據(jù)庫(kù)中字段與javabean中的屬性不一致的問題時(shí),采用的方法是起別名或者開啟mybatis的自動(dòng)駝峰映射,現(xiàn)在可以使用resultMap來做,另外可以使用解決級(jí)聯(lián)查詢的問題
我們現(xiàn)在新建一張員工部門表,要求是在我們查詢員工的同時(shí)查詢出員工對(duì)應(yīng)的部門?
首先給出對(duì)應(yīng)的JavaBean:
public class Dept {
private Integer id;
private String deptName;
//get,set,tostring()。。。
}
寫上相應(yīng)的mapper:
<mapper namespace="com.zgz.MyBatis.dao.DeptMapper">
<select id="getDeptById" resultType="com.zgz.MyBatis.bean.Dept">
select id, dept_name deptName from tbl_dept where id = #{id}
</select>
</mapper>
在SQL映射文件中進(jìn)行配置:
<?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.zgz.MyBatis.dao.EmployeeMapperPlus">
<!--
測(cè)試resultMap(自定義某個(gè)javabean的封裝規(guī)則)
type:自定義規(guī)則的java類型
id:方便引用,唯一
-->
<resultMap type="com.zgz.MyBatis.bean.Employee" id="MyEmp">
<!--
id:指定主鍵的封裝規(guī)則,底層會(huì)有優(yōu)化
column:指定哪一列
property:指定對(duì)應(yīng)的javabean屬性
-->
<id column="id" property="id"/>
<!-- result是指定普通列的封裝規(guī)則 -->
<result column="last_name" property="lastName"/>
<!-- 其他的不指定會(huì)默認(rèn)封裝,最好指定一下 -->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap>
<!-- resultMap:自定義結(jié)果集映射規(guī)則 -->
<select id="getEmployeeByGender" resultMap="MyEmp">
select * from tbl_employee where gender = #{gender}
</select>
<!-- resultMap級(jí)聯(lián)查詢 -->
<!-- 第一種方法:使用級(jí)聯(lián)屬性封裝結(jié)果集-->
<resultMap type="com.zgz.MyBatis.bean.Employee" id="MySecond">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.deptName"/>
</resultMap>
<!-- 第二種方法: 使用association定義單個(gè)對(duì)象的封裝 -->
<resultMap type="com.zgz.MyBatis.bean.Employee" id="MyThird">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!--
association:可以指定聯(lián)合的javabean對(duì)象
property:指定哪個(gè)屬性是聯(lián)合的對(duì)象
javaType:指定這個(gè)屬性的類型(不能省略)
-->
<association property="dept" javaType="com.zgz.MyBatis.bean.Dept">
<id column="did" property="id"/>
<result column="dept_name" property="deptName"/>
</association>
</resultMap>
<!-- 第三種方法:分步查詢 -->
<resultMap type="com.zgz.MyBatis.bean.Employee" id="MyFourth">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!--
使用 association 進(jìn)行分步查詢:
1. 先按照員工id查詢員工信息
2. 根據(jù)查詢到的員工信息的d_id值去查出部門信息
3. 部門設(shè)置到員工里面
association 定義關(guān)聯(lián)對(duì)象的封裝規(guī)則
select:表明當(dāng)前屬性是調(diào)用select指定的方法查出的結(jié)果
column:指定將哪一列的值傳給這個(gè)方法
流程:(理解)
使用select制定的方法,傳入column指定的這列參數(shù)的值查出對(duì)象,并封裝給property指定的屬性
-->
<association property="dept"
select="com.zgz.MyBatis.dao.DeptMapper.getDeptById" column="d_id">
</association>
</resultMap>
<!-- 場(chǎng)景一:查詢員工的同時(shí)查詢出員工對(duì)應(yīng)的部門 -->
<select id="getEmpAndDept" resultMap="MyThird">
SELECT e.id id, e.last_name last_name,e.email email, e.gender gender, e.d_id d_id, d.id did, d.dept_name dept_name
FROM tbl_employee e, tbl_dept d
WHERE e.d_id = d.id AND e.id = #{id}
</select>
<!-- 分步查詢 -->
<select id="getEmpByIdStep" resultMap="MyFourth">
select * from tbl_employee where id = #{id}
</select>
<!--
分步查詢可以使用延遲加載(按需加載)(懶加載):
Employee ===》 Dept
要查詢Dept,我們每次查詢Employee時(shí)都將一起查詢出來
為了達(dá)到部門信息在我們使用的時(shí)候在查詢出來的要求,我們可以在分步查詢的基礎(chǔ)上加兩個(gè)配置(在mybatis中的主配置文件中)
-->
</mapper>
在主配置文件中配置:
<configuration>
<!-- configuration里的標(biāo)簽必須按順序?qū)?-->
<settings>
<!-- 顯示我們需要更改配置的值,即使是默認(rèn)的建議寫上,防止版本更新帶來的問題 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<environments default="development">
<environment id="test">
<transactionManager type=""></transactionManager>
<dataSource type=""></dataSource>
</environment>
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 將我們寫好的sql映射文件(testEmployeeMapper.xml)注冊(cè)到全局配置文件(mybatis-config.xml)中 -->
<mappers>
<mapper resource="testEmployeeMapperPlus.xml" />
<mapper resource="testDeptMapper.xml" />
</mappers>
</configuration>
1》千萬不要忘記將我們寫好的sql映射文件注冊(cè)到全局配置文件中,注意是所有的sql映射文件
<mappers>
<mapper resource="testEmployeeMapperPlus.xml" />
<mapper resource="testDeptMapper.xml" />
</mappers>
2》configuration里的標(biāo)簽必須按順序?qū)?/strong>,標(biāo)簽是有順序的
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。