MyBatis缓存

来源:互联网 发布:甘肃公务员网络培训app 编辑:程序博客网 时间:2024/04/29 23:33

一:MyBatis的一级缓存默认是开启的,但是如果MyBatis和Spring搭配后,一级缓存失效,如果想用一级缓存,加上@Transactional就可以了。




二:二级缓存:


        在mybatis.xml中加入<setting name="cacheEnabled" value="true" />  在PersonDao.xml中加入<cache />


<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC      "-//mybatis.org//DTD Config 3.0//EN"      "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><settings><setting name="cacheEnabled" value="true" /></settings><mappers><mapper resource="com/we/dao/PersonDao.xml" /></mappers></configuration>


<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.we.dao.PersonDao"><select id="get" parameterType="com.we.entry.Person" resultMap="result">select * from person WHERE id=#{id}</select><resultMap type="com.we.entry.Person" id="result"><id column="id" property="id" /><result column="name" property="name" /></resultMap><update id="updateById" parameterType="com.we.entry.Person">update person setname=#{name} where id=#{id}</update><cache /></mapper>


三:可以通过对比日志查看到缓存是否成功。如果不加<cache />和加则是以下两种


DEBUG 2016-08-29 20:08:09,499 org.mybatis.spring.SqlSessionUtils: Creating a new SqlSession
DEBUG 2016-08-29 20:08:09,499 org.mybatis.spring.SqlSessionUtils: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@253d9f73] was not registered for synchronization because synchronization is not active
DEBUG 2016-08-29 20:08:09,499 org.apache.ibatis.cache.decorators.LoggingCache: Cache Hit Ratio [com.we.dao.PersonDao]: 0.75
DEBUG 2016-08-29 20:08:09,499 org.mybatis.spring.SqlSessionUtils: Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@253d9f73]





2
DEBUG 2016-08-29 20:05:25,212 org.mybatis.spring.SqlSessionUtils: Creating a new SqlSession
DEBUG 2016-08-29 20:05:25,212 org.mybatis.spring.SqlSessionUtils: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1941a8ff] was not registered for synchronization because synchronization is not active
DEBUG 2016-08-29 20:05:25,212 org.springframework.jdbc.datasource.DataSourceUtils: Fetching JDBC Connection from DataSource
DEBUG 2016-08-29 20:05:25,212 org.mybatis.spring.transaction.SpringManagedTransaction: JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@2e1ef60] will not be managed by Spring
DEBUG 2016-08-29 20:05:25,212 org.apache.ibatis.logging.jdbc.BaseJdbcLogger: ==>  Preparing: select * from person WHERE id=? 
DEBUG 2016-08-29 20:05:25,212 org.apache.ibatis.logging.jdbc.BaseJdbcLogger: ==> Parameters: 1(Integer)
DEBUG 2016-08-29 20:05:25,228 org.apache.ibatis.logging.jdbc.BaseJdbcLogger: <==      Total: 1
DEBUG 2016-08-29 20:05:25,228 org.mybatis.spring.SqlSessionUtils: Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1941a8ff]
DEBUG 2016-08-29 20:05:25,228 org.springframework.jdbc.datasource.DataSourceUtils: Returning JDBC Connection to DataSource
3






四 配置Ehcache:加入jar包

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.0</version>
</dependency>
然后把上面的cacha换成下面两个的任意一个。可以看一下两个类的实现就可以发现,LoggingEhcache是对EhcacheCache的包装。(LoggingEhcache的构造方法)
     <cache type="org.mybatis.caches.ehcache.LoggingEhcache" />
     <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> 



五:配置redis缓存  

MyBatis配置redis需要实现org.apache.ibatis.cache.Cache接口,然后把四中的类改为自己写的类就行了。例子待续。




注意:在cache标签中还有好几个属性需要挖掘。比如说eviction是配置缓存策略。还有

<cache type="org.mybatis.caches.ehcache.LoggingEhcache"  eviction="缓存策略" blocking="" flushInterval="" readOnly="" size="" />

0 0
原创粉丝点击