MyBatis中實現多表查詢


如果查詢的數據量大,推薦使用N+1次查詢。數據量少使用聯合查詢。。。

一、

1、Mybatis是實現多表查詢方式

  1.1  業務裝配:對兩個表編寫單表查詢語句,在業務(Service)把查詢的兩表結果合並

  1.2  使用Auto Mapping 特性,在實現兩表聯合查詢時通過別名完成映射

  1.3  使用MyBatis<resultMap>屬性進行實現

2、多表查詢時,類中包含另一個類的對象的分類

  2.1 單個對象

  2.2 集合對象

二、resultMap屬性

  1、<resultMap>標簽寫在mapper.xml中,由程序員控制SQL查詢結果與實體類的映射關系。

    1.2 默認MyBatis使用Auto Mapping特性

  2、使用<resultMap> 標簽時,<select>標簽不寫resultType屬性,而是使用resultMap屬性 引用<resultMap>標簽

  3、使用resultMap實現單表映射關系

    3.1 數據庫設計

  

     3.2 實體類設計

    

      3.3 xxxmapper.xml代碼

 1  <mapper namespace="com.bjsxt.mapper.TeacherMapper">
 2      <resultMap type="Teacher" id="mymap">
 3          <!-- 主鍵使用id標簽配置映射關系-->
 4          <id column="id" property="id1"/>
 5          <!-- 其他列使用result標簽配置映射關系 -->
 6          <result  column="name" property="name1"/>
 7      </resultMap>
 8      <select id="selall"  resultMap="mymap">
 9          select * from teacher
10      </select>
11  </mapper>

 三使用resultMap 實現關聯單個對象(N+1方式)

    4.1  N+1 查詢方式,先查詢出某個表的全部信息,根據這個表的信息查詢另一個表的信息

    4.2 與業務裝配的區別:

      4.2.1 之前在service里面寫代碼,現在由mybatis完成裝配

    4.3 實現步驟:

      4.3.1 在Student 實現類中包含了一個Teacher 對象

      

      4.3.2 在TeacherMapper 中提供一個查詢

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

      4.3.3 在StudentMapper中

          4.3.3.1 <association> 裝配一個對象時使用(包含在student中的對象)

          4.3.3.2 property 關聯對象

          4.3.3.3 select 通過哪個查詢查詢出這個對象的信息

          4.3.3.4 column 把當前表的哪個列的值作為參數傳遞給另一個查詢

          4.3.3.5  大前提使用N+1方式時,如果列名和屬性名相同可以不配置,使用Auto mapping特性,但是mybatis默認只會給列專配一次。。

  <mapper namespace="com.bjsxt.mapper.StudentMapper">
      <resultMap type="student" id="stumap">
          <id property="id" column="id"/>
          <result property="name" column="name"/>
          <result property="age" column="age"/>
          <result property="tid" column="tid"/>
          <!-- 如果關聯一個對象 -->
          <association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid"></association>
      </resultMap>
          <select id="selall"  resultMap="stumap">
              select * from student
          </select>
  </mapper>

          4.3.3.6 可以把代碼簡化

  <mapper namespace="com.bjsxt.mapper.StudentMapper">
      <resultMap type="student" id="stumap">
          <result property="tid" column="tid"/>
          <!-- 如果關聯一個對象 -->
          <association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid"></association>
          
      </resultMap>
          <select id="selall"  resultMap="stumap">
              select * from student
          </select>
  </mapper>

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

   1、在Teacher 中添加List<Student>

    

   2、 在StudentMapper.xml中添加通過tid查詢

  <mapper namespace="com.bjsxt.mapper.StudentMapper">
      <select id="selByTid" parameterType="int" resultType="student">
          select * from student where tid=#{0}
      </select> 
  </mapper>

   3、 在TeacherMapper.xml 中添加查詢全部

      3.1、<collection/> 當屬性是集合類型時使用的標簽

 <mapper namespace="com.bjsxt.mapper.TeacherMapper">
         <resultMap type="teacher" id="mymap">
              <id column="id" property="id"/>
              <result column="name" property="name"/>
              <collection property="list" ofType="student" select="com.bjsxt.mapper.StudentMapper.selByTid" column="id"></collection>
          </resultMap>
          <select id="selAll" resultMap="mymap">
            select * from teacher              
          </select>
 </mapper>

 

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

    1、在teacherMapper.xml中添加

      1.1 mybatis可以通過主鍵判斷對象是否被加載過

        不需要擔心創建重復 teacher

 <mapper namespace="com.bjsxt.mapper.TeacherMapper">        
 
          <resultMap type="teacher" id="mymap">
              <id property="id" column="tid"/>
              <result property="name" column="tname"/>
              <collection property="list" ofType="student" >
                  <id property="id" column="sid"/>
                  <result property="name" column="sname"/>
                  <result property="age" column="age"/>
                  <result property="tid" column="tid"/>
              </collection>
          </resultMap>
          
          <select id="selAll" resultMap="mymap">
            select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t left join student s on t.id=s.tid              
          </select>
 </mapper>

 

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

  6.1 只能使用多表聯合查詢方式

  6.2 要求:查詢出的列和屬性名相同

  6.3  實現方式

    6.3.1  . 在SQL是關鍵字符,兩側添加返單引號

  <mapper namespace="com.bjsxt.mapper.StudentMapper">
      <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 s.tid=t.id;
      </select>
  </mapper>

    

    注意點:::使用Auto Mapping 特性查詢集合不好用 !!!!

 

七、 使用注解查詢

  1、注解:為了簡化配置文件

  2、Mybatis 的注解簡化 xxxmapper.xml文件

    2.1 如果涉及動態SQL 依然使用 xxxmapper.xml 

   3、xxxmapper.xml 和注解可以共存

   4、使用注解時 Mybatis.xml 中<mappers> 使用

    4.1 <package/>

    4.2 <mapper class="  "/>

    5、實現查詢

	@Select("select * from teacher")
	List<Teacher> selAll();

   6、實現插入

	@Insert("insert into teacher values(default,#{name})")
	int inTeacher(Teacher teacher);

   7、實現修改

	@Update("update teacher set name=#{name} where id=#{id}")
	int upTeacher(Teacher teacher);

   8、實現刪除

	@Delete("delete from teacher where id=#{0}")
	int delTeacher(int id);

   9、使用注解實現 <resultMap> 功能

    9.1、以 N+1 舉例

    9.2、在StudentMapper 接口添加查詢

@Select ("select * from student where tid=#{0}")
 List<Student> selByTid(int tid);

       9.3、在TeacherMapper 接口添加

      9.3.1 @Results() 相當於 <resultMap>

      9.3.2 @Result() 相當於<id/> 或 <result/>

        9.3.2.1 @Result(id=true)  相當於<id/>

      9.3.3 @Many() 相當於<collection/>

      9.3.4 @One() 相當於<association/>

@Results(value={ 
    @Result(id=true,property="id",column="id"),
    @Result(property="name",column="name"),
        
    @Result(property="list",column="id",many=@Many(select="com.bjsxt.mapper.StudentMapper.selByTid" ))
    })
@Select("select * from teacher")
List<Teacher> selTeacher();

 


免責聲明!

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



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