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 项目某些场景中,我们需要用缓存解决如热点数据访问的性能问题,业 ...
随机推荐
- 【深入浅出Sentinel原理及实战】「基础实战专题」零基础实现服务流量控制实战开发指南(2)
你若要喜爱你自己的价值,你就得给世界创造价值. Sentinel的组成部分 Sentinel 主要由以下两个部分组成. Sentinel核心库(Java客户端) :Sentinel的核心库不依赖任何框 ...
- Netty-架构设计及入门程序-3
一.原生 NIO 存在的问题 1.NIO 的类库和 API 繁杂,使用麻烦:需要熟练掌握 Selector.ServerSocketChannel.SocketChannel.ByteBuffer等. ...
- vs code .net core Linux下离线安装Nuget包
本人第一次使用 vs code在linux下开发.net core项目,由于处于内网,无法通过在线安装,所以在遇见离线安装Nuget包时,耗费了一番功夫,网上也没有相关的,最后还是多个思路结合才解决的 ...
- python进阶之路15 之异常处理、生成器相关
异常捕获处理 1.异常 异常就是代码运行报错 行业术语叫bug 代码运行中一旦遇到异常会直接结束整个程序的运行 我们在编写代码的过程中要尽可能避免 2.异常分类 语法错误 不允许出现 一旦出现立刻改正 ...
- [C#]简单的理解委托和事件
委托 在C++中可以利用"函数指针"将对方法的引用作为实参传递给另一个方法,而C#中可以利用委托提供相同的功能. 委托-内部机制 但是委托实际上是一个特殊的类.委托必须直接或间接的 ...
- [cocos2d-x]关于动画
声明一下:看见这篇文章总结的已经非常好了,没必要再去自己到处东翻西找了,链接:http://shahdza.blog.51cto.com/2410787/1546998 [唠叨] 基本动画制作需要用到 ...
- vue学习笔记(一)---- vue指令( v-on 事件绑定 )
Vue 中提供了 v-on: 事件绑定机制 绑定的事件处理函数必须定义到vm实例的事件处理函数 methods 中去 <div id="app"> <!-- &l ...
- [DuckDB] 多核算子并行的源码解析
DuckDB 是近年来颇受关注的OLAP数据库,号称是OLAP领域的SQLite,以精巧简单,性能优异而著称.笔者前段时间在调研Doris的Pipeline的算子并行方案,而DuckDB基于论文< ...
- 线程基础知识17 Quene
1 ConcurrentLinkedQueue 1.1 简介 它是一个基于链接节点的无界线程安全队列.此队列按照 FIFO(先进先出)原则对元素进行排序. 新的元素插入到队列的尾部,队列获取操作从队列 ...
- 关于我在学习LFU的时候,在开源项目捡了个漏这件事。
你好呀,我是歪歪. 这篇文章带大家盘一下 LFU 这个玩意. 为什么突然想起聊聊这个东西呢,因为前段时间有个读者给我扔过来一个链接: 我一看,好家伙,这不是我亲爱的老朋友,Dubbo 同学嘛. 点进去 ...