Spring 整合 Redis
Maven配置
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.10</version>
</dependency>
Spring 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.slimsmart.redis.spring"
annotation-config="true" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="10"></property>
<property name="maxIdle" value="10"></property>
<property name="minIdle" value="2"></property>
<property name="maxWaitMillis" value="15000"></property>
<property name="minEvictableIdleTimeMillis" value="300000"></property>
<property name="numTestsPerEvictionRun" value="3"></property>
<property name="timeBetweenEvictionRunsMillis" value="60000"></property>
<property name="testOnBorrow" value="true"></property>
<property name="testOnReturn" value="true"></property>
<property name="testWhileIdle" value="true"></property>
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
<property name="hostName" value="192.168.100.205" />
<property name="port" value="6379" />
<property name="timeout" value="15000" />
<property name="database" value="0" />
<property name="password" value="" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<!-- redis template definition p表示對該bean裡面的屬性進行注入,格式為p:屬性名=注入的物件 效果與在bean裡面使用<property>標籤一樣 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory">
<!-- 序列化方式 建議key/hashKey採用StringRedisSerializer。 -->
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
<!-- 對string操作的封裝 -->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
p:connection-factory-ref="jedisConnectionFactory" />
<!-- <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate"/> -->
</beans>
Java程式碼
實體類
package cn.slimsmart.redis.spring.data.redis.demo;
import java.io.Serializable;
import java.util.Date;
public class Order implements Serializable{
private static final long serialVersionUID = 1L;
private String id;
private String orderNo;
private double price;
private Date createDate;
public Order(String id,String orderNo,double price,Date createDate){
this.id = id;
this.orderNo = orderNo;
this.price = price;
this.createDate = createDate;
}
public Order(){
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
redis操作類
package cn.slimsmart.redis.spring.data.redis.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
@Repository
public class OrderDao {
@Autowired
private RedisTemplate<String,Order> redisTemplate;
public void save(Order order) {
/*redisTemplate.opsForList();
redisTemplate.opsForSet();
redisTemplate.opsForHash()*/
ValueOperations<String, Order> valueOper = redisTemplate.opsForValue();
valueOper.set(order.getId(), order);
}
public Order read(String id) {
ValueOperations<String, Order> valueOper = redisTemplate.opsForValue();
return valueOper.get(id);
}
public void delete(String id) {
ValueOperations<String, Order> valueOper = redisTemplate.opsForValue();
RedisOperations<String,Order> RedisOperations = valueOper.getOperations();
RedisOperations.delete(id);
}
}