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 简单实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IT审计如何生存
- 下一篇: WITS标准(1)简介