Home  >  Article  >  Database  >  How to connect redis with SpringBoot

How to connect redis with SpringBoot

王林
王林forward
2023-05-29 19:34:042308browse

First open the redis server:

How to connect redis with SpringBoot

Then add the configuration file in 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

Finally add dependencies in the springboot pom.xml file:

<dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-data-redis</artifactid>
    </dependency>

Then write a test class in the springboot project:

@RunWith(SpringRunner.class)
@SpringBootTest
public 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);
		}
		);
	}</string>

Then you can see the newly created one in redis

How to connect redis with SpringBoot

Then this is the simplest way for springboot to connect to redis

The above is the detailed content of How to connect redis with SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete