使用@Data注解与mybatis的懒加载机制实现一对一关系查询时,发现怎么配置都无效,就是一下都查出来了,根本没有懒加载
1.application.yml配置文件配置如下:
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.steven.springboot.datasource.model
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
lazy-loading-enabled: true
aggressive-lazy-loading: false
lazyLoadTriggerMethods:
2.实体类user.java
package com.steven.springboot.datasource.model;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("users")
public class UserDO {
private Integer id;
private String username;
private String password;
private int did;
private DeptDO deptDO;
}
3.实体类DeptDO.java
package com.steven.springboot.datasource.model;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.List;
@Data
@TableName("dept")
public class DeptDO {
private Integer id;
private String name;
private List<UserDO> userDOList;
}
4.映射文件UserMapper.xml
<?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="com.steven.springboot.datasource.mapper.UserMapper">
<resultMap id="getUserAndDeptStep" type="UserDO">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="did" column="did"/>
<association property="deptDO" select="com.steven.springboot.datasource.mapper.DeptMapper.getDeptByStep" column="did"/>
</resultMap>
<select id="selectUserAndDeptStep" resultMap="getUserAndDeptStep">
select * from users where username like concat(#{username},"%") and password=#{password}
</select>
</mapper>
5.映射文件DeptMapper.xml
<?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="com.steven.springboot.datasource.mapper.DeptMapper">
<select id="getDeptByStep" resultType="DeptDO">
select * from dept where id=#{id}
</select>
</mapper>
6.测试类UserTests.java
package com.steven.springboot.datasource;
import com.steven.springboot.datasource.mapper.UserMapper;
import com.steven.springboot.datasource.model.UserDO;
import
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootDataSourceApplication.class)
public class UserTests {
@Resource
UserMapper userMapper;
@Test
public void getUserAndDeptByStep() {
UserDO userDO=new UserDO();
userDO.setUsername("张希");
userDO.setPassword("123456");
List<UserDO> userDOList = userMapper.selectUserAndDeptStep(userDO);
System.out.println(userDOList);
}
}
所以解决方案就是两种
- 使用@Data同时再重写一遍toString()方法
- 不用toString的话,就把@Data拆开
|