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

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-01-25 13:23:08
广东省委原常委、广州市委原书记郭永航,增补为广东省政协委员

广东省委原常委、广州市委原书记郭永航,增补为广东省政协委员

新京报政事儿
2026-01-24 20:20:24
2026年烟草大洗牌!国家出手后,买烟卖烟全变了,浑水摸鱼的凉了

2026年烟草大洗牌!国家出手后,买烟卖烟全变了,浑水摸鱼的凉了

老特有话说
2026-01-17 21:11:59
中国存在“两大威胁”,一个台湾,另一个则曾需百万大军才镇住

中国存在“两大威胁”,一个台湾,另一个则曾需百万大军才镇住

无情有思ss
2026-01-23 08:30:45
输不起?输掉冠军后,弗朗西斯卡向裁判投诉温瑞博发球遮挡

输不起?输掉冠军后,弗朗西斯卡向裁判投诉温瑞博发球遮挡

凤幻洋
2026-01-25 16:12:17
明确规定来了!机关事业单位职工下班后打牌打麻将,算违纪吗?

明确规定来了!机关事业单位职工下班后打牌打麻将,算违纪吗?

阿纂看事
2026-01-24 09:56:48
马斯克最新震撼发言:人类终有一天将找到逆转衰老之法

马斯克最新震撼发言:人类终有一天将找到逆转衰老之法

闪电新闻
2026-01-24 08:51:22
湖北省独生子女父母奖励:发放标准、申领流程、所需材料?看看!

湖北省独生子女父母奖励:发放标准、申领流程、所需材料?看看!

虎哥闲聊
2026-01-25 10:27:12
事出反常必有妖

事出反常必有妖

汉周读书
2025-12-24 15:00:03
央视揭秘“夺命红薯”!商家故意投毒,已蔓延全国多地,赶紧扔掉

央视揭秘“夺命红薯”!商家故意投毒,已蔓延全国多地,赶紧扔掉

阅微札记
2026-01-24 12:00:30
1月25日俄乌最新:俄罗斯开始妥协

1月25日俄乌最新:俄罗斯开始妥协

西楼饮月
2026-01-25 17:52:41
有存款100万,已经不是一般普通人了。

有存款100万,已经不是一般普通人了。

爱吃糖的猫cat
2026-01-12 19:08:47
风向有点不对了!市场开始喊出“去白银化”,难道风暴要来了吗?

风向有点不对了!市场开始喊出“去白银化”,难道风暴要来了吗?

流苏晚晴
2026-01-24 20:37:01
基辅可能是首个遭到反舰导弹袭击的首都!大俄向乌发射12枚Kh-22

基辅可能是首个遭到反舰导弹袭击的首都!大俄向乌发射12枚Kh-22

战刃
2026-01-25 17:54:10
牛起来了?日本宣布在群马县发现4种含稀土的新矿物

牛起来了?日本宣布在群马县发现4种含稀土的新矿物

随波荡漾的漂流瓶
2026-01-24 20:50:07
贝克汉姆家矛盾再升级!大布和父母彻底撕破脸,只想联系14岁妹妹小七

贝克汉姆家矛盾再升级!大布和父母彻底撕破脸,只想联系14岁妹妹小七

小鱼爱鱼乐
2026-01-25 11:46:18
再见了,沈腾,再见了,贾玲,2026年春晚“新小品演员”来势汹汹

再见了,沈腾,再见了,贾玲,2026年春晚“新小品演员”来势汹汹

真的八卦小学弟
2026-01-24 19:00:06
范元甄:曾是红极一时的“延安四美”,却因“太聪明”输掉了一生

范元甄:曾是红极一时的“延安四美”,却因“太聪明”输掉了一生

干史人
2026-01-23 11:48:39
特朗普知不知道,北极没有企鹅?他为什么P这么多图且“亲自当兵”?

特朗普知不知道,北极没有企鹅?他为什么P这么多图且“亲自当兵”?

新民周刊
2026-01-25 08:46:05
俄罗斯发动大规模袭击

俄罗斯发动大规模袭击

第一财经资讯
2026-01-25 11:13:08
2026-01-25 18:35:00
wRitchie
wRitchie
IT实用技术应用、JAVA
11文章数 12关注度
往期回顾 全部

财经要闻

隋广义等80人被公诉 千亿骗局进入末路

头条要闻

西安一道路车位紧俏 十多辆"老赖"车欠费最多者超2万

头条要闻

西安一道路车位紧俏 十多辆"老赖"车欠费最多者超2万

体育要闻

中国足球不会一夜变强,但他们已经创造历史

娱乐要闻

王玉雯方严正声明 剧方回应:涉事人员已被开除

科技要闻

黄仁勋在上海逛菜市场,可能惦记着三件事

汽车要闻

别克至境E7内饰图曝光 新车将于一季度正式发布

态度原创

亲子
艺术
健康
手机
公开课

亲子要闻

服了这妈妈

艺术要闻

当代唯一能称为“大师”的人,他的字普通人看不懂,启功跟他比,就像小学生!

耳石脱落为何让人天旋地转+恶心?

手机要闻

荣耀泡泡玛特联名手机今日开售:首销即引爆,线下再现排队热潮

公开课

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

无障碍浏览 进入关怀版