MyBatis鉴别器discriminator以及collection、association的使用

MyBatis官方文档中的解释(http://www.mybatis.org/mybatis-3/zh/index.html)

  • association – 一个复杂类型的关联;许多结果将包装成这种类型

    • 嵌套结果映射 – 关联本身可以是一个 resultMap 元素,或者从别处引用一个

  • collection – 一个复杂类型的集合

    • 嵌套结果映射 – 集合本身可以是一个 resultMap 元素,或者从别处引用一个

  • discriminator – 使用结果值来决定使用哪个 resultMap

    • case – 基于某些值的结果映射

      • 嵌套结果映射 – case 本身可以是一个 resultMap 元素,因此可以具有相同的结构和元素,或者从别处引用一个

 

相关代码

<resultMap id="ResultMap"  type="com.neusoft.custsrv.system.api.psgcheckplan.entity.PsgCheckplanEntity">
            <result column="row_id" property="rowId" />
            <result column="company_id" property="companyId" />
            <result column="dept_id" property="deptId" />
                ......
            <result column="deleted_flag" property="deletedFlag" />
            <result column="origin_flag" property="originFlag" />
            <result column="origin_app" property="originApp" />
            <!-- 
                一个resultMap中似乎只能有一个discriminator,多字段的判断可以放在继承的resultMap中 
                如本例中,如果还需要对company_id进行判断,则可将判断写在PlanExecInfoMap或DetailMap中
            -->
            <discriminator column="status" javaType="String">
                    <!-- 以下两种写法效果相同 -->
               <case value="2" resultMap="PlanExecInfoMap" />
                  <case value="3"  resultType="com.neusoft.custsrv.system.api.psgcheckplan.entity.PsgCheckplanEntity">
                        <association property="planExecInfo"  javaType="Map" column="row_id"
                              select="com.neusoft.custsrv.system.psgcprange.getChkPlanExecStatus" />
                  </case>
            </discriminator>
      </resultMap>
      
        <!-- 
            子ResultMap中的内容可以直接写在父ResultMap中引用处的case标签内
         -->
      <resultMap id="PlanExecInfoMap"  type="com.neusoft.custsrv.system.api.psgcheckplan.entity.PsgCheckplanEntity" extends="ResultMap">
            <association property="planExecInfo" javaType="Map"  column="row_id"
                  select="com.neusoft.custsrv.system.psgcprange.getChkPlanExecStatus" />
      </resultMap>
      
      <resultMap id="DetailMap"  type="com.neusoft.custsrv.system.api.psgcheckplan.entity.PsgCheckplanEntity" extends="ResultMap">
            <collection property="ranges" javaType="ArrayList"  ofType="com.neusoft.custsrv.system.api.psgcpinspector.entity.PsgCpinspectorEntity" column="checkplanId=row_id"
                  select="com.neusoft.custsrv.system.psgcpinspector.selectList" />
            <!-- column="checkplanId=row_id",查询语句中的属性值等于该ResultMap中的哪个字段 -->
            <collection property="inspectors" javaType="ArrayList"  ofType="com.neusoft.custsrv.system.api.psgcpinspector.entity.PsgCpinspectorEntity" column="checkplanId=row_id"
                  select="com.neusoft.custsrv.system.psgcpinspector.selectList" />
      </resultMap>

com.neusoft.custsrv.system.psgcprange.getChkPlanExecStatus: 

      <select id="getChkPlanExecStatus" resultType="map"  parameterType="java.lang.String">
            SELECT count(0) total_cust,
            sum(if(`STATUS` = 1 OR `STATUS` = 2,1,0)) exc_cust
            FROM `md_psg_cprange`
            where
            CHECKPLAN_ID=${value}
      </select>

com.neusoft.custsrv.system.psgcpinspector.selectList:

      <select id="selectList" resultMap="ResultMap"  parameterType="com.neusoft.custsrv.system.api.psgcprange.entity.PsgCprangeEntity">
            select <include refid="BaseColnums" /> from md_psg_cprange a
            <include refid="QueryClause" />
            <if test="sorter != null">
                  order by ${sorter}
            </if>
      </select>

      <sql id="QueryClause">
            where 1=1 <include refid="WhereClause" />
      </sql>

      <sql id="WhereClause">
            <trim suffixOverrides="and | or">
                  <if test="rowId != null and rowId != ''">
                        and a.row_id = #{rowId}
                  </if>
                  <if test="companyId != null and companyId != ''">
                        and a.company_id = #{companyId}
                  </if>
                  <if test="checkplanId != null and checkplanId != ''">
                        checkplan_id = #{checkplanId},
                  </if>
                    ......
                  <if test="whereClause != null and whereClause != ''">
                  <![CDATA[
                        ${whereClause}
                  ]]>
                  </if>
            </trim>
      </sql>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值