SpringBoot如何連接redis-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關SpringBoot如何連接redis,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網(wǎng)站設計、成都網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的齊河網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

在初次用springboot連接redis的時候查看官方文檔和一些博客會發(fā)現(xiàn)配置文件非常的多,這就導致了在學習的開始的時候是沒有體驗的,其實利用springboot連接redis的時候并不需要那么多的配置

首先開啟redis服務器

SpringBoot如何連接redis

然后在springboot里面添加配置文件:

# Redis數(shù)據(jù)庫索引(默認為0)
spring.redis.database=0
# Redis服務器地址
spring.redis.host=localhost
# Redis服務器連接端口
spring.redis.port=6379
# Redis服務器連接密碼(默認為空)
spring.redis.password=
# 連接池大連接數(shù)(使用負值表示沒有限制)
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);
		}
		);
	}

然后在redis里面便可以看到了剛才的新建的

SpringBoot如何連接redis

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

關于“SpringBoot如何連接redis”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

標題名稱:SpringBoot如何連接redis-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://muchs.cn/article36/cspgsg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計、網(wǎng)站排名、網(wǎng)站改版企業(yè)建站、做網(wǎng)站、云服務器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設