延迟加载
使用懒加载时
注意事项:使用懒加载时,要注意,开启全局设置 懒加载设置
<!--是否启用延迟加载功能 多对象关联-->
<setting name="lazyLoadingEnabled" value="true"></setting>
<!--抓取策略
fetchType="lazy" 使用懒加载
fetchType="eager" 使用立即加载
-->
<resultMap id="myProvinceLoad" type="Provinces" autoMapping="true">
<id column="id" property="id"></id>
<collection property="cities"
ofType="Cities"
select="getCitiesByPid"
column="provinceid"
fetchType="eager">
</collection>
</resultMap>
多对一延时加载
<!--public List<Cities> lazyLoad();-->
<!--延时加载-->
<select id="lazyLoad" resultMap="myCities">
select * from cities
</select>
<resultMap id="myCities" type="Cities" autoMapping="true">
<id property="id" column="id"></id>
<!--给provinces属性赋值 通过select getProvincesById查询赋值
通过 column="provinceid"指定的列传入值到select语句-->
<association property="provinces"
javaType="Provinces"
column="provinceid"
select="getProvincesById">
</association>
</resultMap>
<select id="getProvincesById" resultType="Provinces">
select * from provinces where provinceid = #{provinceid}
</select>
一对多延时加载
<!-- //延迟加载
public List<Provinces> ProLazyLoad();-->
<select id="ProLazyLoad" resultMap="myPro">
select * from provinces
</select>
<resultMap id="myPro" type="Provinces" autoMapping="true">
<id property="id" column="id"></id>
<collection property="cities"
ofType="Cities"
column="provinceid"
select="getCities">
</collection>
</resultMap>
<select id="getCities" resultType="Cities">
select * from cities where provinceid = #{provinceid}
</select>
缓存
一级缓存:
默认可以直接使用 同一个Sqlsession中数据的缓存
同一个sqlSession中执行多次相同查询,sql日志只会打印一次sql语句
重点:缓存只能在查询中使用
@Test
public void firstOne(){
SqlSession session = sqlSessionFactory.openSession();
ProvincesMapper mapper = session.getMapper(ProvincesMapper.class);
List<Provinces> list =mapper.proList();
session.clearCache();
List<Provinces> list1 = mapper.proList();
}
session.clearCache();//清除缓存
<!--自动映射
useCache="true" 把当前sql的运行结果进行缓存
flushCache="false" 默认false 不清除缓存
-->
<select id="ciList" resultType="Cities" useCache="true" flushCache="true">
select * from cities
</select>
dml语句(增删改)默认清除一级缓存 没有useCache选项 flushCache默认是true
二级缓存:
需要配置才能使用 同一个SqlsessionFactory中(不同的Sqlsession中的缓存)数据的缓存
mybatis在二级缓存处理时 不支持多对象关联查询
1.对缓存的对象实现序列化接口(Serializable)
public class Cities implements Serializable {
private int id;
private String cityid;
private String city;
private String provinceid;
/*多对一*/
private Provinces provinces;
2.在缓存对象所在的sqlmapper映射文件 添加Cache 缓存功能
<!--二级缓存
注意:
二级缓存是事务性的。这意味着,当 SqlSession 完成并提交时,或是完成并回滚,
但没有执行 flushCache=true 的 insert/delete/update 语句时,缓存会获得更新。
eviction="FIFO"
LRU – 最近最少使用:移除最长时间不被使用的对象。
FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
SOFT – 软引用:基于垃圾回收器状态和软引用规则移除对象。
WEAK – 弱引用:更积极地基于垃圾收集器状态和弱引用规则移除对象。
flushInterval="60000"
(刷新间隔)属性可以被设置为任意的正整数,设置的值应该是一个以毫秒为单位的合理时间量。
默认情况是不设置,也就是没有刷新间隔,缓存仅仅会在调用语句时刷新。
size="512"
(引用数目)属性可以被设置为任意正整数,要注意欲缓存对象的大小和运行环境中可用的内存资源。
默认值是 1024。
readOnly="true"
(只读)属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓存对象的相同实例。
因此这些对象不能被修改。这就提供了可观的性能提升。而可读写的缓存会(通过序列化)返回缓存对象的拷贝。
速度上会慢一些,但是更安全,因此默认值是 false。
-->
<cache
eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/>
3.检查sql语句中是否使用flushCache清空缓存 如果清空缓存 二级缓存不可用
flushCache="false"//注意:不清除一级缓存 二级缓存才能使用
4.测试
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//从工厂中获取指定会话
SqlSession sqlSession = sqlSessionFactory.openSession();
CitiesMapper citiesMapper = sqlSession.getMapper(CitiesMapper.class);
List<Cities> list2 = citiesMapper.list();
sqlSession.close();
//清除缓存
//sqlSession.clearCache();
SqlSession sqlSession2 = sqlSessionFactory.openSession();
CitiesMapper citiesMapper2 = sqlSession2.getMapper(CitiesMapper.class);
List<Cities> list3 = citiesMapper2.list();
sqlSession2.close();
分页插件(PageHelper)
1.引入jar包
guava-28.2-jre.jar
jsqlparser-3.1.jar
pagehelper-5.1.10.jar
2.配置mybatis核心配置文件中配置分页插件
一定要在environments之前
<!--引入pagehelper插件-->
<!--注意这里要写成 PageInterceptor 5.0之前的版本都是写PageHelper 5.0之后要换成PageInterceptor -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--reasonable: 分页合理化参数,默认值为false-->
</plugin>
</plugins>
3.使用插件
@Test
public void proList(){
SqlSession session = sqlSessionFactory.openSession();
CitiesMapper citiesMapper =session.getMapper(CitiesMapper.class);
//查询第二页的数据,每页显示5行
PageHelper.startPage(2,5);
List<Cities> list1 = citiesMapper.ciList();
PageInfo<Cities> pageInfo = new PageInfo<>(list1);
System.out.println(list1.toString());
session.close();
}