Mybatis實現多表聯查


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實體類:


   
   
   
           
  1. public class Student {
  2. private int id;
  3. private String name;
  4. private int age;
  5. private int tid;
  6. private Teacher teacher;

TeacherMapper.xml文件中提供一個查詢老師對象的sql:


   
   
   
           
  1. <mapper namespace="com.test.mapper.TeacherMapper">
  2. <select id="selById" parameterType="int" resultType="Teacher">
  3. select * from teacher where id =#{0}
  4. </select>
  5. </mapper>

在StudentMapper.xml中寫上查詢學生信息:


   
   
   
           
  1. <mapper namespace="com.test.mapper.StudentMapper">
  2. <resultMap type="Student" id="stuMap">
  3. <id column="id" property="id"/>
  4. <result column="name" property="name"/>
  5. <result column="age" property="age"/>
  6. <result column="tid" property="tid"/>
  7. <association property="teacher" select="com.test.mapper.TeacherMapper.selById"
  8. column= "tid"> </association>
  9. </resultMap>
  10. <select id="selAll" resultMap="stuMap" >
  11. select * from student
  12. </select>
  13. </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表示返回值類型,其它的還一次對應匹配即可。具體代碼實例:


   
   
   
           
  1. <!-- 在mapper中實現聯合查詢 -->
  2. <resultMap type="Student" id="stuMap1">
  3. <id column="sid" property="id"/>
  4. <result column="sname" property="name"/>
  5. <result column="sage" property="age"/>
  6. <result column="tid" property="tid"/>
  7. <association property="teacher" javaType="teacher">
  8. <id column="tid" property="id"/>
  9. <result column="tname" property="name"/>
  10. </association>
  11. </resultMap>
  12. <select id="selAll1" resultMap="stuMap1">
  13. select s.id sid,s.name sname,s.age sage,t.id tid,t.name tname
  14. from student s left join teacher t on s.tid = t.id;
  15. </select>

4、使用<resultMap>查詢關聯集合對象(N+1)

  • 在實體類Teacher中加上含有多個Student的屬性list,代碼:

   
   
   
           
  1. public class Teacher {
  2. private int id;
  3. private String name;
  4. private List<Student> list;
  • 在StudentMapper.xml中添加通過tid查詢學生的sql語句,代碼:

   
   
   
           
  1. <mapper namespace="com.test.mapper.StudentMapper">
  2. <select id="selByTid" resultType="student" parameterType="int" >
  3. select * from student where tid = #{0}
  4. </select>
  5. </mapper>
  • 在TeacherMapper.xml中添加查詢全部,然后通過<resultMap>來裝配其它的,代碼:

   
   
   
           
  1. <mapper namespace="com.test.mapper.TeacherMapper">
  2. <resultMap type="Teacher" id="teacMap">
  3. <id column="id" property="id"/>
  4. <result column="name" property="name"/>
  5. <collection property="list" select="com.test.mapper.StudentMapper.selByTid" column="id"/>
  6. </resultMap>
  7. <select id="selAll" resultMap="teacMap">
  8. select * from teacher
  9. </select>
  10. </mapper>

還是和查詢單個對象一樣這里<resultMap>中使用<collection>來匹配集合,其中property還是類中的屬性名,select是要執行的sql語句,column為要傳遞的參數字段。

5、使用<resultMap>實現加載集合數據(聯合查詢方式)

只需要寫一條SQL,在TeacherMapper.xml中完成,對於老師的屬性在<resultMap>中直接用<id>或<result>進行裝配(將字段別名與屬性匹配),對於Student對象集合用<collection>標簽來映射,其中 property還是代表在類中該對象集合屬性的名稱   另外要設置ofType表示返回集合的泛型,其它的還一次對應匹配即可。具體代碼實例:


   
   
   
           
  1. <!-- 使用聯合查詢 -->
  2. <resultMap type="Teacher" id="teacMap1">
  3. <id column="tid" property="id"/>
  4. <result column="tname" property="name"/>
  5. <collection property="list" ofType="Student">
  6. <id column="sid" property="id"/>
  7. <result column="sname" property="name"/>
  8. <result column="age" property="age"/>
  9. <result column="tid1" property="tid"/>
  10. </collection>
  11. </resultMap>
  12. <select id="selAll1" resultMap="teacMap1">
  13. select t.id tid, t.name tname, s.id sid, s.name sname,age ,s.tid tid1
  14. from teacher t left join student s on t.id = s.tid
  15. </select>

三、使用Auto Mapping結合別名實現多表查詢

  • 只能使用多表聯合查詢方式.
  • 要求:查詢出的列別和屬性名相同.
  • 實現方式,在 SQL 是關鍵字符,兩側添加反單引號
  • 只能適用於單個對象

代碼實例:


   
   
   
           
  1. <select id="selAll" resultType="student">
  2. select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid
  3. from student s LEFT JOIN teacher t on t.id=s.tid
  4. </select>

四、Mybatis注解

  • 注解:為了簡化配置文件.
  •  Mybatis 的注解簡化 mapper.xml 文件.
  • 如果涉及動態 SQL 依然使用 mapper.xml
  • mapper.xml 和注解可以共存.
  • 使用注解時 mybatis.xml 中<mappers>使用
  • <package/>或 <mapper class=””/>

一般實例:

  • 實現查詢:

   
   
   
           
  1. @Select("select * from teacher")
  2. List<Teacher> selAll ();
  • 實現新增

   
   
   
           
  1. @Insert("insert into teacher values(default,#{name})")
  2. int insTeacher (Teacher teacher);

將主鍵帶回來的方式:設置@Options注解並配置(useGeneratedKeys=true,keyProperty="id")


   
   
   
           
  1. @Insert("insert into log values(default,#{accOut},#{accIn},#{money})")
  2. @Options(useGeneratedKeys=true,keyProperty="id")
  3. int insLog (Log log);
  • 實現刪除(主要看基本類型參數的取法)

   
   
   
           
  1. @Delete("delete from teacher where id=#{0}")
  2. int delById (int id);

 

https://blog.csdn.net/qq_36747735/article/details/90044639


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM