文章目錄
- 解決一對多資料問題
- 關聯的嵌套 Select 查詢
- 關聯的嵌套結果映射
- 解決一對多問題
- 集合的嵌套Select查詢
- 集合的嵌套結果映射
- 小結
解決一對多資料問題
- 在一對多的資料表結構中,比如一個老師教育著很多學生, 對于學生而言, 這就是一個一對多的問題
- 這個主要解決的就是如果我們的POJO物件的屬性中,有一個是參考型別,不是基本型別或者String, 那么簡單的sql陳述句肯定是解決不了的
- 學生型別
public class Student {
private int id;
private String name;
private Teacher teacher;
}
- 老師型別
public class Teacher {
private int id;
private String name;
}
- 比如我們現在有一個需求, 要查找資料庫中所有的學生物件, 該如何做?
關聯的嵌套 Select 查詢
<select id="selectStudent" resultMap="studentTeacher">
select * from student;
</select>
<resultMap id="studentTeacher" type="student">
<association property="teacher" javaType="teacher" column="tid" select="selectOneTeacher"/>
</resultMap>
<select id="selectOneTeacher" resultType="teacher">
select * from teacher where id=#{tid};
</select>
- 帶你解讀一下上面的代碼, 首先id對應的是我們dao層介面的方法 ,回傳值型別我們初始設定成resultmap, 首先查到的是資料庫學生表的所有資訊
- 然后到resultMap標簽里, 這里的id要對應上述我們設定的resultMap的名子, 這個這個回傳型別就是真實的回傳型別, 所以是student, 其中student的id和name已經自動映射好了, 不需要我們再次建立映射關系, 最重要的就是teacher物件的映射, 我們使用association標簽,property映射到student型別的teacher參考名, Javatype表示回傳的是一個teacher型別, column表示所查到的學生資訊中的tid要去這個老師物件建立連接, select表示呼叫下面的select陳述句
- 在最下面的select陳述句中,回傳型別是teacher型別與上面進行對應, 在sql陳述句中通過上面的column設定的列的值進行對應建立連接
- 最后就是回傳student這個物件

關聯的嵌套結果映射
<select id="selectStudent2" resultMap="studentTeacher2">
select s.id sid, s.name sname, t.name tname from student s, teacher t where s.tid=t.id;
</select>
<resultMap id="studentTeacher2" type="student">
<result column="sid" property="id"/>
<result column="sname" property="name"/>
<association property="teacher" javaType="teacher">
<result column="tname" property="name"/>
</association>
</resultMap>
- 使用這個代碼易懂, 但是sql陳述句會變得復雜
- 一開始我們便使用很長的sql陳述句建立老師和學生的連接將所有資訊查詢出來
- 所以在resultmap中我們只需要將這些資訊進行結果映射即可, 但是要注意參考型別的結果映射還得在association元素內使用, 還需指定回傳的型別
- 注意我沒有設定老師id的這個屬性的映射關系, 所以查到顯示的肯定是這個資料型別的默認值

解決一對多問題
- 還是最開始的那個例子, 一個老師教育很多學生, 對于老師而言這就是一對多的關系, 但是在這里我想復雜一下就是一個班級既有老師,又有學生, 也就是既有參考型別, 又有集合型別, 這種該如何查詢
- 好, 我現在有一個需求 ,我指定老師的id 要得到這個教室的所有資訊, 包括老師的資訊和所有學生的資訊
- 教室型別
public class ClassRoom {
private Teacher teacher;
private List<Student> students;
}
集合的嵌套Select查詢
<select id="getClassRoom" parameterType="_int" resultMap="classRoomMap">
select * from teacher where id=#{tid};
</select>
<resultMap id="classRoomMap" type="classRoom">
<association property="teacher" javaType="teacher">
<result column="id" property="id"/>
<result column="name" property="name"/>
</association>
<collection property="students" javaType="ArrayList" ofType="student" column="id" select="students"/>
</resultMap>
<select id="students" resultType="student">
select id, name from student where tid=#{id};
</select>
- 首先最開始id物件方法名是getClassRoom, 通過老師的id查找, 所以會有一個引數parameterType是int型的, 初步回傳型別是resultmap, 在初步的sql陳述句中,我通過老師的id找到這個老師的所有資訊
- 在resultMap的對應名稱classRoomMap里, 我指定回傳型別當然是一個classroom型別, 接下來就是使用association元素對老師這個參考進行屬性建立映射關系
- 接下來使用collection這個元素對學生這個集合進行構造, 首先明確使用collection這個元素, 你首先是要與classroom的屬性名建立映射練習, 然后必須指定javatype和oftype, javatype是java中的什么集合型別, oftype是指定這個集合型別中的泛型引數的什么型別的,這個很重要, 然后column指定通過什么資料與后面的select建立聯系
- 在最后的select中, 我們通過上述column傳來的teacher的id與所有學生的替代建立聯系, 查到對應要求的資訊, 回傳student型別, 值此再回傳classRoom這個型別的查詢結果

集合的嵌套結果映射
- 這里需要改一個東西, 就是如果要滿足我們的需求, 使用上面的classroom型別如果使用集合的嵌套結果映射在SQL層面是實作不了的, 所以得把classroom的資料型別改一下
- 新的教室型別
public class ClassRoom {
private int tid;
private String tname;
private List<Student> students;
}
- ok
<select id="getClassRoom2" parameterType="_int" resultMap="classRoomMap2">
select s.id sid, s.name sname, t.id tid, t.name tname from student s, teacher t where s.tid=t.id and t.id=#{tid};
</select>
<resultMap id="classRoomMap2" type="classRoom">
<result column="tid" property="tid"/>
<result column="tname" property="tname"/>
<collection property="students" javaType="ArrayList" ofType="student">
<result column="sid" property="id"/>
<result column="sname" property="name"/>
</collection>
</resultMap>
- 只要使用的是結果集的映射, 就是一開始就將所有資訊都查出來, 然后在一個resultMap中進行屬性名與資料庫表中列的欄位名建立連接
- 但是還是要注意這里的sql陳述句是比上面的復雜的, 而且要留意只要使用collection元素, 就必須設定其中的Javatype和oftype, 這點很重要

小結
- 關聯association, 用于屬性中有物件時使用, 用于一對多的環境
- 集合collection, 用于屬性中有集合型別的時候使用,用于多對一的環境
- javatype, 用來指定java中指定物體屬性中的型別, 就好比這個指定的是List型別
- oftype, 用來指定映射到java指定物體屬性集合中泛型的具體型別,這個指定的就是List中Studente型別
- 如果需要使用參考型別, association, 只需注明屬性名稱, 注明javatype, 然后在里面進行設定值即可
- 如果是集合型別,使用collection,注明屬性名稱, 注明javatype型別, 和集合型別里面的泛型型別oftype, 然后在里面result值即可.
注意點:
- 保證sql的可讀性, 盡量保證通俗易懂
- 注意多對一和一對多中屬性名和欄位名的匹配問題, 多用于日志
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/197008.html
標籤:java
