springboot集成Redis
前言
Redis是目前使用的非常广泛的内存数据库,相比memcached,它支持更加丰富的数据类型。本来简要介绍在springboot中使用redis的方法。
如何使用?
1、引入spring-boot-starter-redis
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、在application.properties增加Redis的配置
# 使用的数据库(0-15),默认为0
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
3、使用
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping(value = "/redis/{key}/{value}",method = RequestMethod.GET)
@ResponseBody
public String redisTest(@PathVariable String key,@PathVariable String value) {
String redisValue = stringRedisTemplate.opsForValue().get(key);