日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis使用resultMap自定义映射规则与关联映射

發(fā)布時間:2024/9/30 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis使用resultMap自定义映射规则与关联映射 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、寫在前面

在MyBatis 的全局配置文件中我們可以通過在settings標(biāo)簽中設(shè)置
<setting name="mapUnderscoreToCamelCase" value="true"/>來開啟駝峰命名法,實現(xiàn)數(shù)據(jù)庫中的字段名與JavaBean 中屬性的關(guān)系映射。

在數(shù)據(jù)庫中一般使用單詞來定義列名,多個列名之間用’ _ ‘分隔開,比如department_name,在JavaBean 中一般使用駝峰命名來定義屬性,所以會定義為departmentName。在默認(rèn)不開啟駝峰命名法的情況下,它們之間是不能夠?qū)崿F(xiàn)關(guān)系映射的,在開啟對駝峰命名法的支持后,MyBatis 會對數(shù)據(jù)庫中的字段進行駝峰命名轉(zhuǎn)換。

但是有的時候,開啟駝峰命名法也不能完成映射,比如數(shù)據(jù)庫字段dept_name對應(yīng)JavaBean 中的departmentName,這時候就需要我們自定義resultMap關(guān)系映射。

二、resultMap自定義映射規(guī)則

使用resultMap實現(xiàn)數(shù)據(jù)庫中的dept_name字段與JavaBean 中的departmentName的關(guān)系映射。

mapper 接口:

// 通過id 查詢部門信息,返回的是一個Department 對象Department getDeptById(Integer id);

SQL 映射文件:

<!--先通過resultMap 定義映射規(guī)則id:當(dāng)前命名空間中的一個唯一標(biāo)識,用于標(biāo)識一個resultMap type:類的完全限定名, 或者一個類型別名,也就是返回的對應(yīng)JavaBean 類型--><resultMap id="BaseDeptResultMap" type="com.jas.mybatis.bean.Department"><!-- <id>:與下面<result>標(biāo)簽的區(qū)別是可以用于提升性能,所以這里用<id>標(biāo)簽來定義主鍵的映射關(guān)系column:數(shù)據(jù)庫中對應(yīng)的字段名property:對應(yīng)JavaBean 的屬性名因為可以實現(xiàn)自動映射,所以<id column="id" property="id"/> 在這里也可以省略如果開啟了駝峰命名法,對應(yīng)的映射轉(zhuǎn)換也可以省略--><id column="id" property="id"/><result column="dept_name" property="departmentName"/></resultMap><select id="getDeptById" resultMap="BaseDeptResultMap">SELECT * FROM t_dept WHERE id = #{id}</select>

三、resultMap實現(xiàn)關(guān)聯(lián)映射

有這樣一個需求,在根據(jù)id 查詢員工信息的時候,把員工所在的部門信息也查出來。

Employee 員工類:

package com.jas.mybatis.bean;public class Employee {private Integer id;private String username;private Character gender;private String email;//在Employee 類中定義一個Department 對象,用來封裝員工的部門信息private Department department;// 省略get、set 與toString 方法 }

Department部門類:

package com.jas.mybatis.bean;public class Department {private Integer id;private String departmentName; }

mapper 接口:

// 根據(jù)id 查出員工的信息并把對應(yīng)的部門信息也查詢出來Employee getEmpAndDeptById(Integer id);

SQL 映射文件:

方式一:

<resultMap type="com.jas.mybatis.bean.Employee" id="BaseEmployeeResultMap"><id column="id" property="id"/><result column="username" property="username"/><result column="gender" property="gender"/><result column="email" property="email"/><!-- 通過下面兩個列名獲得數(shù)據(jù)將封裝成Department 對象直接通過屬性名.屬性名的方式指定映射的關(guān)系--><result column="d_id" property="department.id"/><result column="dept_name" property="department.departmentName"/></resultMap><select id="getEmpAndDeptById" resultMap="BaseEmployeeResultMap">SELECT e.id, e.username, e.gender, e.email, e.d_id, d.dept_nameFROM t_employee e LEFT JOIN t_dept d ON (e.d_id = d.id) WHERE e.id = #{id}</select>

方式二(在resultMap使用association標(biāo)簽):

<resultMap type="com.jas.mybatis.bean.Employee" id="BaseEmployeeResultMap"><id column="id" property="id"/><result column="username" property="username"/><result column="gender" property="gender"/><result column="email" property="email"/><!--使用association 標(biāo)簽實現(xiàn)關(guān)聯(lián)查詢property:在Employee 類中對應(yīng)的department 屬性javaType:關(guān)聯(lián)的類的全路徑名或者其對應(yīng)的別名--><association property="department" javaType="com.jas.mybatis.bean.Department"><!--property:直接就是Department 類中的屬性--><id column="d_id" property="id"/><result column="dept_name" property="departmentName"/></association></resultMap><select id="getEmpAndDeptById" resultMap="BaseEmployeeResultMap">SELECT e.id, e.username, e.gender, e.email, e.d_id, d.dept_nameFROM t_employee e LEFT JOIN t_dept d ON (e.d_id = d.id) WHERE e.id = #{id}</select>

查看結(jié)果:

四、總結(jié)

這篇博文主要介紹了使用resultMap 實現(xiàn)數(shù)據(jù)庫字段與JavaBean 屬性之間高級映射的知識點,并實現(xiàn)了一個關(guān)聯(lián)映射。resultMap的作用還有很多,比如分布查詢與延遲加載等,都是在resultMap的基礎(chǔ)上完成的。如果有興趣,可以關(guān)注后續(xù)的博文。希望這篇博文能夠為你提供幫助。

總結(jié)

以上是生活随笔為你收集整理的MyBatis使用resultMap自定义映射规则与关联映射的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。