懒加载(延迟加载)
我们为什么要设置懒加载?(好处)
-
减少不必要的数据库查询:在某些情况下,我们可能只需要使用对象的一部分数据,懒加载可以避免在获取对象时加载不需要的数据,提高了查询效率。
-
减少内存消耗:使用懒加载可以避免一次性加载大量数据,减少内存的消耗,尤其是在处理大量数据的情况下非常有用。
-
提高性能:懒加载可以根据需要动态地加载数据,不必一次性加载所有数据,可以减少对数据库的压力,提高系统的性能。
懒加载的使用场景
适合于进行关联查询时,比如一对一或多对一等的场景,在resultMap的association或collection中使用
如何使用懒加载?
- 先准备两张表,及其实体类
User
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private Date birthday;
private String sex;
private String address;
//用户与账户的关系是一对多 一个用户可能拥有多个账户
private List<Account> accountList;
public List<Account> getAccountList() {
return accountList;
}
public void setAccountList(List<Account> accountList) {
this.accountList = accountList;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
", accountList=" + accountList +
'}';
}
}
Account
public class Account {
private Integer id;
private Integer uid;
private Double money;
//一对一
// private User user;
@Override
public String toString() {
return "Account{" +
"aid=" + id +
", uid=" + uid +
", money=" + money +
'}';
}/*
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}*/
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
- 在接口及mapper映射文件中写
UserMapper
public interface UserMapper {
/**
* 查询所有信息包含用户所有的账户信息
* @return
*/
User findAll(Integer id);
}
<resultMap id="findAllResultMap" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="password" column="password"></result>
<result property="birthday" column="birthday"></result>
<result property="sex" column="sex"></result>
<result property="address" column="address"></result>
<!-- 开启懒加载
property:属性名
ofType:集合的泛型
select:懒加载时执行的查询语句
column:查询语句所需要的参数
fetchType:lazy局部开启懒加载
-->
<collection property="accountList" ofType="account"
select="com.xxx.mapper.AccountMapper.getAccountByUid" column="id"
fetchType="lazy">
</collection>
</resultMap>
<select id="findAll" parameterType="int" resultMap="findAllResultMap" >
select * from user where id=#{id}
</select>
Account Mapper
<select id="getAccountByUid" parameterType="int" resultType="account">
select * from account where uid=#{uid}
</select>
测试
@Test
public void testFindAll(){
UserMapper userMapper= ss.getMapper(UserMapper.class);
User user= userMapper.findAll(41);
System.out.println(user.getUserName());
}
注意:使用equals,clone,hashCode,toString方法时,无论是否开启懒加载都会执行你需要懒加载的内容,除非在mybatis-config.xml的配置中写“/<setting name=“lazyLoadTriggerMethods” value=“false”/">
全局配置懒加载
在mybatis-config.xml中写
<settings>
<!-- 开启懒加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>