ssm+redis缓存配置
1、pom.xml引入一下jar
<!-- spring-redis实现 --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.6.2.RELEASE</version></dependency><!-- redis客户端jar --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.0</version></dependency><!-- Ehcache实现,用于参考 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-ehcache</artifactId><version>1.0.0</version></dependency>
2、application-context.xml配置
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 数据源定义 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:redis.properties</value></list> </property> </bean> <!-- redis数据源 --><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxIdle" value="${redis.maxIdle}" /><property name="maxTotal" value="${redis.maxActive}" /><property name="maxWaitMillis" value="${redis.maxWait}" /><property name="testOnBorrow" value="${redis.testOnBorrow}" /></bean><!-- Spring-redis连接池管理工厂 --><bean id="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"p:pool-config-ref="poolConfig" /><!-- 使用中间类解决RedisCache.jedisConnectionFactory的静态注入,从而使MyBatis实现第三方缓存 --><bean id="redisCacheTransfer" class="com.loan.security.cache.RedisCacheTransfer"><property name="jedisConnectionFactory" ref="jedisConnectionFactory" /></bean><!-- 数据源定义 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${connection.driverClassName}" /> <property name="url" value="${connection.url}" /> <property name="username" value="${connection.username}" /> <property name="password" value="${connection.password}" /> <property name="maxActive" value="${connection.maxActive}" /> <property name="maxIdle" value="${connection.maxIdle}" /> <property name="minIdle" value="${connection.minIdle}" /> <property name="removeAbandoned" value="${connection.removeAbandoned}" /> <property name="removeAbandonedTimeout" value="${connection.removeAbandonedTimeout}" /> <property name="logAbandoned" value="${connection.logAbandoned}" /> <property name="defaultAutoCommit" value="${connection.defaultAutoCommit}" /> <property name="defaultReadOnly" value="${connection.defaultReadOnly}" /> <property name="validationQuery" value="${connection.validationQuery}" /> <property name="testOnBorrow" value="${connection.testOnBorrow}" /> </bean> <!-- 创建sqlSessionFactory,同时指定数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- 自动扫描mapper目录, 省掉mybatis-config.xml里的手工配置 --> <property name="mapperLocations"> <list> <!-- <value>classpath:com/loan/*/dao/xml/*.xml</value> --> <value>classpath:com/loan/fore/dao/xml/*.xml</value> <value>classpath:com/loan/back/dao/xml/*.xml</value> <value>classpath:com/loan/security/dao/xml/*.xml</value> </list> </property> </bean> <!-- 通过扫描的模式,扫描目录在com/loan/mapper目录下 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.loan.*.dao.*" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <bean class="com.loan.util.SpringUtils" /> <!-- (事务管理) --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="load*" propagation="SUPPORTS" read-only="true" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="search*" propagation="SUPPORTS" read-only="true" /> <tx:method name="approve" propagation="REQUIRED" /> <tx:method name="undo" propagation="REQUIRED" /> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceMethod" expression="execution(* com.loan.*.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /> </aop:config> <!-- 自动搜索注解路径 --> <context:component-scan base-package="com.loan"></context:component-scan> <!-- 注册定时器 --> <bean id="timer" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" autowire="no"> <property name="triggers"> <list> <ref bean="timerTaskTrigger" /> </list> </property> </bean> <!-- 指定何时触发定时任务 --> <bean id="timerTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="timerTaskJobDetail" /> </property> <property name="cronExpression"> <!-- 每天凌晨3点执行 --> <value>0 0 3 * * ?</value> <!-- <value>20 * * * * ?</value> --> </property> </bean> <!-- 指定定时任务细节 调用哪个类 哪个方法 --> <bean id="timerTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="timerTaskInstance" /> </property> <property name="targetMethod"> <value>doTask</value> </property> <property name="concurrent" value="false" /> </bean> <!-- 实例化定时任务类 --> <bean id="timerTaskInstance" class="com.loan.security.plug.TimerTask"></bean>
</beans>
3、redis.properties
# Redis settings
redis.host=192.168.100.161
redis.port=6379
redis.pass=rootredis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
4、RedisCacheTransfer.java
package com.loan.security.cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;/*** * @描述: 静态注入中间类* @版权: Copyright (c) 2016 * @作者: xiad* @版本: 1.0 * @创建日期: 2016年3月2日 * @创建时间: 下午8:02:57*/
public class RedisCacheTransfer
{@Autowiredpublic void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {RedisCache.setJedisConnectionFactory(jedisConnectionFactory);}}
5、RedisCache.java
package com.loan.security.cache;import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;import org.apache.ibatis.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;import redis.clients.jedis.exceptions.JedisConnectionException;/*** * @描述: 使用第三方内存数据库Redis作为二级缓存* @版权: Copyright (c) 2016 * @作者: xiad* @版本: 1.0 * @创建日期: 2016年3月2日 * @创建时间: 下午8:02:57*/
public class RedisCache implements Cache
{private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);private static JedisConnectionFactory jedisConnectionFactory;private final String id;/*** The {@code ReadWriteLock}.*/private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();public RedisCache(final String id) {if (id == null) {throw new IllegalArgumentException("Cache instances require an ID");}logger.debug("MybatisRedisCache:id=" + id);this.id = id;}@Overridepublic void clear(){JedisConnection connection = null;try{connection = jedisConnectionFactory.getConnection();connection.flushDb();connection.flushAll();}catch (JedisConnectionException e){e.printStackTrace();}finally{if (connection != null) {connection.close();}}}@Overridepublic String getId(){return this.id;}@Overridepublic Object getObject(Object key){Object result = null;JedisConnection connection = null;try{connection = jedisConnectionFactory.getConnection();RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();result = serializer.deserialize(connection.get(serializer.serialize(key)));}catch (JedisConnectionException e){e.printStackTrace();}finally{if (connection != null) {connection.close();}}return result;}@Overridepublic ReadWriteLock getReadWriteLock(){return this.readWriteLock;}@Overridepublic int getSize(){int result = 0;JedisConnection connection = null;try{connection = jedisConnectionFactory.getConnection();result = Integer.valueOf(connection.dbSize().toString());}catch (JedisConnectionException e){e.printStackTrace();}finally{if (connection != null) {connection.close();}}return result;}@Overridepublic void putObject(Object key, Object value){JedisConnection connection = null;try{connection = jedisConnectionFactory.getConnection();RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();connection.set(serializer.serialize(key), serializer.serialize(value));}catch (JedisConnectionException e){e.printStackTrace();}finally{if (connection != null) {connection.close();}}}@Overridepublic Object removeObject(Object key){JedisConnection connection = null;Object result = null;try{connection = jedisConnectionFactory.getConnection();RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();result =connection.expire(serializer.serialize(key), 0);}catch (JedisConnectionException e){e.printStackTrace();}finally{if (connection != null) {connection.close();}}return result;}public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {RedisCache.jedisConnectionFactory = jedisConnectionFactory;}}
6、mapper.xml中引入Redis缓存
<?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.loan.security.dao.mapper.DictionaryCatalogMapper">
<cache type="com.loan.security.cache.RedisCache"/>
</mapper>
ssm+redis缓存配置相关推荐
- mysql redis缓存配置_SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
项目环境: 在SpringMVC + MyBatis + MySQL.Redis部署在Linux虚拟机. 1.整体思路 参考Ehcache实现MyBatis二级缓存代码(Maven引用对应jar查阅) ...
- 基于SSM + Redis的Shiro权限管理项目
概述 本教程结合SSM(SpringMVC + Mybatis)框架讲解Shiro,讲解的内容有自定义shiro拦截器,Shiro Freemarker标签,Shiro JSP标签,权限控制讲解. 详 ...
- spring-boot的spring-cache中的扩展redis缓存的ttl和key名
原文地址:spring-boot的spring-cache中的扩展redis缓存的ttl和key名 前提 spring-cache大家都用过,其中使用redis-cache大家也用过,至于如何使用怎么 ...
- redistemplate使用_SpringBoot 使用 Redis 缓存
1.pom.xml引入jar包,如下: org.springframework.boot spring-boot-starter-data-redis 2.修改项目启动类,增加注解@EnableCac ...
- Azure Redis 缓存使用注意事项与排查问题文档整理
StackExchange.Redis 使用名为 synctimeout 的配置设置进行同步操作,该设置的默认值为 1000 毫秒. 如果同步调用未在规定时间内完成,StackExchange.Red ...
- .Net内部缓存System.Web.Caching.Cache 和Redis缓存缓存工厂切换
有个问题,以前系统采用的是System.Web.Caching.Cache 但是redis缓存的流行 分布式的流行,缓存就被切换了. 但是在redis缓存的环境需要配置,有时候要切换回来. 这时候就弄 ...
- 使用Redis缓存Shiro授权认证信息,搭建集群权限系统
应用如果做负载均衡,集群间session需要共享,如果session没有共享,用户登录系统以后session保存在登录的应用里面,其他应用里面没有session,没有登陆状态,访问会失败.下面介绍一个 ...
- 第7章 集成Redis缓存
开心一笑 [跟老公冷战几天了,一句话也没说过. 早上老公是在忍不住了,跟我说:老婆,你的气消了吧. 我没理他, 他急了说:你生气这么多天了,就是充气的,也早就没气了. 我..] 新书购买 戳图购买 & ...
- Kyligence Enterprise 查询缓存配置
Kyligence Enterprise 查询缓存配置 SQL级缓存,SQL增加空白符号都会不命中. 为了提升执行相同查询的效率,Kyligence Enterprise 系统自带查询缓存并默认开启. ...
最新文章
- Loadrunner常见的乱码问题
- Sql 查询当天、本周、本月记录
- yunyang tensorflow-yolov3 Intel Realsense D435 (并发)调用两个摄像头运行识别程序并画框
- Apache网页优化概述
- javadoc提取工具_使JavaDoc保持最新状态的工具
- Timestamp 与 Date 变量绑定与Oracle的自动分区
- linux 网络端口全连接扫描,端口全连接扫描程序(Linux, socket):TCP的connect方式...
- Linux中的进程调度(六)
- leetcode955. Delete Columns to Make Sorted II
- 数据挖掘-二手车价格预测 Task04:建模调参
- Java程序员面试宝典--面向对象的基本概念
- excel vba 去重
- 【热门主题】蓝色妖姬电脑桌面主题
- 新手演讲:走上演讲台的第一步
- Mathematica学习(2)-mathematica命令
- WSO2 XMl转JSON
- 我的世界服务器怎么自己修改op权限,我的世界op权限指令
- mysql8 中的rank_Mysql8.0+中的rank()、row_num()、dense_rank()等窗口函数
- 批处理文件调用oracle实例
- 【MCS-51】51单片机指令系统大全
热门文章
- 惊魂一小时:全国域名解析首遭大规模污染
- Linux环境变量getenv setenv unsetenv操作
- Docker搭建持续集成平台Jenkins
- 医院需要什么样的集成平台
- 织梦选php哪个版本,织梦DedeCMS网站及其版本的判断
- linux ioctl 设备只读,linux – lsattr:设备的不适当的ioctl在读取标志时
- 习题 9.5 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
- Java计算1~n的和
- 远程控制网吧服务器,方便维修,电脑坏了不用愁,向日葵电脑远程维修省时又省力...
- 深度学习相关公开数据集