Class类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Class {
private Long id;
private String name;
private List students;
}
Student类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Long id;
private String name;
private Long classId;
private Class clazz;
}
StudentMapper写入如下代码
Student selectByIdResult(Long id);
StudentMapper.xml写入如下代码
s.id,
s.name,
s.class_id,
c.id as classId,
c.name as className
StudentMapper写入如下代码
Student selectByIdAssociation(Long id);
StudentMapper.xml写入如下代码
s.id,
s.name,
s.class_id,
c.id as classId,
c.name as className
StudentMapper.xml写入如下代码
Student selectByIdStep(Long id);
ClassMapper写入如下代码
Class selectById(Long id);
StudentMapper.xml写入如下代码
id,
name,
class_id
ClassMapper.xml写入如下代码
id,
name
上述俩种查询每次都要查询2条sql,有时我们不需要查询2条。并且联表查询会出现笛卡尔现象,性能很差
这可以是用延迟加载来提高性能,用到属性的时候查询不用就不会查询
局部设置 fetchType=“lazy” 。只会作用域这条sql
全局开启延迟加载
局部关闭使用 fetchType=“eager”
Class类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Class {
private Long id;
private String name;
private List students;
}
Student类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Long id;
private String name;
private Long classId;
private Class clazz;
}
ClassMapper写入如下代码
Class selectCollection(Long id);
ClassMapper.xml写入如下代码
c.id,
c.name,
s.id as studentId,
s.name as studentName,
s.class_id as classId
ClassMapper写入如下代码
Class selectByIdStep(Long id);
StudentMapper写入如下代码
Student selectById(Long classId);
ClassMapper.xml写入如下代码
id,
name
StudentMapper.xml写入如下代码
id,
name,
class_id
同理可以设置延迟加载