好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java关于MyBatis缓存详解

什么是 MyBatis 缓存

使?缓存可以减少 Java 应?与数据库的交互次数,从而提升程序的运行效率。?如查询出 id = 1 的对象,第?次查询出之后会自动将该对象保存到缓存中,当下?次查询时,直接从缓存中取出对象即可, 无需再次访问数据库。

MyBatis 缓存分类

1.?级缓存:SqlSession级别,默认开启,并且不能关闭.(默认开启)

操作数据库时需要创建 SqlSession 对象,在对象中有?个 HashMap ?于存储缓存数据,不同的 SqlSession 之间缓存数据区域是互不影响的。 ?级缓存的作用域是 SqlSession 范围的,当在同?个 SqlSession 中执?两次相同的 SQL 语句事,第? 次执行完毕会将结果保存到缓存中,第?次查询时直接从缓存中获取。 需要注意的是,如果 SqlSession 执行了 DML 操作(insert、update、delete),MyBatis 必须将缓存清空以保证数据的准确性。

2.二级缓存:Mapper 级别,默认关闭,可以开启

使??级缓存时,多个 SqlSession 使?同?个 Mapper 的 SQL 语句操作数据库,得到的数据会存在? 级缓存区,同样是使? HashMap 进?数据存储,相?较于?级缓存,?级缓存的范围更?,多个 SqlSession 可以共??级缓存,?级缓存是跨 SqlSession 的。 ?级缓存是多个 SqlSession 共享的,其作?域是 Mapper 的同?个 namespace,不同的 SqlSession 两次执?相同的 namespace 下的 SQL 语句,参数也相等,则第?次执?成功之后会将数据保存到?级 缓存中,第?次可直接从?级缓存中取出数据。

二级缓存如何使用

1.MyBatis 自带的二级缓存

1.1config.xml 配置开启?级缓存

?

1

2

3

4

5

6

7

8

settings>

  <!-- 打印SQL-->

  < setting name = "logImpl" value = "STDOUT_LOGGING" />

  <!-- 开启延迟加载 -->

  < setting name = "lazyLoadingEnabled" value = "true" />

  <!-- 开启?级缓存 -->

  < setting name = "cacheEnabled" value = "true" />

</ settings >

1.2Mapper.xml 中配置?级缓存

?

1

< cache ></ cache >

1.3实体类实现序列化接口

?

1

2

3

4

5

6

7

8

9

@Data

@AllArgsConstructor

@NoArgsConstructor

public class Account implements Serializable {

  private long id;

  private String username;

  private String password;

  private int age;

}

2.ehcache 二级缓存(第三方)

2.1pom.xml 添加相关依赖

?

1

2

3

4

5

6

7

8

9

10

< dependency >

  < groupId >org.mybatis</ groupId >

  < artifactId >mybatis-ehcache</ artifactId >

  < version >1.0.0</ version >

</ dependency >

< dependency >

  < groupId >net.sf.ehcache</ groupId >

  < artifactId >ehcache-core</ artifactId >

  < version >2.4.3</ version >

</ dependency >

2.2添加 ehcache.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

< ehcache xmlns:xsi = "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

  xsi:noNamespaceSchemaLocation = "config/ehcache.xsd" >

  < diskStore />

  < defaultCache

  maxElementsInMemory = "1000"

  maxElementsOnDisk = "10000000"

  eternal = "false"

  overflowToDisk = "false"

  timeToIdleSeconds = "120"

  timeToLiveSeconds = "120"

  diskExpiryThreadIntervalSeconds = "120"

  memoryStoreEvictionPolicy = "LRU" >

  </ defaultCache >

</ ehcache >

2.3config.xml 配置开启?级缓存

?

1

2

3

4

5

6

7

8

< settings >

  <!-- 打印SQL-->

  < setting name = "logImpl" value = "STDOUT_LOGGING" />

  <!-- 开启延迟加载 -->

  < setting name = "lazyLoadingEnabled" value = "true" />

  <!-- 开启?级缓存 -->

  < setting name = "cacheEnabled" value = "true" />

</ settings >

2.4 Mapper.xml 中配置?级缓存

?

1

2

3

4

5

6

7

8

< cache type = "org.mybatis.caches.ehcache.EhcacheCache" >

  <!-- 缓存创建之后,最后?次访问缓存的时间?缓存失效的时间间隔 -->

  < property name = "timeToIdleSeconds" value = "3600" />

  <!-- 缓存?创建时间起?失效的时间间隔 -->

  < property name = "timeToLiveSeconds" value = "3600" />

  <!-- 缓存回收策略,LRU表示移除近期使?最少的对象 -->

  < property name = "memoryStoreEvictionPolicy" value = "LRU" />

</ cache >

到此这篇关于Java关于MyBatis缓存详解的文章就介绍到这了,更多相关Java MyBatis 缓存内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/DrLai/article/details/119786974

查看更多关于Java关于MyBatis缓存详解的详细内容...

  阅读:18次