189 8069 5689

SpringBoot如何连接redis

这篇文章将为大家详细讲解有关SpringBoot如何连接redis,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

为始兴等地区用户提供了全套网页设计制作服务,及始兴网站建设行业解决方案。主营业务为做网站、网站制作、始兴网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

在初次用springboot连接redis的时候查看官方文档和一些博客会发现配置文件非常的多,这就导致了在学习的开始的时候是没有体验的,其实利用springboot连接redis的时候并不需要那么多的配置

首先开启redis服务器

SpringBoot如何连接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文件中添加依赖:


      org.springframework.boot
      spring-boot-starter-data-redis
    

然后在springboot的项目中写一个测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ShoppingApplicationTests {
	@Autowired
	  private StringRedisTemplate stringRedisTemplate;
	@Test
	  public void test() throws Exception {
		List 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的方式了

关于“SpringBoot如何连接redis”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网站标题:SpringBoot如何连接redis
浏览路径:http://cdxtjz.cn/article/gjosoc.html

其他资讯