Mybatis配置分布式缓存

本文介绍了如何在Mybatis中整合Ehcache实现分布式缓存,通过配置Ehcache来管理和集中存储缓存数据,避免在各个服务中独立存储,提升系统开发效率。详细讲解了配置步骤,包括引入Ehcache jar包,修改User.xml配置以及在Java代码中的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分布式缓存

对缓存数据进行集中管理使用分布式缓存框架 redis、memcached、ehcache等。mybatis无法实现分布式缓存,需要和其它分布式缓存框架进行整合。

不使用分布缓存,缓存的数据在各各服务单独存储,不方便系统 开发。所以要使用分布式缓存对缓存数据进行集中管理。


分布式缓存工作图

mybatis本身来说是无法实现分布式缓存的,所以要与分布式缓存框架进行整合。

mybatis整合分布式缓存ehcache

首先加入ehcache的jar包


上图为jar包。

在User.xml中加入一行代码

配置缓存

[html]  view plain  copy
  1. <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>  
开启全局缓存配置

[html]  view plain  copy
  1. <settings>  
  2.         <setting name="cacheEnabled" value="true"/>  
  3. </settings>  
测试类

[java]  view plain  copy
  1. package cn.bj.mybatis.test;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.List;  
  6.   
  7. import org.apache.ibatis.io.Resources;  
  8. import org.apache.ibatis.session.SqlSession;  
  9. import org.apache.ibatis.session.SqlSessionFactory;  
  10. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  11. import cn.bj.mybatis.model.IUserOperation;  
  12. import cn.bj.mybatis.model.User;  
  13. import cn.bj.mybatis.model.UserCustom;  
  14. import cn.bj.mybatis.model.UserQueryVO;  
  15.   
  16. public class MybatisTest {  
  17.     public static void main(String[] args){  
  18.         SqlSessionFactory sqlSessionFactory = null;  
  19.         SqlSession session1 = null;  
  20.         SqlSession session2 = null;  
  21.         SqlSession session3 = null;  
  22.         SqlSession session4 = null;  
  23.         String resource = "Configuration.xml";  
  24.         InputStream inputStream;  
  25.         try {  
  26.             inputStream = Resources.getResourceAsStream(resource);  
  27.             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  
  28.             session1 = sqlSessionFactory.openSession();  
  29.             session2 = sqlSessionFactory.openSession();  
  30.             session3 = sqlSessionFactory.openSession();  
  31.             session4 = sqlSessionFactory.openSession();  
  32.             IUserOperation userOperation1 = (IUserOperation)session1.getMapper(IUserOperation.class);  
  33.             IUserOperation userOperation2 = (IUserOperation)session2.getMapper(IUserOperation.class);  
  34.             IUserOperation userOperation3 = (IUserOperation)session3.getMapper(IUserOperation.class);  
  35.             IUserOperation userOperation4 = (IUserOperation)session4.getMapper(IUserOperation.class);  
  36.               
  37.             User user1 = (User)userOperation1.selectUser(5);  
  38.             System.out.println(user1.getId());  
  39.             System.out.println(user1.getUsername());  
  40.             session1.close();  
  41.               
  42.             User user2 = (User)userOperation2.selectUser(5);  
  43.             user2.setAge(34);  
  44.             userOperation2.updateUser(user2);  
  45.             session2.commit();  
  46.             session2.close();  
  47.               
  48.               
  49.             User user3 = (User)userOperation3.selectUser(5);  
  50.             System.out.println(user3.getId());  
  51.             System.out.println(user3.getUsername());  
  52.             session3.close();  
  53.             User user4 = (User)userOperation4.selectUser(5);  
  54.             System.out.println(user4.getId());  
  55.             System.out.println(user4.getUsername());  
  56.             session4.close();  
  57.               
  58.         } catch (IOException e) {  
  59.             e.printStackTrace();  
  60.         }finally{  
  61.               
  62.         }  
  63.           
  64.     }  
  65.   
  66. }  

看输出效果:

