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 项目某些场景中,我们需要用缓存解决如热点数据访问的性能问题,业 ...
随机推荐
- 使用IntelliJ IDEA打开一个项目步骤
目录 1.普通java项目 步骤一.使用IDEA打开一个新的项目 步骤二.设置项目的SDK 步骤三.设置项目的src为Resources Root 2.Maven项目 前三步同普通java项目相同 ...
- @LoadBalanced注解原理
在使用springcloud ribbon客户端负载均衡的时候,可以给RestTemplate bean 加一个@LoadBalanced注解,就能让这个RestTemplate在请求时拥有客户端负载 ...
- 你的 GitHub 年度报告「GitHub 热点速览 v.22.52」
辞旧迎新的日子,又是年度报告满天飞的时候.GitHub 也不落其他平台之后,推出了用户 GitHub Contributions 报告.不知道,今年的你是不是比去年搬了更多的砖呢?在本期的 News ...
- S2-008
漏洞名称 S2-008(CVE-2012-0392) 远程代码执行漏洞 利用条件 Struts 2.0.0 - Struts 2.3.17 漏洞原理 S2-008 涉及多个漏洞,Cookie 拦截器错 ...
- 普冉PY32系列(三) PY32F002A资源实测 - 这个型号不简单
目录 普冉PY32系列(一) PY32F0系列32位Cortex M0+ MCU简介 普冉PY32系列(二) Ubuntu GCC Toolchain和VSCode开发环境 普冉PY32系列(三) P ...
- Java实现BP神经网络MNIST手写数字识别
Java实现BP神经网络MNIST手写数字识别 如果需要源码,请在下方评论区留下邮箱,我看到就会发过去 一.神经网络的构建 (1):构建神经网络层次结构 由训练集数据可知,手写输入的数据维数为784维 ...
- 用if语句替换三元运算符-标准的switch语句
用if语句替换三元运算符 在某些简单的应用中,if语句是可以和三元运算符互换使用的. public static void main(String[] args) { int a = 10; int ...
- 【分析笔记】Linux 4.9 backlight 子系统分析
相关信息 内核版本:Linux version 4.9.56 驱动文件:lichee\linux-4.9\drivers\video\backlight\backlight.c 驱动作用 对上,面对应 ...
- 编程哲学之 C# 篇:005——"Hello,World!"
如第一章说明,程序员的创造能力最接近于神,那么从现在开始,让我们来开始愉快地创造世界吧! 用解决方案来管理我们的创作 运行我们安装好的Visual Studio(以后一般情况下都会简称为VS),如下图 ...
- C-07\字符串的输入输出及常用操作函数
一.算法优化: 减少分支优化 // 求绝对值 int MyAbs(int n) { if (n < 0) { n = ~n + 1; } return n; } // 优化 int MyAbs( ...