有一个数据集输出如下,a表与b表关联,a表主键recseq,其他字段为f96_*,b表主键b_recseq,其他字段为source/keyPart/supplier/comment/sn
由于source/keyPart/supplier/comment是通用字段,其他地方也会复用,所有将这4个字段抽离,entity如下:
复制代码
1
2
3
4
5
6
7
8
9
10@Data @NoArgsConstructor public class KeyBase { private String recseq; private String source; private String keyPart; private String supplier; private String comment; }
对应的resultmap:
复制代码
1
2
3
4
5
6
7
8<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字段:
复制代码
1
2
3
4
5
6
7
8@NoArgsConstructor @Data public class KeyRawDetail extends T96PdLog { private KeyBase keyExtInfo; private String sn; }
对应的resultmap应该定义成如下:
复制代码
1
2
3
4
5
6
7
8
9<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的定义:
复制代码
1
2
3
4<select id="getRawData" resultMap="RawDataMap"> select * from rawdata order by f96_mgtbarcd; </select>
结果如下:
最后
以上就是复杂心情最近收集整理的关于Mybatis的resultmap的继承、关联的全部内容,更多相关Mybatis内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复