Spring Boot可以方便地使用Redis作为缓存,以下是整合Redis的步骤:
1.在pom.xml中添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.在application.properties中配置Redis连接信息:
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
3.使用RedisTemplate操作Redis:
其余的操作这里不在展示了,新建一个项目,配置一些数据库信息即可
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 根据id查询元素
*
* @return
*/
@RequestMapping(value = "/findUserById/{id}", method = RequestMethod.GET)
public User findUserById(@PathVariable Integer id) {
User user = userService.findUserById(id);
redisTemplate.opsForValue().set("user",String.valueOf(user));
return user;
}
从软件上面看postman和redis
以上是整合Redis的基本步骤,根据自己的需求可以扩展更多的操作。