美文网首页
Mybatis XML映射之结果映射

Mybatis XML映射之结果映射

作者: crossyf | 来源:发表于2019-10-28 13:57 被阅读0次

前言:最近有空重新回顾了一下Mybatis的用法,结合自己项目中的实际应用,发现自己并没有有效的使用Mybatis的各种功能,特别是在结果映射的使用上面,于是结合这次项目重新优化了一下项目。

一、相关概念

Mybatis结果映射主要是resultMap的配置,resultMap主要有以下几个节点属性

<resultMap>
      <constructor>
          <idArg/>
          <arg/>
      </constructor>
      <id/>
      <result/>
      <association property=""/>
      <collection property""/>
      <discriminator javaType="">
          <case value=""></case>
      </discriminator>
</resultMap>

其中idresult是我们常用的两个节点属性,其他几个在目前的项目中,几乎没有用到,于是一个一个来。

二、constructor

constructor主要是用来配置构造方法,默认的情况之下,mybatis会调用实体类的无参构造方法,创建一个实例,然后再给相关的属性赋值,如果我们手动给实体类设置了有参的构造方法,又没有创建无参的构造方法,那么如果不进行特殊的配置,resultMap在生成实体类的时候就会进行报错,因为他没有找到无参的构造方法。
具体例子,配置如下


<resultMap id="userResultMap" type="org.sang.bean.User">
        <constructor>
            <idArg column="id" javaType="long"/>
            <arg column="username" javaType="string"/>
            <arg column="password" javaType="string"/>
            <arg column="address" javaType="string"/>
        </constructor>
    </resultMap>

其中 idArg标记出作为ID的结果,可以帮助提高整体的性能。

三、association

association可以映射出复杂的实体类,如果返回的实体类中有另外一个实体类的属性,那么可以使用association进行配置。下面举个例子:
例如,我们在查询用户信息的时候需要把用户的角色也查出来,这时候我现在的做法是先查出来用户的数据,然后再根据用户的role_id或者其他条件查出相关Role,然后再把角色的值赋值到Userrole属性中。
如果利用association就可以一步到位。

<resultMap id="userMap" type="User">
    <id property="id" column="id"></id>
    <result property="username" column="username"></result>
    <result property="password" column="password"></result>
    <result property="address" column="address"></result>
    <result property="email" column="email"></result>
    
    <association property="role" javaType="Role">
        <id property="id" column="role_id"></id>
        <result property="name" column="role_name"></result>
    </association>
</resultMap>

注意
其实这里有一个更快捷的方法,主要是利用 Mybatis解析返回值的相关底层原理,可以自动创建role对象,并且把值写进到属性中。

select u.id, u.username, u.password,u.address,u.email, r.id as role.id, r.name as role.name
from user u left join user_roles ur on ur.user_id = u.id
left join role r on r.id = ur.role_id

三、collection

collection的使用场景实际上在项目中非常的常见,比如后台管理系统中,用户的角色往往会对应挂接的菜单列表,一个订单往往会对应着商品明细列表,这时候查询结果都是一对多的关系,之前我在项目中的写法都是分开查询,然后在把结果组装起来,这样性能比较差,且代码不够整洁。
以订单中使用作为例子。
需求:查询某个订单的详情,包括订单明细。
分析:数据库中订单有两个表,主表保存,订单的主要信息,子表保存订单的详细商品信息。
处理:XML中的具体代码如下

<sql id="Base_Column_List">
    toi.id, toi.number, toi.date, toi.custom_id, toi.custom_name,
    toi.create_time, toi.last_update_time, toi.order_number, toi.phone, toi.address,
    toie.id, toie.parent_id, toie.parent_number, toie.goods_id, toie.goods_number, toie.goods_name,
    toie.standard, toie.unit_id, toie.unit_name, toie.unit_count, toie.aux_unit_id, toie.aux_unit_name,
    toie.aux_unit_count, toie.price, total_price, toie.remark, toie.create_time
  </sql>
