转载:http://blog.csdn.net/xiadi934/article/details/50786293

项目环境: 在SpringMVC +Spring + MyBatis + MySQL。Redis部署在Linux虚拟机。

1、整体思路

  • 参考Ehcache实现MyBatis二级缓存代码(Maven引用对应jar查阅)
  • 使用Spring管理Redis连接池
  • 模仿EhcacheCache,实现RedisCache

2、pom.xml中加入Maven依赖

 1 <!-- spring-redis实现 -->
2 <dependency>
3 <groupId>org.springframework.data</groupId>
4 <artifactId>spring-data-redis</artifactId>
5 <version>1.6.2.RELEASE</version>
6 </dependency>
7 <!-- redis客户端jar -->
8 <dependency>
9 <groupId>redis.clients</groupId>
10 <artifactId>jedis</artifactId>
11 <version>2.8.0</version>
12 </dependency>
13 <!-- Ehcache实现,用于参考 -->
14 <dependency>
15 <groupId>org.mybatis</groupId>
16 <artifactId>mybatis-ehcache</artifactId>
17 <version>1.0.0</version>
18 </dependency>

3、引入applicationContext.xml中引入redis配置

 1 <!-- 引入数据库配置文件 -->
2 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
3 <property name="locations">
4 <list>
5 <value>classpath:jdbc.properties</value>
6 <value>classpath:redis.properties</value>
7 </list>
8 </property>
9 </bean>
10 <!-- redis数据源 -->
11 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
12 <property name="maxIdle" value="${redis.maxIdle}" />
13 <property name="maxTotal" value="${redis.maxActive}" />
14 <property name="maxWaitMillis" value="${redis.maxWait}" />
15 <property name="testOnBorrow" value="${redis.testOnBorrow}" />
16 </bean>
17 <!-- Spring-redis连接池管理工厂 -->
18 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
19 p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
20 <!-- 使用中间类解决RedisCache.jedisConnectionFactory的静态注入,从而使MyBatis实现第三方缓存 -->
21 <bean id="redisCacheTransfer" class="com.strive.cms.cache.RedisCacheTransfer">
22 <property name="jedisConnectionFactory" ref="jedisConnectionFactory"/>
23 </bean>

4、创建缓存实现类RedisCache

  1 /**
2 *
3 * @描述: 使用第三方内存数据库Redis作为二级缓存
4 * @版权: Copyright (c) 2016
5 * @作者: xiad
6 * @版本: 1.0
7 * @创建日期: 2016年3月2日
8 * @创建时间: 下午8:02:57
9 */
10 public class RedisCache implements Cache
11 {
12 private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);
13
14 private static JedisConnectionFactory jedisConnectionFactory;
15
16 private final String id;
17
18 /**
19 * The {@code ReadWriteLock}.
20 */
21 private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
22
23 public RedisCache(final String id) {
24 if (id == null) {
25 throw new IllegalArgumentException("Cache instances require an ID");
26 }
27 logger.debug("MybatisRedisCache:id=" + id);
28 this.id = id;
29 }
30
31 @Override
32 public void clear()
33 {
34 JedisConnection connection = null;
35 try
36 {
37 connection = jedisConnectionFactory.getConnection();
38 connection.flushDb();
39 connection.flushAll();
40 }
41 catch (JedisConnectionException e)
42 {
43 e.printStackTrace();
44 }
45 finally
46 {
47 if (connection != null) {
48 connection.close();
49 }
50 }
51 }
52
53 @Override
54 public String getId()
55 {
56 return this.id;
57 }
58
59 @Override
60 public Object getObject(Object key)
61 {
62 Object result = null;
63 JedisConnection connection = null;
64 try
65 {
66 connection = jedisConnectionFactory.getConnection();
67 RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
68 result = serializer.deserialize(connection.get(serializer.serialize(key)));
69 }
70 catch (JedisConnectionException e)
71 {
72 e.printStackTrace();
73 }
74 finally
75 {
76 if (connection != null) {
77 connection.close();
78 }
79 }
80 return result;
81 }
82
83 @Override
84 public ReadWriteLock getReadWriteLock()
85 {
86 return this.readWriteLock;
87 }
88
89 @Override
90 public int getSize()
91 {
92 int result = 0;
93 JedisConnection connection = null;
94 try
95 {
96 connection = jedisConnectionFactory.getConnection();
97 result = Integer.valueOf(connection.dbSize().toString());
98 }
99 catch (JedisConnectionException e)
100 {
101 e.printStackTrace();
102 }
103 finally
104 {
105 if (connection != null) {
106 connection.close();
107 }
108 }
109 return result;
110 }
111
112 @Override
113 public void putObject(Object key, Object value)
114 {
115 JedisConnection connection = null;
116 try
117 {
118 connection = jedisConnectionFactory.getConnection();
119 RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
120 connection.set(serializer.serialize(key), serializer.serialize(value));
121 }
122 catch (JedisConnectionException e)
123 {
124 e.printStackTrace();
125 }
126 finally
127 {
128 if (connection != null) {
129 connection.close();
130 }
131 }
132 }
133
134 @Override
135 public Object removeObject(Object key)
136 {
137 JedisConnection connection = null;
138 Object result = null;
139 try
140 {
141 connection = jedisConnectionFactory.getConnection();
142 RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
143 result =connection.expire(serializer.serialize(key), 0);
144 }
145 catch (JedisConnectionException e)
146 {
147 e.printStackTrace();
148 }
149 finally
150 {
151 if (connection != null) {
152 connection.close();
153 }
154 }
155 return result;
156 }
157
158 public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
159 RedisCache.jedisConnectionFactory = jedisConnectionFactory;
160 }
161
162 }

