MyBatis 在resultMap中使用association但是没有指定id

时间:2019-02-19
本文章向大家介绍MyBatis 在resultMap中使用association但是没有指定id,主要包括MyBatis 在resultMap中使用association但是没有指定id使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

情景描述:
     resultMap中,如果不指定id列,在映射中同时存在result 和association,如果表中映射过的字段 有连续的非空重复值,那么存在重复值的行不会被加入结果集中。
JavaBean(省略getter  setter):

public class Account {
    /** 属性账目编号 */
    private Long id;
    /** 属性物料编码相关信息 */
    private Item item;
    /**
     * 属性单据类型 0 出库,1入库 ,9:默认(出现9则是异常)
     */
    private int type = 9;
    /** 属性数量 */
    private Double number;
    /** 属性归属单位 */
    private String department;
    /** 属性操作员 */
    private String operator;
    /** 属性经办人 */
    private String handler;
    /** 属性出入库原因 */
    private String reason;
    /** 属性出入库时间 */
    private Date optTime;
    /** 属性录入时间 */
    private Date createTime = Calendar.getInstance().getTime();
    /** 属性最后修改时间 */
    private Date updateTime;
    /** 属性最后修改人 */
    private User updater;
}

Mapper:
         
 <resultMap type="Account"id="accountMap"autoMapping="true">
        <id column ="id" property="id"/> 
        <result column ="opt_time" property="optTime"/>
        <result column ="create_time" property="createTime"/>
        <result column ="update_time" property="updateTime"/>
        <association property ="item" javaType="Item">
              <result column ="item_code" property="code"/>
              <result column ="item_name" property="name"/>
              <result column ="item_model" property="model"/>
              <result column ="unit" property="unit"/>
        </association >
</resultMap >


SQL:
     select * from t_account;

在数据库中的查询结果(部分):

1、图中涂红的部分修改过,原来的值与上一行相同,都是 00:00:00。
2、修改以前,程序查询结果中只有id<=38的数据,因为id=37行(以下简称37行)与36行的opt_time字段不同,38行与37行的create_time、update_time字段不同,而39行与38行的这三个映射字段的值完全相同
3、修改后,查询结果中39、40行能显示了,其后的数据不能显示
4、在Mapper中加入id列设置后,所有数据行都能显示了。
--------------------- 
作者:sailei 
来源:CSDN 
原文:https://blog.csdn.net/sailei/article/details/51158462 
版权声明:本文为博主原创文章,转载请附上博文链接!