lombok中的@Data注解和Mybatis的懒加载机制冲突解决

lombok中的@Data注解和Mybatis的懒加载机制冲突解决

今天使用mybatis的懒加载机制实现一对一关系查询时,发现怎么配置都无效,就是一下都查出来了,根本没有懒加载。
我使用的是springBoot集成的mybatis方式:
application.yml配置文件配置如下:

mybatis:
  mapper-locations: mapper/*.xml
  type-aliases-package: cn..sso.mapper
  type-handlers-package: org.apache.ibatis.type.LocalDateTypeHandler
  configuration:
  # 启动懒加载
    lazy-loading-enabled: true
  #false 为按需加载,可通过fetchType="eager"设置为立即加载
    aggressive-lazy-loading: false
    lazy-load-trigger-methods:

User的模型:

@Data
public class User implements Serializable {

    private static final long serialVersionUID = -2522840344126053929L;

    @ApiModelProperty(value = "主键id")
    private Integer id;

    @ApiModelProperty(value = "登录账号")
    private String loginname;

    @ApiModelProperty(value = "密码",required = true)
    private String password;

    @ApiModelProperty(value = "手机号")
    private Long mobile;

    @ApiModelProperty(value = "电子邮箱")
    private String email;

    @ApiModelProperty(value = "微博ID")
    private String weiboid;

    @ApiModelProperty(value = "微信ID")
    private String wechatid;

    @ApiModelProperty(value = "QQID")
    private String qqopenid;

    @ApiModelProperty(value = "状态 0:正常 -1:冻结")
    private Integer status;

    @ApiModelProperty(value = "注册日期")
    private Date registertime;

    @ApiModelProperty(value = "最后一次登录日期")
    private Date lastlogintime;

    @ApiModelProperty(value = "最后一次登录ip")
    private String lastloginip;

    @ApiModelProperty(value = "登录次数")
    private Integer count;

    @ApiModelProperty(value = "用户扩展信息")
    private UserInfo userinfo;

    public User(){}

    public User(Integer id) {
        this.id = id;
    }
}

UserMapper(就是User和UserInfo是一对一的关系,我想通过查询User然后懒加载userInfo,下面association对应的查询)

<?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="cn.sso.persistence.dao.UserDao">
    <resultMap id="BaseResultMap" type="cn.sso.persistence.entity.dto.User">
        <id column="Id" jdbcType="INTEGER" property="id"/>
        <result column="LoginName" jdbcType="VARCHAR" property="loginname"/>
        <result column="Password" jdbcType="VARCHAR" property="password"/>
        <result column="Mobile" jdbcType="INTEGER" property="mobile"/>
        <result column="Email" jdbcType="VARCHAR" property="email"/>
        <result column="WeiboId" jdbcType="VARCHAR" property="weiboid"/>
        <result column="WechatId" jdbcType="VARCHAR" property="wechatid"/>
        <result column="QQOpenId" jdbcType="VARCHAR" property="qqopenid"/>
        <result column="Status" jdbcType="INTEGER" property="status"/>
        <result column="RegisterTime" jdbcType="TIMESTAMP" property="registertime"/>
        <result column="LastLoginTIme" jdbcType="TIMESTAMP" property="lastlogintime"/>
        <result column="LastLoginIP" jdbcType="VARCHAR" property="lastloginip"/>
        <result column="Count" jdbcType="INTEGER" property="count"/>
        <!--一对一延迟加载:property对应dto中的对象实体引用,column对应要给子查询传的参数,select对应Dao中的查询方法
        javaType对应返回值类型,fetchType="eager"表示直接加载,不写默认懒加载-->
        <association property="userinfo" column="Id" fetchType="lazy"
                    select="cn.sso.persistence.dao.UserInfoDao.selectUserInfoByUserId"
                    javaType="cn.sso.persistence.entity.dto.UserInfo" />

    </resultMap>
    <sql id="Base_Column_List">
    Id, LoginName, Password, Mobile, Email, WeiboId, WechatId, QQOpenId, Status, RegisterTime,
    LastLoginTIme, LastLoginIP, Count
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from user
        where Id = #{id,jdbcType=INTEGER}
    </select>
    
</mapper>

运行测试类,结果怎么查都是直接把User和UserInfo一起查不来了,执行了两次sql,这跟我的初衷不一致啊,没有实现懒加载:
在这里插入图片描述
然后我不用@Data注解,然后自己alt+Ins生成getter,setter,toString方法就可以实现懒加载,百思不得其解啊。

最后试了一大圈,发现问题

只要使用了@Data懒加载就会失效,但是你显示的重写一遍toString(),懒加载就能生效
注:@Data相当于@Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode这5个注解的合集

所以解决方案就是两种:
  • 使用@Data同时再重写一遍toString()方法

  • 不用toString的话,就把@Data拆开:

      @Setter
      @Getter
      @EqualsAndHashCode				
      public class User implements Serializable {
    
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值