5、创建中间类RedisCacheTransfer,完成RedisCache.jedisConnectionFactory的静态注入

 1 /**
2 *
3 * @描述: 静态注入中间类
4 * @版权: Copyright (c) 2016
5 * @作者: xiad
6 * @版本: 1.0
7 * @创建日期: 2016年3月2日
8 * @创建时间: 下午8:02:57
9 */
10 public class RedisCacheTransfer
11 {
12
13 @Autowired
14 public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
15 RedisCache.setJedisConnectionFactory(jedisConnectionFactory);
16 }
17
18 }

6、配置文件redis.properties

1 # Redis settings
2 redis.host=192.168.25.132
3 redis.port=6379
4 redis.pass=
5
6 redis.maxIdle=300
7 redis.maxActive=600
8 redis.maxWait=1000
9 redis.testOnBorrow=true

7、mapper中加入MyBatis二级缓存

<mapper namespace="com.strive.cms.dao.site.CatalogMapper" >
<cache type="com.strive.cms.cache.RedisCache"/>
.....
</mapper>

8、Mybatis全局配置

 1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE configuration
3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 <configuration>
6 <!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->
7 <settings>
8
9 <!-- 全局映射器启用缓存 -->
10 <setting name="cacheEnabled" value="true"/>
11
12 <!-- 查询时,关闭关联对象即时加载以提高性能 -->
13 <setting name="lazyLoadingEnabled" value="false"/>
14
15 <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
16 <setting name="multipleResultSetsEnabled" value="true"/>
17
18 <!-- 允许使用列标签代替列名 -->
19 <setting name="useColumnLabel" value="true"/>
20
21 <!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
22 <setting name="useGeneratedKeys" value="false"/>
23
24 <!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL -->
25 <setting name="autoMappingBehavior" value="PARTIAL"/>
26
27 <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE -->
28 <!-- <setting name="defaultExecutorType" value="BATCH" /> -->
29
30 <!-- 数据库超过25000秒仍未响应则超时 -->
31 <!-- <setting name="defaultStatementTimeout" value="25000" /> -->
32
33 <!-- Allows using RowBounds on nested statements -->
34 <setting name="safeRowBoundsEnabled" value="false"/>
35
36 <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. -->
37 <setting name="mapUnderscoreToCamelCase" value="true"/>
38
39 <!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT
40 local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->
41 <setting name="localCacheScope" value="SESSION"/>
42
43 <!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values
44 like NULL, VARCHAR or OTHER. -->
45 <setting name="jdbcTypeForNull" value="OTHER"/>
46
47 <!-- Specifies which Object's methods trigger a lazy load -->
48 <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
49
50 <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
51 <setting name="aggressiveLazyLoading" value="true"/>
52
53 </settings>
54
55 </configuration>

9、打印Sql日志,方便测试

 1 #定义LOG输出级别为INFO
2 log4j.rootLogger=INFO,Console,File
3
4 ####定义日志输出目的地为控制台
5 log4j.appender.Console=org.apache.log4j.ConsoleAppender
6 log4j.appender.Console.Target=System.out
7 #可以灵活地指定日志输出格式,下面一行是指定具体的格式
8 log4j.appender.Console.layout = org.apache.log4j.PatternLayout
9 log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
10
11 ####文件大小到达指定尺寸的时候产生一个新的文件
12 log4j.appender.File = org.apache.log4j.RollingFileAppender
13 #指定输出目录
14 log4j.appender.File.File = logs/ssm.log
15 #定义文件最大大小
16 log4j.appender.File.MaxFileSize = 10MB
17 #输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
18 log4j.appender.File.Threshold = ALL
19 log4j.appender.File.layout = org.apache.log4j.PatternLayout
20 log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
21
22 ####显示本项目SQL语句部分
23 log4j.logger.com.strive.cms=DEBUG

10、测试代码

 1 @RunWith(SpringJUnit4ClassRunner.class)
2 @ContextConfiguration(locations = {"classpath:applicationContext.xml"})
3 public class MyBatisCacheSecondTest
4 {
5 private static final Logger logger = LoggerFactory.getLogger(MyBatisCacheSecondTest.class);
6
7 @Autowired
8 private SiteService service;
9
10 /*
11 * 二级缓存测试
12 */
13 @Test
14 public void testCache2() {
15 PageInfo<Site> page1 = service.querySite("", 1, 2, "", "");
16 logger.info(page1.getList().get(1).getName());
17
18 PageInfo<Site> page2 = service.querySite("", 2, 2, "", "");
19 logger.info(page2.getList().get(0).getName());
20
21 PageInfo<Site> page3 = service.querySite("", 1, 2, "", "");
22 logger.info(page3.getList().get(0).getName());
23 }
24
25 }

首次运行结果 
 
后续运行结果 
 
同条件的查询语句可以发现,已经不再查询Mysql,而是直接取Redis数据 
查看Redis数据库 keys *, 会发现多了很多数据,结果如下 
 
至此,Redis基本配置成功。

SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置的更多相关文章

  1. SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置

    2016年03月03日 10:37:47 标签: mysql / redis / mybatis / spring mvc / spring 33805 项目环境: 在SpringMVC + MyBa ...

  2. mybatis 使用redis实现二级缓存(spring boot)

    mybatis 自定义redis做二级缓存 前言 如果关注功能实现,可以直接看功能实现部分 何时使用二级缓存 一个宗旨---不常变的稳定而常用的 一级是默认开启的sqlsession级别的. 只在单表 ...

  3. SpringMVC+Spring+Mybatis+Mysql项目搭建

    眼下俺在搭建一个自己的个人站点玩玩.一边练习.一边把用到的技术总结一下,日后好复习. 站点框架大致例如以下图所看到的: 眼下仅仅用到了SpringMVC+Spring+Mybatis+Mysql.把它 ...

  4. Mybatis整合Redis实现二级缓存

    Mybatis集成ehcache . 为什么需要缓存 拉高程序的性能 . 什么样的数据需要缓存 很少被修改或根本不改的数据 业务场景比如:耗时较高的统计分析sql.电话账单查询sql等 . ehcac ...

  5. springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

    @_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...

  6. maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

    转自:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral @_@ 写在最前 之 ...

  7. springboot+mybatis 用redis作二级缓存

    1.加入相关依赖包: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  8. mybatis结合redis实战二级缓存(六)

    之前的文章中我们意见分析了一级缓存.二级缓存的相关源码和基本原理,今天我们来分享下了mybatis二级缓存和redis的结合,当然mybatis二级缓存也可以和ehcache.memcache.OSC ...

  9. SpringBank 开发日志 Mybatis 使用redis 作为二级缓存时,无法通过cacheEnabled=false 将其关闭

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...

随机推荐

  1. Jmeter之cookie的处理方式,token处理

    cookie是什么 由于http是无状态的协议,一旦客户端和服务器的数据交换完毕,就会断开连接,再次请求,会重新连接,这就说明服务器单从网络连接上是没有办法知道用户身份的.怎么办呢?那就给每次新的用户 ...

  2. CSS3参数matrix(n,n,n,n,n,n)定义 2D 转换,使用六个值的矩阵。那这六个值分别代表了什么参数?

    matrix( a, b, c, d, e, f );a 水平缩放b 水平倾斜c 垂直倾斜d 垂直缩放e 水平移动f 垂直移动

  3. Java 缓存池(使用Map实现)

    之前只是听说过缓存池,也没有具体的接触到,今天做项目忽然想到了用缓存池,就花了一上午的时间研究了下缓存池的原理,并实现了基本的缓存池功能. /** * 缓存池 * @author xiaoquan * ...

  4. PHP三种字符串界定符的区别(单引号,双引号,<<<)

      单引号,双引号,<<<的区别如下: 前续:今天突然遇到了<<<EOT,可在运行的时候出错了,所以就度娘了下. 1.单引号:’a string’  \’是唯一的转 ...

  5. mac下phpize编译提示Cannot find autoconf解决办法

    mac下phpize编译如下报错: /usr/bin/phpizeConfiguring for:PHP Api Version: 20121113Zend Module Api No: 201212 ...

  6. linux 最大文件打开数

    配置文件 vim /etc/security/limits.conf   # /etc/security/limits.conf##This file sets the resource limits ...

  7. ZROI 19.08.01 树上数据结构

    1.总览 LCT 链分治(树剖) 点/边分治 2.点分治 一棵树,点有\(0/1\),多次修改,询问最远的两个\(1\)距离. 建出点分树,每个子树用堆维护:①最远的\(1\)距离:②它的每个儿子的① ...

  8. 【GDKOI2013选拔】大LCP

    题目 LCP就是传说中的最长公共前缀,至于为什么要加上一个大字,那是因为-你会知道的. 首先,求LCP就要有字符串.既然那么需要它们,那就给出n个字符串好了. 于是你需要回答询问大LCP,询问给出一个 ...

  9. spark的accumulator值保存在哪里?

    答案:保存在driver端.因此需要对收集的信息的规模要加以控制,不宜过大.避免 driver端的outofmemory问题!!!

  10. mysql——InnoDB 锁

    https://www.cnblogs.com/leedaily/p/8378779.html 1.InnoDB锁的实现方式:给索引项加锁,只有通过索引条件检索数据,InnoDB才使用行级锁,否则,I ...