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字符串即。
热门跟贴