日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot整合redis 简单实现

發布時間:2024/1/18 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot整合redis 简单实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

主要邏輯代碼 restemplate使用resource注解才能正常使用

@Service
public class InforServiceImpl implements InforService {
?? ?@Autowired
?? ?private InforMapper inforMapper;
?? ?@Autowired? //通過類型查找,找不到就報錯
?? ?//@Resource //@resource通過name匹配,找不到報錯
?? ?private RedisTemplate<String, Infor> redisTemplate;

?? ?@Override
?? ?public Infor getById(BigInteger id) {
?? ??? ?Infor infor = null;
?? ??? ?//判斷是否在緩存中存在這個key,存在就從緩存中獲取
?? ??? ?if (redisTemplate.hasKey("infor" + id)) {
?? ??? ??? ?System.out.println("=============從緩存中獲取數據===========");
?? ??? ??? ?infor=(Infor) redisTemplate.opsForValue().get("infor"+id);
?? ??? ??? ?System.out.println("輸出從緩存中讀取的結果:"+infor);
?? ??? ?} else {
?? ??? ??? ?//不存在數據,將數據寫到緩存中去
?? ??? ??? ?infor = inforMapper.getById(id);
?? ??? ??? ?redisTemplate.opsForValue().set("infor" + id, infor);
?? ??? ??? ?System.out.println("=============將數據寫到緩存中去============");
?? ??? ?}
?? ??? ?return? infor;
?? ?}

}


沒修改出現的錯誤信息

Field redisTemplate in com.ys.study.service.impl.InforServiceImpl required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.

The injection point has the following annotations:
?? ?- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.

?


依賴

<parent>
?? ??? ?<groupId>org.springframework.boot</groupId>
?? ??? ?<artifactId>spring-boot-starter-parent</artifactId>
?? ??? ?<version>2.2.4.RELEASE</version>
?? ??? ?<relativePath /> <!-- lookup parent from repository -->
?? ?</parent>
?? ?<dependencies>
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.springframework.boot</groupId>
?? ??? ??? ?<artifactId>spring-boot-starter-web</artifactId>
?? ??? ?</dependency>

?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.springframework.boot</groupId>
?? ??? ??? ?<artifactId>spring-boot-starter-test</artifactId>
?? ??? ??? ?<scope>test</scope>
?? ??? ??? ?<exclusions>
?? ??? ??? ??? ?<exclusion>
?? ??? ??? ??? ??? ?<groupId>org.junit.vintage</groupId>
?? ??? ??? ??? ??? ?<artifactId>junit-vintage-engine</artifactId>
?? ??? ??? ??? ?</exclusion>
?? ??? ??? ?</exclusions>
?? ??? ?</dependency>
?? ??
?? ??
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.springframework.boot</groupId>
?? ??? ??? ?<artifactId>spring-boot-starter-data-redis</artifactId>
?? ??? ?</dependency>
?? ??? ?<!-- redis 緩存 -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.springframework.boot</groupId>
?? ??? ??? ?<artifactId>spring-boot-starter-cache</artifactId>
?? ??? ?</dependency>
?? ??? ?<!-- 連接數據庫配置 -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>mysql</groupId>
?? ??? ??? ?<artifactId>mysql-connector-java</artifactId>
?? ??? ??? ?<scope>runtime</scope>
?? ??? ?</dependency>
?? ??? ?<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.mybatis.spring.boot</groupId>
?? ??? ??? ?<artifactId>mybatis-spring-boot-starter</artifactId>
?? ??? ??? ?<version>1.3.2</version>
?? ??? ?</dependency>
?

?? ?</dependencies>

?? ?<build>
?? ??? ?<plugins>
?? ??? ??? ?<plugin>
?? ??? ??? ??? ?<groupId>org.springframework.boot</groupId>
?? ??? ??? ??? ?<artifactId>spring-boot-maven-plugin</artifactId>
?? ??? ??? ?</plugin>
?? ??? ?</plugins>
?? ?</build>

遇到的錯誤和解釋

總結

以上是生活随笔為你收集整理的springboot整合redis 简单实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。