一、application.yml配置
?
Spring boot,二、RedisService 配置
package com.example.song.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
spring整合redis集群?@Service
public class RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Resource(name="stringRedisTemplate")
private ValueOperations<String, String> valOpsStr;
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
@Resource(name="redisTemplate")
private ValueOperations<Object, Object> valOpsObj;
/**
*
* @author songyunfei
* @Title: getStr
* @Description: 根據指定的key獲取字符串
* @param key
* @return String
* @throws
*/
public String getStr(String key){
return valOpsStr.get(key);
}
/**
*
* @author songyunfei
* @Title: setStr
* @Description: 設置緩存
* @param key
* @param val void
* @throws
*/
public void setStr(String key,String val){
valOpsStr.set(key, val);
}
/**
*
* @author songyunfei
* @Title: del
* @Description: 刪除指定key的緩存
* @param key void
* @throws
*/
public void del(String key){
stringRedisTemplate.delete(key);
}
/**
*
* @author songyunfei
* @Title: getObj
* @Description: 根據指定的對象獲取到對象
* @param o
* @return Object
* @throws
*/
public Object getObj(Object o){
return valOpsObj.get(o);
}
/**
*
* @author songyunfei
* @Title: setObj
* @Description: 設置obj緩存
* @param o1
* @param o2 void
* @throws
*/
public void setObj(Object o1,Object o2){
valOpsObj.set(o1, o2);
}
/**
*
* @author songyunfei
* @Title: delObj
* @Description: 刪除Obj緩存
* @throws
*/
public void delObj(Object o){
redisTemplate.dump(o);
}
}
三、實體類
package com.example.song.domain.primary;
import java.io.Serializable;
redis和spring整合。import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Product implements Serializable {
/**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = -5628050431468774947L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="productId")
private Long productId;
@Column(name="prodName")
private String prodName;
/**
* @return the productId
*/
public Long getProductId() {
return productId;
}
redis解決的問題、 /**
* @param productId the productId to set
*/
public void setProductId(Long productId) {
this.productId = productId;
}
/**
* @return the prodName
*/
public String getProdName() {
return prodName;
}
/**
* @param prodName the prodName to set
*/
public void setProdName(String prodName) {
this.prodName = prodName;
}
}
redis項目,注意:?spring-data-redis默認使用的序列化方式為JdkSerializationRedisSerializer,所以實體類需要實現Serializable 接口,不然報錯
? ? ? ? ? ??java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.example.song.domain.primary.Product]
?