首先开启redis服务器:
然后在springboot里面添加配置文件:
# redis数据库索引(默认为0)spring.redis.database=0# redis服务器地址spring.redis.host=localhost# redis服务器连接端口spring.redis.port=6379# redis服务器连接密码(默认为空)spring.redis.password=# 连接池最大连接数(使用负值表示没有限制)spring.redis.pool.max-active=8# 连接池最大阻塞等待时间(使用负值表示没有限制)spring.redis.pool.max-wait=-1# 连接池中的最大空闲连接spring.redis.pool.max-idle=8# 连接池中的最小空闲连接spring.redis.pool.min-idle=0# 连接超时时间(毫秒)spring.redis.timeout=0
最后在springboot的pom.xml文件中添加依赖:
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency>
然后在springboot的项目中写一个测试类:
@runwith(springrunner.class)@springboottestpublic class shoppingapplicationtests { @autowired private stringredistemplate stringredistemplate; @test public void test() throws exception { list<string> list =new arraylist<>(); list.add(a); list.add(b); list.add(v); stringredistemplate.opsforvalue().set(abc, 测试); stringredistemplate.opsforlist().leftpushall(qq,list); // 向redis存入list stringredistemplate.opsforlist().range(qwe,0,-1).foreach(value ->{ system.out.println(value); } ); }
然后在redis里面便可以看到了刚才的新建的
然后这就是最简单的springboot连接redis的方式了
以上就是springboot怎么连接redis的详细内容。
