首頁  >  文章  >  資料庫  >  SpringBoot怎麼連接redis

SpringBoot怎麼連接redis

王林
王林轉載
2023-05-29 19:34:042312瀏覽

首先開啟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檔案中加入依賴:

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

然後在springboot的專案中寫一個測試類別:

@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>

然後在redis裡面便可以看到了剛才的新建的

SpringBoot怎麼連接redis

#然後這就是最簡單的springboot連接redis的方式了

以上是SpringBoot怎麼連接redis的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除