在初次用springboot連接redis的時候查看官方文檔和一些博客會發現配置文件非常的多,這就導致了在學習的開始的時候是沒有體驗的,其實利用springboot連接redis的時候并不需要那么多的配置
首先開啟redis服務器:
然后在springboot里面添加配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 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文件中添加依賴:
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency> |
然后在springboot的項目中寫一個測試類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@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詳解的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:https://segmentfault.com/a/1190000011661209