zoukankan      html  css  js  c++  java
  • 浅析Mybatis的resultMap的用法以及关联结果集映射

    一、resultType

      resultType 可以把查询结果封装到 pojo 类型中,但必须 pojo 类的属性名和查询到的数据库表的字段名一致。也就是说一般数据库字段名喜欢用下划线类型:user_id、而实体类通常是用驼峰 userId。如果这样子那么就对不上了,怎么办呢?

      如果 sql 查询到的字段与 pojo 的属性名不一致,则需要使用 resultMap 将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中。

    二、resultMap用法

      resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。

      resultMap包含的元素:

    <!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
    <resultMap id="唯一的标识" type="映射的pojo对象">
      <id column="表的主键字段,或者可以为查询语句中的别名字段" 
      jdbcType
    ="字段类型"
      property
    ="映射pojo对象的主键属性" /> <result column="表的一个字段(可以为任意表的一个字段)"
      jdbcType
    ="字段类型"
      property
    ="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/> <association property="pojo的一个对象属性" javaType="pojo关联的pojo对象"> <id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/> <result column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/> </association> <!-- 集合中的property须为oftype定义的pojo对象的属性--> <collection property="pojo的集合属性" ofType="集合中的pojo对象"> <id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" /> <result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" /> </collection> </resultMap>

      如果collection标签是使用嵌套查询,格式如下:

    <collection column="传递给嵌套查询语句的字段参数"
    property="pojo对象中集合属性"
    ofType
    ="集合属性中的pojo对象"
    select
    ="嵌套的查询语句" > </collection>

      注意:<collection>标签中的column:要传递给select查询语句的参数,如果传递多个参数,格式为column= ” {参数名1=表字段1,参数名2=表字段2} ;

      resultMap 可以实现将查询结果映射为复杂类型的 pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。

    1、先在Mapper文件中,配置基本的sql语句

        <!-- 查询所有的订单数据 -->
        <!-- resultMap:填入配置的resultMap标签的id值 -->
        <select id="queryOrderAll" resultMap="orderResultMap">
            SELECT id, user_id,
            number,
            createtime, note FROM `order`
        </select>

    2、配置resultMap标签,映射不同的字段和属性名

      此处的 id 就对应上面的 resultMap,此处的 type 就对应你设置的实体类的 pojo

       <!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo -->
        <!-- id:设置ResultMap的id -->
        <resultMap type="order" id="orderResultMap">
            <!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id -->
            <!-- property:主键在pojo中的属性名 -->
            <!-- column:主键在数据库中的列名 -->
            <id property="id" column="id" />
    
            <!-- 定义普通属性 -->
            <result property="userId" column="user_id" />
            <result property="number" column="number" />
            <result property="createTime" column="createtime" />
            <result property="note" column="note" />
        </resultMap>

      结果就可以封装到 pojo 中

    三、使用resultMap进行关联查询

    1、一对一查询

      一对一数据模型:订单用户

      一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。

      如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以下多个订单。

    (1)改造pojo类

      在订单类中添加User属性,User属性是一个引用类型,用于存储关联查询的用户信息,因为关联关系是一对一,所以只需要添加单个属性即可

    (2)配置Mapper.xml配置文件

      OrderMapper.xml — 先使用id和result属性,映射order类的结果集,然后在使用association映射关联对象User的结果集

    <resultMap type="order" id="orderUserResultMap">
        <id property="id" column="id" />
        <result property="userId" column="user_id" />
        <result property="number" column="number" />
        <result property="createTime" column="createtime" />
        <result property="note" column="note" />
    
        <!-- association :配置一对一属性 -->
        <!-- property:order里面的User属性名 -->
            <!-- javaType:属性类型 -->
        <association property="user" javaType="user">
            <!-- id:声明主键,表示user_id是关联查询对象的唯一标识-->
            <id property="id" column="user_id" />
            <result property="username" column="username" />
            <result property="address" column="address" />
        </association>
    
    </resultMap>
    <!-- 一对一关联,查询订单,订单内部包含用户属性 -->
    <select id="queryOrderUserResultMap" resultMap="orderUserResultMap">
        SELECT
        o.id,
        o.user_id,
        o.number,
        o.createtime,
        o.note,
        u.username,
        u.address
        FROM
        `order` o
        LEFT JOIN `user` u ON o.user_id = u.id
    </select>
    @Test
    public void testQueryOrderUserResultMap() {
        // mybatis和spring整合,整合之后,交给spring管理
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        // 创建Mapper接口的动态代理对象,整合之后,交给spring管理
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    
        // 使用userMapper执行根据条件查询用户,结果封装到Order类中
        List<Order> list = userMapper.queryOrderUserResultMap();
        for (Order o : list) {
            System.out.println(o);
        }
        // mybatis和spring整合,整合之后,交给spring管理
        sqlSession.close();
    }

    2、一对多查询

      查询所有用户信息及相关订单。

    (1)修改pojo类,在pojo类添加订单集合属性

    (2)修改UserMapper.xml配置文件

      先使用id和result配置映射User类的结果,然后使用一对多关系的collection标签配置Order结果

    <resultMap type="user" id="userOrderResultMap">
        <id property="id" column="id" />
        <result property="username" column="username" />
        <result property="birthday" column="birthday" />
        <result property="sex" column="sex" />
        <result property="address" column="address" />
    
        <!-- 配置一对多的关系
            property:填写pojo类中集合类类属性的名称
            javaType:填写集合类型的名称 
        -->
        <collection property="orders" javaType="list" ofType="order">
            <!-- 配置主键,是关联Order的唯一标识 -->
            <id property="id" column="oid" />
            <result property="number" column="number" />
            <result property="createtime" column="createtime" />
            <result property="note" column="note" />
        </collection>
    </resultMap>
    
    <!-- 一对多关联,查询订单同时查询该用户下的订单 -->
    <select id="queryUserOrder" resultMap="userOrderResultMap">
        SELECT
        u.id,
        u.username,
        u.birthday,
        u.sex,
        u.address,
        o.id oid,
        o.number,
        o.createtime,
        o.note
        FROM
        `user` u
        LEFT JOIN `order` o ON u.id = o.user_id
    </select>

    四、实例介绍resultMap的用法

    1、创建商品pojo对象

    public class TShopSku  {
    
        private Long id;
        private String skuName;
        private Long categoryId;
      ......
    }

      对应的resultMap

    <resultMap id="BaseResultMap" type="com.meikai.shop.entity.TShopSku">
        <id column="ID" jdbcType="BIGINT" property="id" />
        <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
        <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
    </resultMap> 

    2、商品pojo类添加属性集合:

      一个商品会有一些属性,现在需要将查询出的商品属性添加到商品对象中,首先需要在原商品pojo类的基础上中添加属性的集合:

      // 属性集合
        private List<TShopAttribute> attributes;
       
        public List<TShopAttribute> getAttributes() {
            return attributes;
        }
         public void setAttributes(List<TShopAttribute> attributes) {
            this.attributes = attributes;
         }

      将Collection标签添加到resultMap中,这里有两种方式:

    1、嵌套结果:对应的resultMap:

    <resultMap id="BasePlusResultMap" type="com.meikai.shop.entity.TShopSku">
        <id column="ID" jdbcType="BIGINT" property="id" />
        <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
        <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
        <collection property="attributes" ofType="com.meikai.shop.entity.TShopAttribute" > 
            <id column="AttributeID" jdbcType="BIGINT" property="id" />
            <result column="attribute_NAME" jdbcType="VARCHAR" property="attributeName" />
        </collection>
    </resultMap>
    
    // 查询语句
    <select id="getById"  resultMap="basePlusResultMap">
        select s.ID,s.SKU_NAME,s.CATEGORY_ID,a.ID,a.ATTRIBUTE_NAME
        from t_shop_sku s,t_shop_attribute a 
        where s.ID =a.SKU_ID and s.ID = #{id,jdbcType =BIGINT};
    </select>

    2、关联的嵌套查询(在collection中添加select属性):

      商品结果集映射resultMap:

    <resultMap id="BasePlusResultMap" type="com.meikai.shop.entity.TShopSku">
        <id column="ID" jdbcType="BIGINT" property="id" />
        <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
        <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
        <collection column="{skuId=ID}" property="attributes" ofType="com.meikai.shop.entity.TShopAttribute" select="getAttribute" > 
        </collection>
    </resultMap>

      collection的select会执行下面的查询属性语句:

    <select id="getAttribute"  resultMap="AttributeResultMap">
        select a.ID,s.ATTRIBUTE_NAME
        from t_shop_attribute a
        where  a.ID = #{skuId,jdbcType =BIGINT};
    </select>

      属性结果集映射:

    <resultMap id="AttributeResultMap" type="com.meikai.shop.entity.TShopAttribute">
        <id column="ID" jdbcType="BIGINT" property="id" />
        <result column="ATTRIBUTE_NAME" jdbcType="VARCHAR" property="attributeName" />
    </resultMap>

      BasePlusResultMap包含了属性查询语句的Collection,所以通过下面的查询商品语句就可获得商品以及其包含的属性集合:

    <select id="getById"  resultMap="BasePlusResultMap">
        select s.ID,s.SKU_NAME,s.CATEGORY_ID
        from t_shop_sku s
        where  s.ID = #{id,jdbcType =BIGINT};
    </select>
  • 相关阅读:
    Mysql一套完整练习题
    Ubuntu kylin优麒麟下配置Hive环境
    win10解决无法远程桌面连接问题(参考)
    OSI七层模型的工作协议划分
    20201116-每日一题
    20201115-福州大学-助教-周总结-第9次
    2020年11月学习记录
    20201104-福州大学-助教-周总结-第7次
    2020年10月学习记录
    20201025-福州大学-助教-周总结-第6次
  • 原文地址:https://www.cnblogs.com/goloving/p/14766241.html
Copyright © 2011-2022 走看看