[plain]  view plain  copy
  1. DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.  
  2. DEBUG [main] - PooledDataSource forcefully closed/removed all connections.  
  3. DEBUG [main] - PooledDataSource forcefully closed/removed all connections.  
  4. DEBUG [main] - PooledDataSource forcefully closed/removed all connections.  
  5. DEBUG [main] - PooledDataSource forcefully closed/removed all connections.  
  6. DEBUG [main] - Configuring ehcache from ehcache.xml found in the classpath: file:/D:/workspace/MyBatisTest/bin/ehcache.xml  
  7. DEBUG [main] - Configuring ehcache from URL: file:/D:/workspace/MyBatisTest/bin/ehcache.xml  
  8. DEBUG [main] - Configuring ehcache from InputStream  
  9. DEBUG [main] - Ignoring ehcache attribute xmlns:xsi  
  10. DEBUG [main] - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation  
  11. DEBUG [main] - Disk Store Path: F:\develop\ehcache  
  12. DEBUG [main] - Creating new CacheManager with default config  
  13. DEBUG [main] - propertiesString is null.  
  14. DEBUG [main] - No CacheManagerEventListenerFactory class specified. Skipping...  
  15. DEBUG [main] - No BootstrapCacheLoaderFactory class specified. Skipping...  
  16. DEBUG [main] - CacheWriter factory not configured. Skipping...  
  17. DEBUG [main] - No CacheExceptionHandlerFactory class specified. Skipping...  
  18. DEBUG [main] - Initialized net.sf.ehcache.store.NotifyingMemoryStore for cn.bj.mybatis.model.IUserOperation  
  19. DEBUG [main] - Initialised cache: cn.bj.mybatis.model.IUserOperation  
  20. DEBUG [main] - CacheDecoratorFactory not configured for defaultCache. Skipping for 'cn.bj.mybatis.model.IUserOperation'.  
  21. DEBUG [main] - <span style="color:#ff0000;">Cache Hit Ratio [cn.bj.mybatis.model.IUserOperation]: 0.0</span>  
  22. DEBUG [main] - Opening JDBC Connection  
  23. DEBUG [main] - Created connection 28235257.  
  24. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]  
  25. DEBUG [main] - ==>  Preparing: select * from t_user where id = ?   
  26. DEBUG [main] - ==> Parameters: 5(Integer)  
  27. DEBUG [main] - <==      Total: 1  
  28. 5  
  29. kksskdk  
  30. DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]  
  31. DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]  
  32. DEBUG [main] - Returned connection 28235257 to pool.  
  33. DEBUG [main] - <span style="color:#ff6666;">Cache Hit Ratio [cn.bj.mybatis.model.IUserOperation]: 0.5</span>  
  34. DEBUG [main] - Opening JDBC Connection  
  35. DEBUG [main] - Checked out connection 28235257 from pool.  
  36. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]  
  37. DEBUG [main] - ==>  Preparing: update t_user set username=?,age=? where id=?   
  38. DEBUG [main] - ==> Parameters: kksskdk(String), 34(Integer), 5(Integer)  
  39. DEBUG [main] - <==    Updates: 1  
  40. DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]  
  41. DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]  
  42. DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]  
  43. DEBUG [main] - Returned connection 28235257 to pool.  
  44. DEBUG [main] - <span style="color:#ff6666;">Cache Hit Ratio [cn.bj.mybatis.model.IUserOperation]: 0.3333333333333333</span>  
  45. DEBUG [main] - Opening JDBC Connection  
  46. DEBUG [main] - Checked out connection 28235257 from pool.  
  47. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]  
  48. DEBUG [main] - ==>  Preparing: select * from t_user where id = ?   
  49. DEBUG [main] - ==> Parameters: 5(Integer)  
  50. DEBUG [main] - <==      Total: 1  
  51. 5  
  52. kksskdk  
  53. DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]  
  54. DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]  
  55. DEBUG [main] - Returned connection 28235257 to pool.  
  56. DEBUG [main] - Cache Hit Ratio [cn.bj.mybatis.model.IUserOperation]: 0.5  
  57. 5  
  58. kksskdk  

命中率不小的。






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值