二级缓存默认不开启, 开启之后, 可以在不同的session范围内执行相同的数据查询, 只进行一次数据库检索
开启设置
- 在mapper文件中 设置cache
- 在实体类中实现序列化接口, 否则会报错NotSerializableException
测试
@Test |
映射文件
添加cache标签
<mapper namespace="io.github.coinsjack.dao.ShopMapper">
|
结果
可以看到我在另一个session里面进行相同的数据查询时, 并没有再次进行实际地数据库检索
2018-12-28 19:27:06,533 [main] DEBUG [io.github.coinsjack.dao.ShopMapper] - Cache Hit Ratio [io.github.coinsjack.dao.ShopMapper]: 0.0 2018-12-28 19:27:06,960 [main] DEBUG [io.github.coinsjack.dao.ShopMapper.getShopById] - ==> Preparing: select * from tb_shop where `shop_id` = ? 2018-12-28 19:27:07,052 [main] DEBUG [io.github.coinsjack.dao.ShopMapper.getShopById] - ==> Parameters: 1(Integer) 2018-12-28 19:27:07,143 [main] DEBUG [io.github.coinsjack.dao.ShopMapper.getShopById] - <== Total: 1 Shop{id=1, ownerId=1, areaId=3, categoryId=14, name='new 正式店铺名称', desc='测试描述', addr='正式地址', phone='13810524086', image='/upload/item/shop/1/2017091621545314507.jpg', priority=10, createTime=2017-08-03, lastEditTime=2017-09-16, enableStatus=0, advice='审核中'} 2018-12-28 19:27:07,182 [main] DEBUG [io.github.coinsjack.dao.ShopMapper] - Cache Hit Ratio [io.github.coinsjack.dao.ShopMapper]: 0.5 Shop{id=1, ownerId=1, areaId=3, categoryId=14, name='new 正式店铺名称', desc='测试描述', addr='正式地址', phone='13810524086', image='/upload/item/shop/1/2017091621545314507.jpg', priority=10, createTime=2017-08-03, lastEditTime=2017-09-16, enableStatus=0, advice='审核中'} |