mybatis高级映射

resultType &resultMap :

1、resultType :使用resultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
比如在常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用 resultType 将每一条记录映射到 pojo 中,在前端页面遍历 list ( list 中是 pojo )即可。
2、resultMap :使用 association 和 collection 完成一对一和一对多以及多对多的高级映射(对结果有特殊的映射要求)。
association :
作用:将关联查询信息映射到一个pojo对象中。如为了方便查询关联信息可以使用 association 将关联订单信息映射为用户对象的 pojo 属性中,比如:查询订单及关联用户信息。
使用 resultType 无法将查询结果映射到 pojo 对象的 pojo 属性中,根据对结果集查询遍历的需要选择使用 resultType 还是 resultMap 。
collection :作用:将关联查询信息映射到一个list集合中。如为了方便查询遍历关联信息可以使用 collection 将关联信息映射到 list 集合中,比如:查询用户权限范围模块及模块下的菜单,可使用 collection 将模块映射到模块 list 中,将菜单列表映射到模块对象的菜单 list 属性中,这样的作的目的也是方便对查询结果集进行遍历查询。如果使用 resultType 无法将查询结果映射到 list 集合中。

resultType 实现一对一查询

resultType 实现一对一查询相对比较简单,只需定义pojo类即可实现,那么接下来实现都是基于mybatis代理方式来实现resultType 和resultMap,即接口实现等不写了,用代理的方式就很好去用这些映射文件去实现相应的功能 ,。
mapper.xml映射文件配置如:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="Dao.UserMapper">
   <!--在resultType中定义映射类即可-->
   <select id="findUserById" parameterType="int"     resultType="mybatisjdbc.User">
  select * from timetable where id=#{value}
   </select>
  </mapper>

而对于resultMap ,需要在映射文件中配置resultMap 要映射的类属性和对应数据库的列名

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  <!-- 
    注意:使用mapper代理开发namespace有特殊意义,其值为mapper接    口的地址
       -->
  <mapper namespace="returnMap.ClassMapper">

    <!-- 

 定义returnMap,将结果映射到returnMap.classMap中
    type:要映射到的po类的路径,id为任意唯一标识
    -->
    <resultMap type="returnMap.classMap"       id="returnMapClassMapper">
    <!-- 
 id标签为表的唯一标识,当一个表中有多个唯一标识时可以使用多个id
      column:为数据库表中的列名,property为要映射到po类的属性
     -->
     <id column="class_id" property="class_id"/>
     <result column="className" property="class_name"/>
     <result column="classCode" property="class_code"/>
     <result column="grade" property="grade"/>
</resultMap>

    <!-- resultMap的值为对应定义的resultMap的id -->
    <select id="findUserByreturnMap"         resultMap="returnMapClassMapper">
      select * from class,timetable where     class.classCode=timetable.classCode and class.grade=2014
   </select> -->

    </mapper>

resultType 和 resultMap小结:
1、resultType :使用 resultType 实现较为简单,如果 pojo 中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
2、如果没有查询结果的特殊要求建议使用 resultType 。
3、resultMap :需要单独定义 resultMap ,实现有点麻烦,如果对查询结果有特殊的要求,使用 resultMap 可以完成将关联查询映射 pojo 的属性中。
4、resultMap 可以实现延迟加载, resultType 无法实现延迟加载。

一对多查询

在接下来的映射文件的配置中,resultType 的使用还是基本一样的,而resultMap 就很容易体现两者的区别了。
使用 resultType 实现:将订单明细映射到一个类的类属性中中,需要自己处理,使用双重循环遍历,去掉重复记录,将订单明细放在类属性中,实现起来相对麻烦,不建议使用。
而mybatis 使用 resultMap 的 collection 对关联查询的多条记录映射到一个 list 集合属性中。

mapper.xml映射文件的配置

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      <!-- 
      注意:使用mapper代理开发namespace有特殊意义,其值为mapper    接口的地址
       -->
    <mapper namespace="returnMap.ClassMapper">

    <!-- 
       班级关联课表
       定义returnMap,将结果映射到returnMap.classMap中
         type:要映射到的po类的路径,id为任意唯一标识
          -->
        <resultMap type="returnMap.classMap"             id="returnMapClassMapper">
    <!-- 
      配置主表class
      id标签为表的唯一标识,当一个表中有多个唯一标识时可以使用多个id
      column:为表中的列名,property为要映射到po类的属性
     -->
     <id column="class_id" property="class_id"/>
     <result column="className" property="class_name"/>
     <result column="classCode" property="class_code"/>
     <result column="grade" property="grade"/>
  <!-- 
     配置关联查询表timetable
     association为映射po类中timetable类对象
     javaType:属性对象的类名
   -->
  <association property="timeble" javaType="returnMap.timetable">
       <id column="id" property="id"/>
       <result column="classCode" property="classCode"/>
       <result column="semester" property="semester"/>
       <result column="section" property="section"/>
       <result column="one" property="one"/>
       <result column="two" property="two"/>
       <result column="three" property="three"/>
       <result column="four" property="four"/>
       <result column="five" property="five"/>
         <result column="" property="six"/>
           <result column="seven" property="seven"/>
        
     </association>
    </resultMap>
   <!-- 将第三个关联表的信息存储在list中 使用继承,继承前面查询相同的class和timetable表 ,extends的值为继承的resuilt的id-->
