spring boot连接配置使用redis

2025-11-05 14:06:47

1、首先在pom.xml添加依赖,依赖内容如下

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

spring boot连接配置使用redis

2、如果是首次创建springboot工程可以直接在新建的时候勾选上redis

spring boot连接配置使用redis

3、上面依赖添加好了,现在设置redis连接参数数,我这里用的是yml格式的配置

spring:  redis:    database: 0    host: ip

   port: 端口    password: xxx

如果是另外一种配置文件直接改成这种格式即可

spring.redis.database=0

其他依此类型转换即可

spring boot连接配置使用redis

4、然后写个测试类,进行简单的连接测试

@AutowiredStringRedisTemplate stringRedisTemplate;

stringRedisTemplate.opsForValue().set("testconnect","hello world"); //设置值stringRedisTemplate.opsForValue().get("testconnect");//取值

spring boot连接配置使用redis

5、写好只后点击测试即可,下面就可以看到输出存到redis里面的值了

spring boot连接配置使用redis

6、然后可以通过idea的redis插件看里面是否多了key和值,但是似乎插件有bug

spring boot连接配置使用redis

7、如果插件用不成,直接redis服务器去查看即可,连接redis,输入密码

然后keys *查看所有的key

spring boot连接配置使用redis

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