SSM项目集成Redis
1. 加入依赖
<!--redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- config redis data and client jar-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
2. 配置文件redis-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描redis配置文件-->
<context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties"/>
<!--设置连接池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="${redis.testOnReturn}" />
</bean>
<!--设置链接属性-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:hostName="${redis.host}"
p:port="${redis.port}"
p:password=""
p:pool-config-ref="poolConfig"
p:timeout="100000"/>
<!-- Jedis模板配置 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> </beans>
redis.properties
redis.host=192.168.181.130
redis.port=6379
redis.maxIdle=300
redis.maxWaitMillis=1000
redis.maxTotal=600
redis.testOnBorrow=true
redis.testOnReturn=true
在 Spring 的配置文件中引入 redis 的 xml 文件
<import resurce="classpath:redis-context.xml"/>
3. 测试
service:
@Service
public class RedisServiceImpl implements RedisService { @Autowired
private StringRedisTemplate redisTemplate; /*
* 将字符串类型的键值对保存到Redis时调用
* */
@Override
public Boolean saveNormalStringKeyValue(String normalKey, String normalValue, Integer timeoutMinute) {
// 获取字符串操作器对象
ValueOperations<String, String> operator = redisTemplate.opsForValue();
// 判断timeoutMinute值:是否为无效值
if(timeoutMinute == null || timeoutMinute == 0) {
return false;
}
// 判断timeoutMinute值:是否为不设置过期时间
if(timeoutMinute == -1) {
// 按照不设置过期时间的方式执行保存
try {
operator.set(normalKey, normalValue);
} catch (Exception e) {
e.printStackTrace();
return false;
}
// 返回结果
return true;
}
// 按照设置过期时间的方式执行保存
try {
operator.set(normalKey, normalValue, timeoutMinute, TimeUnit.MINUTES);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
} /**
* 根据key查询对应value时调用
*/
@Override
public String retrieveStringValueByStringKey(String normalKey) { try {
String value = redisTemplate.opsForValue().get(normalKey); return value; } catch (Exception e) {
e.printStackTrace(); return "";
}
} /**
* 根据key删除对应value时调用
*/
@Override
public Boolean removeByKey(String key) { try {
redisTemplate.delete(key); return true; } catch (Exception e) {
e.printStackTrace(); return false;
}
}
}
在 controller 中调用 service :
redisservice.xxx();
SSM项目集成Redis的更多相关文章
- ssm项目集成
ssm项目集成 说明:ssm指的是 spring + springMvc+ mybatis,是目前开发比较流行的集成方式,可以较好的实现前后端分离 spring与mybatis的集成,是通过配置文件a ...
- SSM框架集成Redis
SSM-Spring+SpringMVC+Mybatis框架相信大家都很熟悉了,但是有的时候需要频繁访问数据库中不变或者不经常改变的数据,就会加重数据库的负担,这时我们就会想起Redis Redis是 ...
- SpringBoot项目集成Redis
一.在pom文件中添加依赖 <!-- 集成redis --> <dependency> <groupId>org.springframework.boot</ ...
- 谷粒 | 项目集成redis
添加依赖 由于redis缓存是公共应用,所以我们把依赖与配置添加到了common模块下面,在common模块pom.xml下添加以下依赖 <!-- redis --> <depend ...
- Spring Boot 项目集成Redis
目录 集成方式 使用Jedis 使用spring-data-redis Redis的安装 绑定配置 获取Redis客户端 Redis工具的编写 使用 集成方式 使用Jedis Jedis是Redis官 ...
- Java项目集成Redis
1.项目pom.xml中添加Jedis依赖 <dependency> <groupId>redis.clients</groupId> <artifactId ...
- SSM项目集成Lucene+IKAnalyzer在Junit单元测试中执行异常
个人博客 地址:http://www.wenhaofan.com/article/20181108132519 问题描述 在项目运行以及main方法中能够正常运行,但是使用junit单元测试时却报如下 ...
- Spring Boot集成Redis集群(Cluster模式)
目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...
- ssm集成redis
身在一个传统的IT公司,接触的新技术比较少,打算年后跳槽,所以抽空学了一下redis. 简单的redis测试,咱们这边就不讲了,现在主要讲讲ssm集成redis的过程,因为现在项目用的就是ssm的框架 ...
- Spring Boot 项目实战(四)集成 Redis
一.前言 上篇介绍了接口文档工具 Swagger 及项目监控工具 JavaMelody 的集成过程,使项目更加健壮.在 JAVA Web 项目某些场景中,我们需要用缓存解决如热点数据访问的性能问题,业 ...
随机推荐
- [深度学习] RBM及DBN
转载于:http://blog.csdn.net/app_12062011/article/details/54313082 我们目前的讨论的神经网络,虽然学习算法不同,但基本上架构还是相同的,就是都 ...
- Dubbo架构设计与源码解析(二) 服务注册
作者:黄金 一.Dubbo简介 Dubbo是一款典型的高扩展.高性能.高可用的RPC微服务框架,用于解决微服务架构下的服务治理与通信问题.其核心模块包含 [RPC通信] 和 [服务治理] ,其中服务治 ...
- P1005 [NOIP2007 提高组] 矩阵取数游戏
题目传送门 前言 今天依旧是不写高精的一天呢!(是的,这位作者又只拿了开 \(LL\) 的 \(\color{yellow}{60}\) 分) 思路描述 看到数据 \(n,m \le 80(30)\) ...
- 通俗易懂angular搭建
- DLL的两种加载方式
案例简述 在某项目中,需要使用两个不同版本的HCNetSDK库,我们通常使用的静态加载DLL的方式不能满足该需求,故用到动态加载DLL的方式. 背景技术及术语解释 静态加载:也称隐式调用,指在运行程序 ...
- 12月8日内容总结——Django推导流程,Django模块的下载和基本使用、Django的应用和目录结构讲解、Django三板斧
目录 一.纯手撸web框架 二.基于wsgiref模块 三.代码封装优化 四.动静态网页 五.jinja2模块 六.前端.后端.数据库三者联动 七.python主流web框架 八.django简介 1 ...
- KStudio-Java程序连接KingbaseES数据库异常
错误信息: --KStudio客户端工具错误信息 The conncetion attempt failed.Reason:connect time out --Java应用程序控制台日志 Cause ...
- JAVA虚拟机16-方法的动态调用
更详细:https://www.cnblogs.com/jthr/p/15762527.html 1.子类重写父类方法 1.1父类 public class Father { public int n ...
- 1 .NET Core笔试题
1.说说显示实现接口和隐式实现接口的区别. 2.说说file访问修饰的作用. 3.说说什么是原始字符串. 4.C#10 中struct有什么改进? 5.说说C#10中Lambda表达式的新特点. 6. ...
- SpringCloud GateWay网关(入门)
1.介绍 强烈推荐,看官网文档 Spring Cloud Gateway ①简介 Cloud全家桶里有个重要组件:网关 SpringCloud Gateway基于WebFlux框架 WebFlux底层 ...