<resultMap type="returnMap.classMap" id="findUserByreturnListMap" extends="returnMapClassMapper">
       <!-- 查询class表 -->
       <!-- 查询timetable表 -->
       
       <!--一个信息对应多条记录时,要使用collection把多条记录映射在集合中
        collection 对关联查询到多条记录映射到集合对象中
        property:要映射到的po类的List集合属性名
        ofType:映射的集合所在的po类的路径
        -->
     <collection property="listmap" ofType="returnMap.classMap">
         
      <!-- 
      第三个关联表映射成集合对象
      id标签为表的唯一标识,当一个表中有多个唯一标识时可以使用多个id
      column:为表中的列名,property为要映射到po类的属性
     -->
     <id column="listclass_code" property="listclass_code"/>
     <result column="listgrade" property="listgrade"/>
  
     </collection>

</resultMap>
   <!-- 将查询结果映射到list集合中,resultMap的值对应定义的resultMap的id -->
   <select id="findUserByreturnListMap"     resultMap="findUserByreturnListMap">
  select * from class,timetable where     class.classCode=timetable.classCode and class.grade=2014
   </select>

</mapper>

多对多查询

这个mapper.xml是实现多对多查询的结果,以用户查询商品明细为例子。
映射思路:将用户信息映射到user类中。
在user类中添加订单列表属性List<Orders> orders,将用户创建的订单映射到orders
在Orders中添加订单明细列表属性List<Detail>detial,将订单的明细映射到detial
在OrderDetail中添加Items属性,将订单明细所对应的商品映射到Items
总结来说:在User类中创建List<orders>集合属性,在orders类中创建List<Detail>detials集合属性,
最后在detial类中定义类属性iterm,最后实现映射文件( 即嵌套再嵌套的关系)

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="List_to_list.List_to_useMap">
  <!-- 
 用户商品关联查询
 定义returnMap,将结果映射到List_to_list.List_User中
  type:要映射到的po类的路径,id为任意唯一标识
      -->
<resultMap type="List_to_list.List_User"  id="findUserByreturnListMap">
    <!-- 
      配置主表User
      id标签为表的唯一标识,当一个表中有多个唯一标识时可以使用多个id
      column:为表中的列名,property为要映射到po类的属性
     -->
     <id column="id" property="id"/>
     <result column="usename" property="usename"/>
     <result column="address" property="address"/>
   
   <!--一个信息对应多条记录时,要使用collection把多条记录映射在集合中
        collection 对关联查询到多条记录映射到集合对象中,collection为属性为集合类型的定义标签
        property:要映射到的po类的List集合属性名
        ofType:映射的集合所在的po类的路径
        -->
        <collection property="List_orders" ofType="List_to_list.List_orders">
             <id column="order_id" property="order_id"/>
              <result column="order_usename" property="order_usename"/>
              <result column="order_show" property="order_show"/>
            
                 <!-- 在集合属性中的类的集合属性,collection需要嵌套 -->
                   <collection property="List_detail" ofType="List_to_list.List_detail">
                              <id column="detail_id" property="detail_id"/>
                               <result column="detail_usename" property="detail_usename"/>
                                <result column="detail_show" property="detail_show"/>
                   
                              <!-- 在集合属性的类中定义的类属性,association需要嵌套在里面,association为属性为类类型的定义标签 -->
                                <!-- 
                                  association为映射po类中属性类对象
                                    javaType:属性对象的类名
                                    -->
                               <association property="List_iterm" javaType="List_to_list.List_iterm">
                                        <id column="iterm_id" property="iterm_id"/>
                                           <result column="iterm_usename" property="iterm_usename"/>
                                            <result column="iterm_show" property="iterm_show"/>
                               
                               </association>
                     
                   </collection>
        
         </collection>
   
</resultMap>

   <!-- 将查询结果映射到list集合中,resultMap的值对应定义的resultMap的id -->
      <select id="findUserByreturnListMap" resultMap="findUserByreturnListMap">
      select * from class,timetable where class.classCode=timetable.classCode and class.grade=2014
   </select>

  </mapper>

总结:mybatis高级映射主要是对数据库的表的字段进行映射到pojo类或者集合中,不同要求所使用resultType和resultMap 也不同。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容