我是靠谱客的博主 复杂心情,最近开发中收集的这篇文章主要介绍Mybatis的resultmap的继承、关联,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

有一个数据集输出如下,a表与b表关联,a表主键recseq,其他字段为f96_*,b表主键b_recseq,其他字段为source/keyPart/supplier/comment/sn

在这里插入图片描述
由于source/keyPart/supplier/comment是通用字段,其他地方也会复用,所有将这4个字段抽离,entity如下:

@Data
@NoArgsConstructor
public class KeyBase {
  private String recseq;
  private String source;
  private String keyPart;
  private String supplier;
  private String comment;
}

对应的resultmap:

<resultMap id="KeyBaseMap" type="com.xx.yy.KeyBase">
    <id column="b_recseq" jdbcType="VARCHAR" property="recseq" />
    <result column="source" jdbcType="VARCHAR" property="source" />
    <result column="keyPart" jdbcType="VARCHAR" property="keyPart" />
    <result column="supplier" jdbcType="VARCHAR" property="supplier" />
    <result column="comment" jdbcType="VARCHAR" property="comment" />
  </resultMap>

那么整个数据集的entity定义如下,它继承于T96PdLog又有KeyBase信息,也多了个sn字段:


@NoArgsConstructor
@Data
public class KeyRawDetail extends T96PdLog {
   private KeyBase keyExtInfo;
   private String sn;
}

对应的resultmap应该定义成如下:

<resultMap id="RawDataMap" type="com.xx.yy.entity.KeyRawDetail"
             extends="com.xx.yy.dao.T96PdLogMapper.BaseResultMap">
    <result column="sn" jdbcType="VARCHAR" property="sn" />
    <association property="keyExtInfo"
                 column="recseq"
                 resultMap="KeyBaseMap"
                 />
  </resultMap>

补充说明一下,association 应用比较灵活,这里的意思是,通过A表的主键recseq(column指定)去关联B表的b_recseq(看KeyBaseMap的id定义),然后mybatis会根据KeyBaseMap里面的结果集定义自动将数据集中的source/keyPart/supplier/comment组装成keyExtInfo的字段。

最后,select的定义:

<select id="getRawData" resultMap="RawDataMap">
        select * from rawdata order by f96_mgtbarcd;
  </select>

结果如下:
在这里插入图片描述

最后

以上就是复杂心情为你收集整理的Mybatis的resultmap的继承、关联的全部内容,希望文章能够帮你解决Mybatis的resultmap的继承、关联所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部