Spring整合redis的操作代碼
導(dǎo)入坐標(biāo)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
配置文件
spring: redis: host: localhost port: 6379
進(jìn)行操作
首先要確認(rèn)一下操作的類型。
1.普通的key-value格式
@Autowired private RedisTemplate redisTemplate; @Test void contextLoads() { } void set() { ValueOperations ops = redisTemplate.opsForValue(); ops.set("age",41); void get() { Object age = ops.get("age"); System.out.println(age);
2.哈希格式
@Test void hset() { HashOperations hashOperations = redisTemplate.opsForHash(); hashOperations.put("info","a","aa"); } @Test void hget() { Object o = hashOperations.get("info", "a"); System.out.println(o);
StringRedisTemplate
其實StringRedisTemplate和RedisTemplate是兩種性質(zhì)一樣的東西,區(qū)別就是前一個指定了泛型是String類型,后一個指定的泛型是Object類型的。
黑框框中寫的其實就是Sting類型的,這就是為什么在黑框框傳入的東西在RedisTemplate中找不到的原因,可以使用StringRedisTemplate對其進(jìn)行查找。
@SpringBootTest public class test { @Autowired private StringRedisTemplate stringRedisTemplate; @Test void test() { ValueOperations<String, String> ops = stringRedisTemplate.opsForValue(); String name = ops.get("name"); System.out.println(name); } }
jedis
這個和lettuce分別是兩種客戶端,是可以隨意選擇的。
添加坐標(biāo)
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
軟后切換模式
spring: redis: host: localhost port: 6379 client-type: jedis
就可以了
到此這篇關(guān)于Spring整合redis的文章就介紹到這了,更多相關(guān)Spring整合redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java并發(fā)包線程池ThreadPoolExecutor的實現(xiàn)
本文主要介紹了Java并發(fā)包線程池ThreadPoolExecutor的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Spring boot JPA實現(xiàn)分頁和枚舉轉(zhuǎn)換代碼示例
這篇文章主要介紹了Spring boot JPA實現(xiàn)分頁和枚舉轉(zhuǎn)換代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09Java利用TCP實現(xiàn)服務(wù)端向客戶端消息群發(fā)的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java如何利用TCP協(xié)議實現(xiàn)服務(wù)端向客戶端消息群發(fā)功能,文中的示例代碼講解詳細(xì),需要的可以參考下,希望對你有所幫助2022-08-08IDEA2020.1啟動SpringBoot項目出現(xiàn)java程序包:xxx不存在
這篇文章主要介紹了IDEA2020.1啟動SpringBoot項目出現(xiàn)java程序包:xxx不存在,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06