1.懒加载
在配置映射关联关系的Result中的fetchType属性选择懒加载,一共有三种加载方式,DEFAULT:默认,默认就是立即加载;EAGER:立即加载,每次查询都会查找这个属性;LAZY:懒加载,每当有需要用到这个属性的时候才会查找
@Select("select * from student")
@Results({
@Result(id = true,property = "id",column = "id"),
@Result(property = "name",column = "name"),
@Result(property = "age",column = "age"),
@Result(property = "gender",column = "gender"),
@Result(property = "cid",column = "cid"),
@Result(property = "aClass",column = "cid",one = @One(select = "cn.com.witsystem.dao.ClassMapper.findClassById",
fetchType = FetchType.LAZY)),
})
List<Student> findAll();
2.DTO
通过需要查询的字段创建一个DTO类,详情
3.缓存
原理图
第一次执行查询操作的时候会写入缓存,然后再执行查询操作的时候就是读取缓存中的数据,如果执行修改或删除操作就会清空缓存。关于mybatis注解版开启二级缓存