Spring boot,spring boot (整合redis)

 2023-10-21 阅读 31 评论 0

摘要:一、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

一、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]

?

轉載于:https://www.cnblogs.com/muyarn/p/9234660.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/155046.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息