Mybatis實現多表聯查
一、Mybatis實現多表聯查詢
1、Mybatis實現多表聯查詢方式
- 業務裝配對兩個表寫單獨的sql語句,在業務(service)把查詢結果進行聯合。
- 使用Auto Mapping特性,在實現兩個表聯合查詢時通過別名完成自動映射。
-
使用Mybatis的<resultMap>標簽進行實現
2、多表查詢時類中包含另一個對象的分類
- 單個對象
- 集合對象
二、resultMap標簽
1、<resultMap>標簽單表中的映射
寫在<select>標簽中,不用謝resultType屬性,可以單獨的在<resultMap>中將數據庫字段與java屬性不匹配進行映射。
2、使用<resultMap>標簽在兩個表中關聯單個對象(N+1方式)
- N+1查詢的方式,先查詢出一個表的全部信息,根據這個表的信息查詢另一個表的信息。
- 實現步驟在Student類中包含一個Teacher對象
- 在StudentMapper.xml文件中寫上查詢學生的sql,然后通過<resultMap>來完成Teacher對象的查詢
具體代碼:
java實體類:
-
public
class
Student {
-
private
int id;
-
private String name;
-
private
int age;
-
private
int tid;
-
private Teacher teacher;
TeacherMapper.xml文件中提供一個查詢老師對象的sql:
-
<mapper namespace="com.test.mapper.TeacherMapper">
-
<select id="selById" parameterType="int" resultType="Teacher">
-
select * from teacher where id =#{0}
-
</select>
-
</mapper>
在StudentMapper.xml中寫上查詢學生信息:
-
<mapper namespace="com.test.mapper.StudentMapper">
-
<resultMap type="Student" id="stuMap">
-
<id column="id" property="id"/>
-
<result column="name" property="name"/>
-
<result column="age" property="age"/>
-
<result column="tid" property="tid"/>
-
<association property="teacher" select="com.test.mapper.TeacherMapper.selById"
-
column=
"tid">
</association>
-
</resultMap>
-
<select id="selAll" resultMap="stuMap" >
-
select * from student
-
</select>
-
</mapper>
幾個屬性標簽說明:
- <id> 主鍵
- <result> 其它字段
- <association>裝配對象時使用,其中 property 表示類中的屬性名 select 表示要執行的sql語句(寫完整的sql語句) column 要傳過去的字段參數
解釋:(N+1指的是每個學生類中有一個老師對象,因此一條sql查詢出所有的學生,然后在查每個學生中的老師,即N+1).看日志信息:
補充說明:當其它字段一樣時,可以在<resultMap>中不用寫,但對於要傳遞的參數字段mybatia只裝配一次因此要寫
3、使用<resultMap>實現關聯單個對象(聯合查詢方式)
只需要寫一條SQL,在StudentMapper.xml中完成,對於學生屬性直接用<id>或<result>進行裝配(將字段別名與屬性匹配),對於Teacher對象直接用<association>標簽來映射,其中 property還是代表在類中該對象屬性的名稱 另外要設置javaType表示返回值類型,其它的還一次對應匹配即可。具體代碼實例:
-
<!-- 在mapper中實現聯合查詢 -->
-
<resultMap type="Student" id="stuMap1">
-
<id column="sid" property="id"/>
-
<result column="sname" property="name"/>
-
<result column="sage" property="age"/>
-
<result column="tid" property="tid"/>
-
<association property="teacher" javaType="teacher">
-
<id column="tid" property="id"/>
-
<result column="tname" property="name"/>
-
</association>
-
</resultMap>
-
-
<select id="selAll1" resultMap="stuMap1">
-
select s.id sid,s.name sname,s.age sage,t.id tid,t.name tname
-
from student s left join teacher t on s.tid = t.id;
-
</select>
4、使用<resultMap>查詢關聯集合對象(N+1)
- 在實體類Teacher中加上含有多個Student的屬性list,代碼:
-
public
class
Teacher {
-
private
int id;
-
private String name;
-
private List<Student> list;
- 在StudentMapper.xml中添加通過tid查詢學生的sql語句,代碼:
-
<mapper namespace="com.test.mapper.StudentMapper">
-
<select id="selByTid" resultType="student" parameterType="int" >
-
select * from student where tid = #{0}
-
</select>
-
</mapper>
- 在TeacherMapper.xml中添加查詢全部,然后通過<resultMap>來裝配其它的,代碼:
-
<mapper namespace="com.test.mapper.TeacherMapper">
-
<resultMap type="Teacher" id="teacMap">
-
<id column="id" property="id"/>
-
<result column="name" property="name"/>
-
<collection property="list" select="com.test.mapper.StudentMapper.selByTid" column="id"/>
-
</resultMap>
-
<select id="selAll" resultMap="teacMap">
-
select * from teacher
-
</select>
-
</mapper>
還是和查詢單個對象一樣這里<resultMap>中使用<collection>來匹配集合,其中property還是類中的屬性名,select是要執行的sql語句,column為要傳遞的參數字段。
5、使用<resultMap>實現加載集合數據(聯合查詢方式)
只需要寫一條SQL,在TeacherMapper.xml中完成,對於老師的屬性在<resultMap>中直接用<id>或<result>進行裝配(將字段別名與屬性匹配),對於Student對象集合用<collection>標簽來映射,其中 property還是代表在類中該對象集合屬性的名稱 另外要設置ofType表示返回集合的泛型,其它的還一次對應匹配即可。具體代碼實例:
-
<!-- 使用聯合查詢 -->
-
<resultMap type="Teacher" id="teacMap1">
-
<id column="tid" property="id"/>
-
<result column="tname" property="name"/>
-
<collection property="list" ofType="Student">
-
<id column="sid" property="id"/>
-
<result column="sname" property="name"/>
-
<result column="age" property="age"/>
-
<result column="tid1" property="tid"/>
-
</collection>
-
</resultMap>
-
<select id="selAll1" resultMap="teacMap1">
-
select t.id tid, t.name tname, s.id sid, s.name sname,age ,s.tid tid1
-
from teacher t left join student s on t.id = s.tid
-
</select>
三、使用Auto Mapping結合別名實現多表查詢
- 只能使用多表聯合查詢方式.
- 要求:查詢出的列別和屬性名相同.
- 實現方式,在 SQL 是關鍵字符,兩側添加反單引號
- 只能適用於單個對象
代碼實例:
-
<select id="selAll" resultType="student">
-
select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid
-
from student s LEFT JOIN teacher t on t.id=s.tid
-
</select>
四、Mybatis注解
- 注解:為了簡化配置文件.
- Mybatis 的注解簡化 mapper.xml 文件.
- 如果涉及動態 SQL 依然使用 mapper.xml
- mapper.xml 和注解可以共存.
- 使用注解時 mybatis.xml 中<mappers>使用
- <package/>或 <mapper class=””/>
一般實例:
- 實現查詢:
-
@Select("select * from teacher")
-
List<Teacher>
selAll
();
- 實現新增
-
@Insert("insert into teacher values(default,#{name})")
-
int
insTeacher
(Teacher teacher);
將主鍵帶回來的方式:設置@Options注解並配置(useGeneratedKeys=true,keyProperty="id")
-
@Insert("insert into log values(default,#{accOut},#{accIn},#{money})")
-
@Options(useGeneratedKeys=true,keyProperty="id")
-
int
insLog
(Log log);
- 實現刪除(主要看基本類型參數的取法)
-
@Delete("delete from teacher where id=#{0}")
-
int
delById
(int id);