Home > Database > Redis > body text

How to connect redis with SpringBoot

王林
Release: 2023-05-29 19:34:04
forward
2349 people have browsed it

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
Copy after login

Finally add dependencies in the springboot pom.xml file:

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

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>
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template