MyBatis整合EhCache

MyBatis对缓存的功能做了cache接口,可以对其实现,自己实现缓存,有一些第三方做了实现如redis-cache,ehcache-cache,memcached-cache 等等,下面使用ehcache做一个实例:

  • 添加maven包

  • 添加ehcache.xml配置文件

  • mapper.xml配置 使用

  • 测试

添加maven包

  <groupId>org.mybatis.caches</groupId>
      <artifactId>mybatis-ehcache</artifactId>
      <version>1.1.0</version>
   </dependency>

添加ehcache.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">

    <diskStore path="/Users/xiaobei/git-rep/emp/ehcache/"></diskStore>
    <!--      指定数据存储位置,可指定磁盘中的文件夹位置。样例中配置位置为“d:/ehcache/”-->

    <!-- 默认缓存配置 -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
    />

    <cache
        name="User"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
    />

</ehcache>

mapper.xml配置 使用

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="cn.fulong.mybatis.emp.dao.DeptMapper" >
    <!--    使用EhcacheCache缓存-->
    <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
    <!--    引用其他namespace的缓存-->
    <cache-ref namespace="cn.fulong.mybatis.emp.dao.EmployeeMapper"/>
</mapper>

测试

  /***
     * EhCache 缓存
     * @throws IOException
     */
    @Test
    public void testEhCache() throws IOException {
        // 读取类路径 config目录下的 mybatis-config.xml 配置文件 获取mybatis 全局配置  数据库连接 等信息
        String resource = "config/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 必须使用同一个  SqlSessionFactory 获取sqlsession
        SqlSessionFactory sessionFactory=  new SqlSessionFactoryBuilder().build(inputStream);
        // 获取一个 SqlSession
        SqlSession  openSession =sessionFactory.openSession();
        // 获取一个 SqlSession
        SqlSession  sqlSession  = sessionFactory.openSession();
        try {
            //  获取一个映射器
            DeptMapper mapper=  openSession.getMapper(DeptMapper.class);
            //  查询ID为3的部门的信息
            mapper.selectDeptById(3);
            /***
             * 1:手动关闭 sqlsession
             * 2:spring整合mybatis之后,通过动态代理的方式,使用SqlSessionTemplate持有的sqlSessionProxy属性来代理执行sql操作
             * 执行完毕后就自行关闭了sqlSession,不需要我们对其进行手动关闭
             */
            openSession.close();
            //  获取一个新的映射器
            DeptMapper mapperNew=  sqlSession.getMapper(DeptMapper.class);
            //  再次查询ID为3的部门的信息
            mapperNew.selectDeptById(3);
        } finally {
            // 关闭Session
            sqlSession.close();
        }
    }

测试结果

Created connection 205962452.
Returned connection 205962452 to pool.
Cache Hit Ratio [cn.fulong.mybatis.emp.dao.DeptMapper]: 0.0
Opening JDBC Connection
Checked out connection 205962452 from pool.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@c46bcd4]
==>  Preparing: select * from tbl_dept where id = ? 
==> Parameters: 3(Integer)
<==    Columns: id, dept_name
<==        Row: 3, 吴国
<==      Total: 1
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@c46bcd4]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@c46bcd4]
Returned connection 205962452 to pool.
# 缓存命中
Cache Hit Ratio [cn.fulong.mybatis.emp.dao.DeptMapper]: 0.5

Process finished with exit code 0