<resultMap id="orderMessageResultMap" type="org.guoxn.order.api.model.OrderInfo">
    <id property="id" column="id"/>
    <result property="number" column="number"/>
    <result column="date" property="date" jdbcType="VARCHAR" />
    <result column="custom_id" property="customId" jdbcType="VARCHAR" />
    <result column="custom_name" property="customName" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="VARCHAR" />
    <result column="last_update_time" property="lastUpdateTime" jdbcType="VARCHAR" />
    <result column="phone" property="phone" jdbcType="VARCHAR" />
    <result column="address" property="address" jdbcType="VARCHAR" />
    <result column="order_number" property="orderNumber" jdbcType="VARCHAR" />
    <collection property="orderEntryList" ofType="org.guoxn.order.api.model.OrderInfoEntry">
      <id property="id" column="parent_id"/>
      <result column="parent_number" property="parentNumber" jdbcType="VARCHAR"/>
      <result column="goods_id" property="goodsId" jdbcType="CHAR"/>
      <result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>
      <result column="goods_number" property="goodsNumber" jdbcType="VARCHAR"/>
      <result column="standard" property="standard" jdbcType="VARCHAR"/>
      <result column="unit_id" property="unitId" jdbcType="CHAR"/>
      <result column="unit_name" property="unitName" jdbcType="VARCHAR"/>
      <result column="unit_count" property="unitCount" jdbcType="DOUBLE"/>
      <result column="aux_unit_id" property="auxUnitId" jdbcType="CHAR"/>
      <result column="aux_unit_name" property="auxUnitName" jdbcType="VARCHAR"/>
      <result column="aux_unit_count" property="auxUnitCount" jdbcType="DOUBLE"/>
      <result column="price" property="price" jdbcType="DOUBLE"/>
      <result column="total_price" property="totalPrice" jdbcType="DOUBLE"/>
      <result column="remark" property="remark" jdbcType="VARCHAR"/>
      <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </collection>
  </resultMap>
<select id="selectOrderInfo" resultMap="orderMessageResultMap">
    select <include refid="Base_Column_List"/>
    from t_order_info toi left join t_order_info_entry toie
    on toi.id = toie.parent_id
    where toi.id = #{id}
  </select>

调用该方法就可以得到一个订单实例,并且包含其订单的详细品项的数组属性。

总结:

Mybatis有很多易用且方便的能够快速提升开发效率的用法,作为一个新项目几乎必用的组件,一定要善于使用它。

相关文章

  • Mybatis XML映射之结果映射

    前言:最近有空重新回顾了一下Mybatis的用法,结合自己项目中的实际应用,发现自己并没有有效的使用Mybatis...

  • MyBatis快速入门(11)XML映射文件-select

    XML映射文件 MyBatis 的真正强大在于它的映射语句,这是它的魔力所在。由于它的异常强大,映射器的 XML ...

  • Mybatis映射文件

    mybatis二级缓存在的问题? 1、Mybatis 映射文件之增删改: 2、Mybatis 映射文件之 Inse...

  • @Result与@ResultMap

    mybatis在使用XML配置时,可以通过标签组定义定义结果映射集使用@Results注解来...

  • mybatis学习小点记录

    mybatis的mybatis-config.xml及映射表mapper.xml的位置一般都是放在resource...

  • mybatis的基础应用

    mybatis入门 mybatis解决的问题 xml或者配置映射sql,执行sql,返回对象 mybatis的xm...

  • 3.详解MyBatis的配置文件

    MyBatis配置xml层次结构,而且必须注意其顺序。 MyBatis官网中文XML映射配置文件 1.proper...

  • Mybatis映射原理

    MyBatis 的真正强大之处在于它的映射语句,这也是它的魔力所在。由于它的映射语句异常强大,映射器的 XML 文...

  • 那些配置文件

    1024 获取下boot-cloud的jar mybatis-config.xml mybatis的映射文件Boo...

  • Mybatis02

    1、Mybatis入门程序之添加用户返回ID 1.1在映射文件 User.xml 中添加 insert 的 SQL...

网友评论

      本文标题:Mybatis XML映射之结果映射

      本文链接:https://www.haomeiwen.com/subject/hwnmvctx.html