网易首页 > 网易号 > 正文 申请入驻

Spring 5.0.4+ SpringMVC +mybatis 3.4.5+Jedis 2.9.0集成

0
分享至


Spring 5.0.4+ SpringMVC +mybatis 3.4.5+Jedis 2.9.0集成之

--Spring+Jedis集成

来源: http://bj9420.com

作者:wRitchie(吴理琪)

概述:本文主要讲解Jedis与Spring的在SSM项目中的集成,至于ssm框架本文不赘述。

1、pom.xml


redis.clients
jedis
2.9.0
org.springframework.data
spring-data-redis
2.0.6.RELEASE
com.barchart.wrap
barchart-wrap-jackson
1.8.6-build001

导jar包后如图所示:

2、创建redis.properties

redis.host=127.0.0.1
redis.port=6379
redis.password=redis
redis.maxIdle=400
redis.maxTotal=6000
redis.maxWaitMillis=1000
redis.blockWhenExhausted=true
redis.testOnBorrow=true
redis.timeout=15000
defaultCacheExpireTime=60

注意:redis.properties值后不要有任何空格

3、创建spring-data-redis.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"
default-lazy-init="false">
value="${redis.blockWhenExhausted}" />
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
class="org.springframework.data.redis.core.RedisTemplate">
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>

4、在applicationContext.xml中引入spring-data-redis.xml文件:


class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
classpath:mybatis-dev.properties
classpath:redis.properties

5、集成完毕,测试类


* @Title: SpringRedisTest.java
* @Description: Redis测试类
* @author wRitchie
* @date 2018年4月19日 下午4:24:12
* @version V1.0
* @Copyright (c): 2018 www.bj9420.com All rights reserved.
package writchie;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
* @author wRitchie

