主要逻辑代码 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>
遇到的错误和解释