8、mybatis中的sql映射文件详解(3)
對于初學者,如何進行mybatis的學習呢?我總結了幾點,會慢慢的更新出來。首先大家需要了解mybatis是什么、用mybatis來做什么、為什么要用mybatis、有什么優缺點;當知道了為什么的時候就開始了解如何用的問題,如何使用mybatis、有幾種使用方式、各種方式的優缺點,在這個階段也會學習mybatis涉及到的一些標簽的用法;當知道了基礎用法之后,就開始接觸一些高級的用法,例如動態sql的使用、mybatis的緩存使用等;至此,在實戰項目中使用mybatis進行開發已經沒有問題了。
接下來就開始深入的研究一下mybatis這個持久層的框架,在純技術的方面進行研究,提高自己的能力。首先,大家需要了解一下mybatis的整體技術架構和工作原理;接下來,就開始了解一下mybatis各大核心組件的具體功能及其工作原理。至此,算是對mybatis的原理簡單的了解一下了,由于博主的能力有限,因此對于mybatis的框架技術研究也就到這里算結束了。
最后會了解一些其他的東西,例如:mybatis的逆向工程使用、如何開發一個mybatis插件,在這里會介紹一下mybatis的分頁實現等。
至此,mybatis也算是入門了,出去就可以和別人說,你稍微了解mybatis框架,對其也多少有一點自己的理解和看法了。
目錄
1、級聯屬性的映射
2、association指定關聯對象映射
3、association 分步查詢(嵌套查詢)
4、collection 指定關聯集合對象映射
5、collection 分步查詢(嵌套查詢)
6、discriminator 鑒別器的使用
上一篇?介紹了輸入映射和輸出映射,結束于自定義映射resultMap的更多用法,這一篇主要針對resultMap 的一些高級用法進行敘述。
1、級聯屬性的映射
這里直接通過例子來看用法,例如:每一個用戶對應一個部門,查詢用戶信息時需要級聯查詢出部門的信息,此時就可以使用resultMap 自定義映射關系,具體如下:
<resultMap id="user2" type="com.app.mapper.User"><id column="id" property="id"/><result column="name" property="name"/><result column="dname" property="department.name"/> </resultMap><select id="selectUser2" resultMap="user2">select a.id, a.name, b.tname from oa_user a, oa_department b where a.id = #{id} and a.department = b.id </select>此時,User 實體類的屬性定義如下:
private Long id; // id private String name; // 用戶名 private Department department; // 關聯的部門2、association指定關聯對象映射
對于1中的sql 映射,可以使用?association 標簽進行指定關聯對象的映射,其具體使用如下:
<select id="selectUser3" resultMap="user3">select a.id, a.name, b.did, b.dname from oa_user a, oa_department b where a.id = #{id} and a.department = b.did </select><resultMap id="user3" type="com.app.mapper.User"><id column="id" property="id"/><result column="name" property="name"/><association property="department" javaType="com.app.mapper.Department"><id column="did" property="did"/><result column="dname" property="name"/></association> </resultMap>3、association 分步查詢(嵌套查詢)
有的時候sql 語句過于復雜,無法一條語句獲取到最終的結果,需要多條語句,或者為了實現延遲加載,針對于 2 中例子,可以通過嵌套查詢的方式實現,具體如下:
<select id="getUserById" resultMap="user"></select><resultMap type="com.app.mapper.User" id="user"><id column="id" property="id"/><result column="name" property="name"/><!-- 嵌套一個查詢,N+1查詢 --><association property="department"select="com.app.mapper.UserMapper.getDepartmentByDid"column="did"/></resultMap><select id="getDepartmentByDid" resultType="com.app.mapper.Department">select did, name from department where did = #{did} </select>如果想要實現延時加載,需要在mybatis全局配置文件中設置,這個在?全局配置文件?介紹一篇中有介紹:
<setting name="lazyLoadingEnabled" value="true"></setting>4、collection 指定關聯集合對象映射
有的時候關聯的數據是一個集合,例如,一個學生有多個老師,此時就需要使用collection 標簽進行結果對象映射了,具體如下:
<select id="getStudentBySid" resultMap="student1">select a.sid, a.sname, b.tid, b.tname from student a, teacher b where b.sid = a.sid </select><resultMap id="student1" type="com.app.mapper.Student"><id column="sid" property="sid"/><result column="sname" property="sname"/><collection property="teachers" ofType="com.app.mapper.Teacher"><id column="tid" property="tid"/><result column="tname" property="tname"/></collection> </resultMap>?對應的java實體如下:
private Long sid; private String sname; private List<Teacher> teachers;5、collection 分步查詢(嵌套查詢)
類似于 3 ,如果想要實現延時加載的話,可以通過使用 嵌套查詢(N+1查詢)的方式實現,具體如下:
<select id="getStudentBySid" resultMap="student1">select a.sid, a.sname, b.tid, b.tname from student a, teacher b where a.tid = b.tid </select><resultMap id="student1" type="com.app.mapper.Student"><id column="sid" property="sid"/><result column="sname" property="sname"/><collection property="teachers" select="com.app.mapper.UserMapper.selectTeacherBySid" column="sid" fetchType="lazy"/> </resultMap> <select id="getStudentBySid" resultType="com.app.mapper.UserMapper.Teacher">select tid, tname from teacher where sid = #{sid} </select>6、discriminator 鑒別器的使用
鑒別器的作用類似于if ... else ... 選擇的作用,在進行分步查詢時,可以對前一步查詢出來的自定字段值進行判斷來確定下一步的執行行為,例如查詢用戶字段,根據用戶的性別來進行不同聯系方式的映射,具體使用如下:
<select id="getUser" resultMap="user3">select uid, uname, sex, phone, email from user where uid = #{uid} </select><resultMap id="user3" type="com.app.mapper.User"><id column="uid" property="uid"/><result column="uname" property="uname"/><discriminator javaType="int"><!-- 1 為男,若為1, 則將用戶的手機映射到聯系方式; 2 為女,若為2, 則將用戶的郵箱映射到聯系方式 --><case value="1"><result column="phone" property="link_info"/></case><case value="2"><result column="email" property="link_info"/></case></discriminator> </resultMap>這一篇這介紹到這里,下一篇開始進行動態sql開發的介紹。
總結
以上是生活随笔為你收集整理的8、mybatis中的sql映射文件详解(3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 交换机怎么连接无线路由器怎么设置无线路由
- 下一篇: 无线路由器怎样连接你好我想问问路由器是怎