崛起于Springboot2.X + 单机Redis(14)

拥有回忆 提交于 2019-11-28 11:22:37

《SpringBoot2.X心法总纲》

简介:单机redis+springboot,并不是jedis单机操作redis,jedis是需要另外的集成,下一篇博客会讲到。

1、添加pom依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、添加配置文件

spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=10000ms

2.0之后需要加ms参数才可以,不然报错,所以以后找2.0博客,标题说是springboot 2.0,但是内容依然是spring.redis.timeout=10000后面没有加ms的都是1.X版本,博客都不是很严格。

3、连接成功

@RestController
public class TestController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping(value = "/redis/test1")
    public String getOne(){

        ValueOperations<String,String> operations = stringRedisTemplate.opsForValue();

        operations.set("mjt","测试redis添加");

        String result = operations.get("mjt");

        return result;

    }
}

只需要写一个controller就好,然后我们启动springboot,调用接口成功!

4、说明

纯自己单机使用redis,用来掌握RedisTemplate操作数据库的,但是开发不是下面这简单的配置,配置文件中也没有使用类似于 spring.redis.jedis.pool.max-active=8 这样的配置,那么下一篇会讲解。

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!