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的更多相关文章

  1. ssm项目集成

    ssm项目集成 说明:ssm指的是 spring + springMvc+ mybatis,是目前开发比较流行的集成方式,可以较好的实现前后端分离 spring与mybatis的集成,是通过配置文件a ...

  2. SSM框架集成Redis

    SSM-Spring+SpringMVC+Mybatis框架相信大家都很熟悉了,但是有的时候需要频繁访问数据库中不变或者不经常改变的数据,就会加重数据库的负担,这时我们就会想起Redis Redis是 ...

  3. SpringBoot项目集成Redis

    一.在pom文件中添加依赖 <!-- 集成redis --> <dependency> <groupId>org.springframework.boot</ ...

  4. 谷粒 | 项目集成redis

    添加依赖 由于redis缓存是公共应用,所以我们把依赖与配置添加到了common模块下面,在common模块pom.xml下添加以下依赖 <!-- redis --> <depend ...

  5. Spring Boot 项目集成Redis

    目录 集成方式 使用Jedis 使用spring-data-redis Redis的安装 绑定配置 获取Redis客户端 Redis工具的编写 使用 集成方式 使用Jedis Jedis是Redis官 ...

  6. Java项目集成Redis

    1.项目pom.xml中添加Jedis依赖 <dependency> <groupId>redis.clients</groupId> <artifactId ...

  7. SSM项目集成Lucene+IKAnalyzer在Junit单元测试中执行异常

    个人博客 地址:http://www.wenhaofan.com/article/20181108132519 问题描述 在项目运行以及main方法中能够正常运行,但是使用junit单元测试时却报如下 ...

  8. Spring Boot集成Redis集群(Cluster模式)

    目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...

  9. ssm集成redis

    身在一个传统的IT公司,接触的新技术比较少,打算年后跳槽,所以抽空学了一下redis. 简单的redis测试,咱们这边就不讲了,现在主要讲讲ssm集成redis的过程,因为现在项目用的就是ssm的框架 ...

  10. Spring Boot 项目实战(四)集成 Redis

    一.前言 上篇介绍了接口文档工具 Swagger 及项目监控工具 JavaMelody 的集成过程,使项目更加健壮.在 JAVA Web 项目某些场景中,我们需要用缓存解决如热点数据访问的性能问题,业 ...

随机推荐

  1. react 高效高质量搭建后台系统 系列 —— antd和样式

    其他章节请看: react 高效高质量搭建后台系统 系列 antd 后续要做登录模块(主页),不仅要解决请求数据的问题,还需要完成 antd 配置以及样式的准备. antd 多种主题风格 详情请看 这 ...

  2. 前端Linux部署命令与流程记录

    以前写过一篇在Linux上从零开始部署前后端分离的Vue+Spring boot项目,但那时候是部署自己的个人项目,磕磕绊绊地把问题解决了,后来在公司有了几次应用到实际生产环境的经验,发现还有很多可以 ...

  3. Zotero自定义引文样式

    注意 在实际使用中发现还是有许多与要求不同的地方,之后会再次进行修改,特此记录 -----2022/11/28 16:57 目标格式: 期刊:[序号]作者.题名[J].刊名,出版年份,卷号 ( 期号 ...

  4. C++Vector源码解析(侯捷STL)

    vector是动态空间,随着元素的加入,它的内部机制会自行扩充空间以容纳新的元素.vector的实现技术,关键在于对大小的控制以及重新配置时的数据移动效率. 一.vector的数据结构 vector采 ...

  5. vue打包---放到服务器下(一个服务器多个项目需要配置路径),以及哈希模式和历史模式的不同配置方法

    哈希模式,好用,不需要服务器配合分配路径指向,自己单机就可以打开了 接下来上代码截图 接下来开始截图 历史模式 历史模式需要后端支持 打包后自己直接点击是打不开的 截图如下

  6. Vue3+TypeScript 项目中,配置 ESLint 和 Prettier

    接上篇:从0搭建vite-vue3-ts项目框架:配置less+svg+pinia+vant+axios 文档同步项目gitee:https://gitee.com/lixin_ajax/vue3-v ...

  7. 【分析笔记】展讯 RDA8810PL 平台 Camera 驱动分析和移植(Android 4.4 )

    前言概述 因以下原因,导致不得不通过代码分析来学习如何在该平台下进行摄像头驱动移植 香橙派开发商(迅龙软件)仅提供能跑起来的源代码.固件,以及简单的编译文档,不提供其它技术支持 baidu.googl ...

  8. Vue.js 前端项目在常见 Web 服务器上的部署配置

    Web 服务器是一种用于存储,处理和传输Web内容的软件.它是一种特殊类型的服务器,具有处理 HTTP 请求并向浏览器返回 Web 页面和其他内容的能力.Web服务器支持多种编程语言,如 PHP,Ja ...

  9. 浅谈Python中的if,可能有你不知道的

    Python中的if,没那么简单,虽然也不难 https://docs.python.org/zh-cn/3.9/reference/compound_stmts.html#if python语言参考 ...

  10. python导入xls数据到db--优化版

    import sys from orator import DatabaseManager import xlrd dbconfig = { 'mysql': { 'driver': 'mysql', ...