集合之前的ssm
引包
<!--redis相关-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<!--对象,json相互转化-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.6</version>
</dependency>
在resources下编写jedis.properties
# 设置最大连接数
REDIS.MAXTOTAL=50
#设置获取连接的最大等待时间
REDIS.MAXWAITMILLIS=1000
#开启获取连接时可用性校验,保证拿到的连接都是可用的
REDIS.TESTONBORROW=true
#设置IP
REDIS.HOSTNAME=localhost
#设置端口
REDIS.PORT=6379
#密码
REDIS.PASSWORD=默认没有密码
#设置是否使用连接池
REDIS.USEPOOL=true
编写applicationContext-redis.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--构建连接池配置信息-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--设置最大连接数-->
<property name="maxTotal" value="${REDIS.MAXTOTAL}"></property>
<!--设置获取连接的最大等待时间-->
<property name="maxWaitMillis" value="${REDIS.MAXWAITMILLIS}"></property>
<!--开启获取连接时可用性校验,保证拿到的连接都是可用的-->
<property name="testOnBorrow" value="${REDIS.TESTONBORROW}"></property>
</bean>
<!--构建Spring-data-redis连接工厂,其实就是连接池-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg index="0" ref="jedisPoolConfig"></constructor-arg>
<!--设置IP-->
<property name="hostName" value="${REDIS.HOSTNAME}"></property>
<!--设置端口-->
<property name="port" value="${REDIS.PORT}"></property>
<!--设置是否使用连接池-->
<property name="usePool" value="${REDIS.USEPOOL}"></property>
<!--设置密码默认是没有密码-->
<property name="password" value="${REDIS.PASSWORD}"></property>
</bean>
<!--stringRedisTemplate-->
<!-- String类型的RedisTemplate模板 -->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<constructor-arg index="0" ref="jedisConnectionFactory" />
</bean>
</beans>
将文件引入spring主配置文件
编写RedisService
package com.example.service;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* @author zhaozeren
* @version 1.0
* @date 2019/3/28
*/
@Service
public class RedisService {
@Autowired
private StringRedisTemplate redisTemplate;
@Resource(name = "stringRedisTemplate")
private ValueOperations<String, String> valueOperations;
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 执行set指令
*
* @param key
* @param value
* @throws JsonProcessingException
*/
public void set(String key, Object value) throws JsonProcessingException {
if(!(value instanceof String)){
value = MAPPER.writeValueAsString(value);
}
this.valueOperations.set(key, (String)value);
}
/**
* 执行set指令,设置生存时间
*
* @param key
* @param value
* @throws JsonProcessingException
*/
public void set(String key, Object value, Long seconds) throws JsonProcessingException {
if(!(value instanceof String)){
value = MAPPER.writeValueAsString(value);
}
this.valueOperations.set(key, (String)value, seconds, TimeUnit.SECONDS);
}
/**
* 执行get指令
*
* @param key
* @return
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
@SuppressWarnings("unchecked")
public <T> T get(String key, TypeReference<T> type) throws JsonParseException, JsonMappingException, IOException {
String json = this.valueOperations.get(key);
if(json == null){
return null;
}
if(type == null){
return (T) json;
}
return MAPPER.readValue(json, type);
}
/**
* 执行del命令
*
* @param key
*/
public void del(String key) {
this.redisTemplate.delete(key);
}
/**
* 设置过期时间
*
* @param key
* @param seconds
*/
public void expire(String key, Long seconds) {
this.redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
}
}