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 {