• mybatis--01重复使用resultmap,便于在association中自动映射。


    便于在association中自动映射。需要加一个columnPrefix。

    columnPrefix: 在resultMap的元素中加自动加前缀,便于重用resultmap。

    例:

    1.在每个字段上加author_

    <resultMap id="authorResult" type="Author">
      <id property="id" column="author_id"/>
      <result property="username" column="author_username"/>
      <result property="password" column="author_password"/>
      <result property="email" column="author_email"/>
      <result property="bio" column="author_bio"/>
    </resultMap>

    2.association中使用

    <resultMap id="blogResult" type="Blog">
      <id property="id" column="blog_id" />
      <result property="title" column="blog_title"/>
      <association property="author"
        resultMap="authorResult" />
      <association property="coAuthor"
        resultMap="authorResult"
        columnPrefix="co_" />
    </resultMap>

     3.执行sql

    <select id="selectBlog" resultMap="blogResult">
      select
        B.id            as blog_id,
        B.title         as blog_title,
        A.id            as author_id,
        A.username      as author_username,
        A.password      as author_password,
        A.email         as author_email,
        A.bio           as author_bio,
        CA.id           as co_author_id,
        CA.username     as co_author_username,
        CA.password     as co_author_password,
        CA.email        as co_author_email,
        CA.bio          as co_author_bio
      from Blog B
      left outer join Author A on B.author_id = A.id
      left outer join Author CA on B.co_author_id = CA.id
      where B.id = #{id}
    </select>

    改造结果:

    <?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="com.ruoyi.ruoyiwx.mapper.WxMassMapper">
    
        <resultMap id="MassandsendResult" type="WxMass" autoMapping="true" >
    <!--        <id property="id" column="id"/>-->
    <!--        <result property="sysUnitId" column="sys_unit_id" />-->
    <!--        <result property="name" column="name" />-->
    <!--        <result property="type" column="type" />-->
    <!--        <result property="mediaId" column="media_id" />-->
    <!--        <result property="picurl" column="picurl" />-->
    <!--        <result property="scope" column="scope" />-->
    <!--        <result property="content" column="content" />-->
    <!--        <result property="status" column="status" />-->
    <!--        <result property="wxid" column="wxid" />-->
    <!--        <result property="opBy" column="opBy" />-->
    <!--        <result property="opAt" column="opAt" />-->
    <!--        <result property="delFlag" column="delFlag" />-->
            <association property="massSend" resultMap="wxMassSendResult" columnPrefix="ms_" >
            </association>
        </resultMap>
    
        <resultMap id="wxMassSendResult" type="WxMassSend" >
            <id property="id" column="ms_id" />
            <result property="sysUnitId" column="sys_unit_id" />
            <result property="massId" column="massId" />
            <result property="receivers" column="receivers" />
            <result property="status" column="status" />
            <result property="msgId" column="msgId" />
            <result property="errCode" column="errCode" />
            <result property="errMsg" column="errMsg" />
            <result property="wxid" column="wxid" />
            <result property="opBy" column="opBy" />
            <result property="opAt" column="opAt" />
            <result property="delFlag" column="delFlag" />
        </resultMap>
    
        <select id="getWxMassandsend" resultMap="MassandsendResult" >
            select
                m.*,
                ms.id ms_id,
                ms.sys_unit_id ms_sys_unit_id,
                ms.massId ms_massId,
                ms.receivers ms_receivers,
                ms.status ms_status,
                ms.msgId ms_msgId,
                ms.errCode ms_errCode,
                ms.errMsg ms_errMsg,
                ms.wxid ms_wxid,
                ms.opBy ms_opBy,
                ms.wxid ms_wxid,
                ms.opAt ms_opAt,
                ms.delFlag ms_delFlag
            from wx_mass m,wx_mass_send ms
            where m.id=ms.massId and m.wxid=ms.wxid and m.wxid=#{1}
        </select>
    </mapper>

    参考:

    关联的嵌套结果映射   https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps

  • 相关阅读:
    JMeter Gui – TestElement约定(转自约会言行的博客,链接:http://blog.csdn.net/yue530tomtom/article/details/77649872?locationNum=4&fps=1)
    Maven的下载和安装
    开源蜘蛛集合(转自haizhiguang博客,链接:http://blog.csdn.net/haizhiguang/article/details/20209573)
    HtmlCleaner CleanerProperties 参数配置(转自macken博客,链接:http://macken.iteye.com/blog/1579809)
    java解析xml汇总(转自倾城幻影-Java解析xml汇总,链接:http://www.cnblogs.com/jiugehuanying/archive/2012/01/12/2320058.html)
    [20191005机房测试] Reverse
    【HAOI2010】软件安装
    [20191004机房测试] ZGY的早餐
    [20191004机房测试] C++锦标赛
    [20191004机房测试] 三角
  • 原文地址:https://www.cnblogs.com/kltsee/p/13738179.html
Copyright © 2020-2023  润新知