1. 程式人生 > >mybatis學習之查詢快取

mybatis學習之查詢快取

編寫mapper.xml,配置tyoe為ehcache對cache介面的實現型別
    <!--
    開啟本mapper的namespace下的二級快取
    type:指定cache介面的實現類的型別,預設使用PerpetualCache,要和EhCache整合,需要配置type為ehcache實現cache介面的型別
     -->
    <cache type="org.mybatis.caches.ehcache.EhcacheCache" />

5、加入EhCache配置檔案

classpath下新增:ehcache.xml

內容如下:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
	<diskStore path="F:\develop\ehcache" />
	<defaultCache 
		maxElementsInMemory="1000" 
		maxElementsOnDisk="10000000"
		eternal="false" 
		overflowToDisk="false" 
		timeToIdleSeconds="120"
		timeToLiveSeconds="120" 
		diskExpiryThreadIntervalSeconds="120"
		memoryStoreEvictionPolicy="LRU">
	</defaultCache>
</ehcache>

6、測試程式碼

以上面的二級快取測試程式碼為例:
   //二級快取測試
    @Test
    public void testCache2() throws Exception{
        //建立SqlSession1與SqlSession2
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        SqlSession sqlSession3 = sqlSessionFactory.openSession();

        UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
        UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
        UserMapper userMapper3 = sqlSession3.getMapper(UserMapper.class);

        //第一次發起請求,查詢ID為29的使用者
        User user1 = userMapper1.findUserById(29);
        System.out.println(user1);
        //執行關閉操作,將sqlSession的資料寫到二級快取區域
        sqlSession1.close();

        //第三次發起請求,執行commit操作
        User user3 = userMapper3.findUserById(29);
        user3.setUsername("二級快取已更新");
        userMapper3.updateUser(user3);
        //執行提交操作,清空UserMapper下的二級快取
        sqlSession3.commit();
        //執行關閉操作,將sqlSession的資料寫到二級快取區域
        sqlSession3.close();

        //第二次發起請求,查詢ID為29的使用者
        User user2 = userMapper2.findUserById(29);
        System.out.println(user2);
        //執行關閉操作,將sqlSession的資料寫到二級快取區域
        sqlSession2.close();
    }



7、執行結果

DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - Class not found: org.jboss.vfs.VFS
DEBUG [main] - JBoss 6 VFS API is not available in this environment.
DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
DEBUG [main] - Reader entry: Items.class
DEBUG [main] - Reader entry: Order.class
DEBUG [main] - Reader entry: OrderCustom.class
DEBUG [main] - Reader entry: OrderDetail.class
DEBUG [main] - Reader entry: User.class
DEBUG [main] - Reader entry: UserCustom.class
DEBUG [main] - Reader entry: UserQueryVo.class
DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Items.class
DEBUG [main] - Reader entry: ����   4 W
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/Order.class
DEBUG [main] - Reader entry: ����   4 f
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderCustom.class
DEBUG [main] - Reader entry: ����   4 "
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/OrderDetail.class
DEBUG [main] - Reader entry: ����   4 J
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/User.class
DEBUG [main] - Reader entry: ����   4 ]
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserCustom.class
DEBUG [main] - Reader entry: ����   4 
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/pojo/UserQueryVo.class
DEBUG [main] - Reader entry: ����   4 %
DEBUG [main] - Checking to see if class pojo.Items matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.Order matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.OrderCustom matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.OrderDetail matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.User matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.UserCustom matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class pojo.UserQueryVo matches criteria [is assignable to Object]
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
DEBUG [main] - Reader entry: OrderMapperCustom.class
DEBUG [main] - Reader entry: OrderMapperCustom.xml
DEBUG [main] - Reader entry: UserMapper.class
DEBUG [main] - Reader entry: UserMapper.xml
DEBUG [main] - Listing file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.class
DEBUG [main] - Reader entry: ����   4    
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/OrderMapperCustom.xml
DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.class
DEBUG [main] - Reader entry: ����   4    findUserByIdResultMap (I)Lpojo/User; 
DEBUG [main] - Find JAR URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
DEBUG [main] - Not a JAR: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/mapper/UserMapper.xml
DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
DEBUG [main] - Checking to see if class mapper.OrderMapperCustom matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class mapper.UserMapper matches criteria [is assignable to Object]
DEBUG [main] - Configuring ehcache from ehcache.xml found in the classpath: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/ehcache.xml
DEBUG [main] - Configuring ehcache from URL: file:/E:/%e5%a4%a7%e5%ad%a6%e9%a1%b9%e7%9b%ae/IDEA%20Project/mybatis/out/production/mybatis/ehcache.xml
DEBUG [main] - Configuring ehcache from InputStream
DEBUG [main] - Ignoring ehcache attribute xmlns:xsi
DEBUG [main] - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
DEBUG [main] - Disk Store Path: F:\develop\ehcache
DEBUG [main] - Creating new CacheManager with default config
DEBUG [main] - propertiesString is null.
DEBUG [main] - No CacheManagerEventListenerFactory class specified. Skipping...
DEBUG [main] - No BootstrapCacheLoaderFactory class specified. Skipping...
DEBUG [main] - CacheWriter factory not configured. Skipping...
DEBUG [main] - No CacheExceptionHandlerFactory class specified. Skipping...
DEBUG [main] - Initialized net.sf.ehcache.store.NotifyingMemoryStore for mapper.UserMapper
DEBUG [main] - Initialised cache: mapper.UserMapper
DEBUG [main] - CacheDecoratorFactory not configured for defaultCache. Skipping for 'mapper.UserMapper'.
DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.0
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 631659383.
DEBUG [main] - Setting autocommit to false on JDBC Connection [
[email protected]
] DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id = ? DEBUG [main] - ==> Parameters: 29(Integer) DEBUG [main] - <== Total: 1 User{id=29, username='二級快取已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益陽', orderList=null} DEBUG [main] - Resetting autocommit to true on JDBC Connection [[email protected]] DEBUG [main] - Closing JDBC Connection [[email protected]] DEBUG [main] - Returned connection 631659383 to pool. DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.5 DEBUG [main] - Opening JDBC Connection DEBUG [main] - Checked out connection 631659383 from pool. DEBUG [main] - Setting autocommit to false on JDBC Connection [[email protected]] DEBUG [main] - ==> Preparing: UPDATE user SET username = ? , birthday = ? , sex = ? , address = ? where id = ? DEBUG [main] - ==> Parameters: 二級快取已更新(String), 2017-06-19 00:00:00.0(Timestamp), 1(String), 湖南益陽(String), 29(Integer) DEBUG [main] - <== Updates: 1 DEBUG [main] - Committing JDBC Connection [[email protected]] DEBUG [main] - Resetting autocommit to true on JDBC Connection [[email protected]] DEBUG [main] - Closing JDBC Connection [[email protected]] DEBUG [main] - Returned connection 631659383 to pool. DEBUG [main] - Cache Hit Ratio [mapper.UserMapper]: 0.3333333333333333 DEBUG [main] - Opening JDBC Connection DEBUG [main] - Checked out connection 631659383 from pool. DEBUG [main] - Setting autocommit to false on JDBC Connection [[email protected]] DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id = ? DEBUG [main] - ==> Parameters: 29(Integer) DEBUG [main] - <== Total: 1 User{id=29, username='二級快取已更新', sex='1', birthday=Mon Jun 19 00:00:00 CST 2017, address='湖南益陽', orderList=null} DEBUG [main] - Resetting autocommit to true on JDBC Connection [[email protected]] DEBUG [main] - Closing JDBC Connection [[email protected]] DEBUG [main] - Returned connection 631659383 to pool. Process finished with exit code 0