1.創建一個redis-context.xml文件:內容如下
<span style="font-size:18px;"><beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- scannerredis properties -->
<context:property-placeholderlocation="classpath:redis.properties"/>
<beanid="connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}"
p:port="${redis.port}"
p:password="${redis.password}"
p:pool-config-ref="poolConfig"/>
<beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
<propertyname="maxIdle"value="${redis.maxIdle}"/>
<propertyname="maxTotal"value="${redis.maxTotal}"/>
<propertyname="maxWaitMillis"value="${redis.maxWaitMillis}"/>
<propertyname="testOnBorrow"value="${redis.testOnBorrow}"/>
</bean>
<!-- <bean id = "connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg index="0"ref="poolConfig"/>
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="timeout" value="${redis.timeout}"/>
<property name="password" value="${redis.password}"/>
</bean> -->
<beanid="redisTemplate"class="org.springframework.data.redis.core.StringRedisTemplate">
<propertyname="connectionFactory" ref="connectionFactory"/>
<propertyname="keySerializer">
<beanclass="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<propertyname="valueSerializer">
<beanclass="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<!-- <property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property> -->
</bean>
</beans></span>
2.再創建一個redis.properties文件:內容如下
<span style="font-size:18px;"># Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.password=
redis.maxIdle=300
redis.maxTotal=600
redis.maxWaitMillis=1000
redis.testOnBorrow=true</span>
3.在springMVC-servlet.xml中添加下面內容
<!-- 引入同文件夾下的redis屬性配置文件 -->
<importresource="redis-context.xml"/>
4.新建java類文件,用於對redis操作
<span style="font-size:18px;">package com.pam.redis;
import java.io.Serializable;
import javax.annotation.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component("redisBase")
public class RedisBase{
@Resource(name="redisTemplate")
private RedisTemplate<Serializable, Serializable> redisTemplate;
/**
* 獲取數據
* @param key
* @return
*/
public String getInfo(final String key){
return (String) this.redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
byte[] keys = redisTemplate.getStringSerializer().serialize(key);
if (connection.exists(keys)) {
byte[] value = connection.get(keys);
String valueStr = redisTemplate.getStringSerializer().deserialize(value);
return valueStr;
}
return null;
}
});
}
/**
* 存儲數據(設定過期時間)
* @param key
* @param value
* @param timeout 單位秒,
*/
public void setAndUpdateInfo(String key,String value,int timeout){
this.redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.pSetEx(redisTemplate.getStringSerializer().serialize(key), 60*1000, redisTemplate.getStringSerializer().serialize(value));
return null;
}
});
}
/**
* 刪除數據
* @param key
*/
public void delete(String key){
this.redisTemplate.delete(key);
}
}</span>
注:內有已註釋內容