springboot (整合redis)

一、application.yml配置

二、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;

@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;

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;
}

/**
* @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;
}

}

注意: 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]

猜你喜欢

转载自www.cnblogs.com/muyarn/p/9234660.html