Spring 集成 Redis
Maven配置
redis.clients
jedis
2.3.1
org.springframework.data
spring-data-redis
1.5.0.RELEASE
org.slf4j
slf4j-log4j12
1.7.10
Spring 配置
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">
annotation-config="true" />
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
p:connection-factory-ref="jedisConnectionFactory">
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
p:connection-factory-ref="jedisConnectionFactory" />
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 redisTemplate;
public void save(Order order) {
/*redisTemplate.opsForList();
redisTemplate.opsForSet();
redisTemplate.opsForHash()*/
ValueOperations valueOper = redisTemplate.opsForValue();
valueOper.set(order.getId(), order);
}
public Order read(String id) {
ValueOperations valueOper = redisTemplate.opsForValue();
return valueOper.get(id);
}
public void delete(String id) {
ValueOperations valueOper = redisTemplate.opsForValue();
RedisOperations RedisOperations = valueOper.getOperations();
RedisOperations.delete(id);
}
}