public class SpringRedisTest {
private static ApplicationContext applicationContext;
static {
applicationContext = new
ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testRedisConnection() {
RedisTemplate redisTemplate = (RedisTemplate)
applicationContext.getBean("redisTemplate");
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
String username = (String) redisTemplate.opsForValue().get("username");
System.out.println("username:" + username);
redisTemplate.opsForValue().set("username", "wuliqi", 5,
TimeUnit.MINUTES);
String username = (String) redisTemplate.opsForValue().get("username");
username = (String) redisTemplate.opsForValue().get("username");
System.out.println("***username:" + username);

6、通用服务类

通用redis服务接口:


* @Title: IRedisServer.java
* @Description: Redis服务接口
* @author wRitchie
* @date 2018年4月19日 下午4:55:18
* @version V1.0
* @Copyright (c): 2018 www.bj9420.com All rights reserved.
package com.bj9420.service.redis;
* @author wRitchie

public interface IRedisService {
public String getCacheValue(String cacheKey);
public void setCacheValue(String key, String value);
public void setCacheValueForTime(String key, String value, long time);
public void delCacheByKey(String key);
public long getExpireTime(String key);
public long getExpireTimeBySeconds(String key);
public long getExpireTimeTypeByMinute(String key);
public void getInc(String key, Long growthLength);

通用redis服务实现例


* @Title: RedisServerImpl.java
* @Description: Redis服务实现类
* @author wRitchie
* @date 2018年4月19日 下午4:57:18
* @version V1.0
* @Copyright (c): 2018 bj9420.com All rights reserved.
package com.bj9420.service.redis.impl;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import cn.airbest.service.redis.IRedisService;
* @author wRitchie

@Service("redisService")
public class RedisServiceImpl implements IRedisService {
@Resource(name = "redisTemplate")
RedisTemplate redisTemplate;
* 获取缓存的地址
* @param key
* @return
public String getCacheValue(String key) {
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
String cacheValue = (String) redisTemplate.opsForValue().get(key);
return cacheValue;
/**设置缓存值
* @param key
* @param value
public void setCacheValue(String key, String value) {
redisTemplate.opsForValue().set(key, value);

* 设置缓存值并设置有效期
* @param key
* @param value
public void setCacheValueForTime(String key, String value, long time) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
/** 删除key值
* @param key
public void delCacheByKey(String key) {
redisTemplate.opsForValue().getOperations().delete(key);
redisTemplate.opsForHash().delete("");

* 获取token的有效期
* @param key
public long getExpireTime(String key) {
long time = redisTemplate.getExpire(key);
return time;

* 指定时间类型---秒
* @param key
* @return
public long getExpireTimeBySeconds(String key) {
long time = redisTemplate.getExpire(key, TimeUnit.SECONDS);
return time;

* @param key---分
* @return
public long getExpireTimeTypeByMinute(String key) {
long time = redisTemplate.getExpire(key, TimeUnit.MINUTES);
return time;

* 设置一个自增的数据
* @param key
* @param growthLength
public void getInc(String key, Long growthLength) {
redisTemplate.opsForValue().increment(key, growthLength);

7、在控制层Controller调用redis通用服务类

@Resource

private IRedisService redisService;

//1、设置缓存值并设置有效期

redisService.setCacheValueForTime("userName", "吴理琪", 60*10);//有效期10分钟

//2、设置缓存值

redisService.setCacheValue("userName", "吴理琪");//有效期10分钟

//3、获取缓存的值

String username =redisService.getCacheValue("userName");

说明:一般情况下,使用setCacheValue或setCacheValueForTime即可,将Java的Pojo对

象采用FastJson 转化为json字符串即。

特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。

Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.

相关推荐
热点推荐
普京:中国一贯寻求以和平方式解决问题,从不以施压或军事方式解决问题

普京:中国一贯寻求以和平方式解决问题,从不以施压或军事方式解决问题

环球网资讯
2026-10-02 10:33:10
C罗陷入绝望?无一名队友支持他:C罗离队没啥影响 我们力挺热苏斯

C罗陷入绝望?无一名队友支持他:C罗离队没啥影响 我们力挺热苏斯

风过乡
2026-10-02 09:19:32
刘欢妻子卢璐发声!她已回到北京家中,请求朋友辟谣,还老公安息

刘欢妻子卢璐发声!她已回到北京家中,请求朋友辟谣,还老公安息

动物奇奇怪怪
2026-10-01 05:43:37
严格!前NBA内线因入狱黑历史无缘签约上海男篮,大麦基因此获利

严格!前NBA内线因入狱黑历史无缘签约上海男篮,大麦基因此获利

林小湜体育频道
2026-10-01 22:55:43
9月销量出炉,又是“一米穿五界”!网友:余承东不必尴尬,华为乾崑ADS车型已经遍地开花

9月销量出炉,又是“一米穿五界”!网友:余承东不必尴尬,华为乾崑ADS车型已经遍地开花

大白聊IT
2026-10-02 22:29:11
1959年,印尼总统访日,日本人知道他好色,专门为他安排了一名19岁的绝色艺伎,没想到,就是这个女人,竟然改变了他的一生。

1959年,印尼总统访日,日本人知道他好色,专门为他安排了一名19岁的绝色艺伎,没想到,就是这个女人,竟然改变了他的一生。

回京历史梦
2026-10-01 06:35:14
曾是东方卫视王牌主持,如今落魄到无人知,嫁入豪门传闻真相大白

曾是东方卫视王牌主持,如今落魄到无人知,嫁入豪门传闻真相大白

圆梦的小老头
2026-10-02 19:02:43
孙颖莎不退出中国大满贯,不只是门票问题,主要原因是这个

孙颖莎不退出中国大满贯,不只是门票问题,主要原因是这个

余蹮搞笑段子
2026-10-02 20:00:16
全网吵翻!《现在就出发4》大换血,王楚然顶替金晨当唯一女常驻,争议背后藏太多无奈

全网吵翻!《现在就出发4》大换血,王楚然顶替金晨当唯一女常驻,争议背后藏太多无奈

阿废冷眼观察所
2026-10-02 19:30:36
一句话证明你在国企上班,网友:你太敢说了!

一句话证明你在国企上班,网友:你太敢说了!

另子维爱读史
2026-10-02 21:08:15
首日票房直破8000万,国庆档断崖领跑,陈思诚把悬疑片大门踹烂了

首日票房直破8000万,国庆档断崖领跑,陈思诚把悬疑片大门踹烂了

悬崖边上的爱情
2026-10-02 02:55:52
以色列警告客机袭击幕后者:如果是恐袭,躲到哪里都会被找到

以色列警告客机袭击幕后者:如果是恐袭,躲到哪里都会被找到

桂系007
2026-10-02 04:03:58
安踏,快被自己养大的“小弟”挤下主位了!

安踏,快被自己养大的“小弟”挤下主位了!

可乐爱微笑
2026-09-03 22:56:06
新型出轨,越来越流行了!

新型出轨,越来越流行了!

黯泉
2026-04-16 21:56:14
白白嫩嫩的我,也曾迷倒众千少年

白白嫩嫩的我,也曾迷倒众千少年

疾跑的小蜗牛
2026-10-02 20:15:36
王鹤棣爆下体坏死「首露面发声」! 亲口报平安:我的哥们儿很好

王鹤棣爆下体坏死「首露面发声」! 亲口报平安:我的哥们儿很好

ETtoday星光云
2026-09-30 09:49:38
悲惨!因辅导作业,情绪失控,母女相继跳楼,双双坠亡!

悲惨!因辅导作业,情绪失控,母女相继跳楼,双双坠亡!

闲侃闲侃
2026-06-25 07:43:49
中国柔道名将亚运会被罚下判负!咬对手手臂20秒不松口 牙印曝光

中国柔道名将亚运会被罚下判负!咬对手手臂20秒不松口 牙印曝光

念洲
2026-10-01 20:55:42
日本劫难开始,中方不救,特朗普没想到,轮到自己遭日本人骂

日本劫难开始,中方不救,特朗普没想到,轮到自己遭日本人骂

旧史新谭
2026-10-01 10:54:56
63岁世界冠军出局!倪夏莲致谢全场观众,深情告别WTT北京大满贯赛

63岁世界冠军出局!倪夏莲致谢全场观众,深情告别WTT北京大满贯赛

乒谈
2026-10-02 22:28:20
2026-10-02 23:07:00
wRitchie
wRitchie
IT实用技术应用、JAVA
11文章数 12关注度
往期回顾 全部

财经要闻

珠宝黑马爆雷,老板全家跑路泰国!

头条要闻

邵佳一回应"国足不敌巴勒斯坦":整个团队状态都不在线

头条要闻

邵佳一回应"国足不敌巴勒斯坦":整个团队状态都不在线

体育要闻

30天30队·快船:被莱纳德挂在半空的未来?

娱乐要闻

潘玮柏老婆被猜怀二胎 中秋vlog露馅

科技要闻

“分手”15天后,华为与赛力斯签约新五年

汽车要闻

方程豹9月热销破4万 首款皮卡鲨鱼将于四季度上市

态度原创

健康
游戏
家居
公开课
军事航空

刷酸祛痘,为什么有人翻车?

《星球大战》新作IGN8分!反重力赛车点燃沉寂领域

家居要闻

2026建博会(广州) 公装联探展交流活动

公开课

李玫瑾:为什么性格比能力更重要?

军事要闻

胡塞武装:24小时内沙特空袭也门47次

无障碍浏览 进入关怀版