springboot使用RedisRepository操作數(shù)據(jù)的實現(xiàn)
通過集成spring-boot-starter-data-redis之后一共有三種redis hash數(shù)據(jù)操作方式可以供我們選擇
- 一個屬性、一個屬性的存取
- 使用Jackson2HashMapper存取對象
- 使用RedisRepository的對象操作(本節(jié)核心內(nèi)容)
一、一個屬性、一個屬性的存取
這種方式在本專欄上一文章中的代碼,已經(jīng)得以體現(xiàn)。
@Test
public void HashOperations() {
Person person = new Person("kobe","byrant");
person.setAddress(new Address("洛杉磯","美國"));
//使用hash的方法存儲對象數(shù)據(jù)(一個屬性一個屬性的存,下節(jié)教大家簡單的方法)
hashOperations.put("hash:player","firstname",person.getFirstname());
hashOperations.put("hash:player","lastname",person.getLastname());
hashOperations.put("hash:player","address",person.getAddress());
String firstName = (String)hashOperations.get("hash:player","firstname");
System.out.println(firstName);
}
數(shù)據(jù)在redis數(shù)據(jù)庫里面存儲結構是下面這樣的

- 一個hash代表一個對象的數(shù)據(jù)
- 一個對象有多個屬性key、value鍵值對數(shù)據(jù),每一組鍵值對都可以單獨存取
二、使用Jackson2HashMapper存取對象
上一小節(jié)我們操作hash對象的時候是一個屬性一個屬性設置的,那我們有沒有辦法將對象一次性hash入庫呢?我們可以使用jacksonHashOperations和Jackson2HashMapper
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
public class RedisConfigTest3 {
@Resource(name="redisTemplate")
private HashOperations<String, String, Object> jacksonHashOperations;
//注意這里的false,下文會講解
private HashMapper<Object, String, Object> jackson2HashMapper = new Jackson2HashMapper(false);
@Test
public void testHashPutAll(){
Person person = new Person("kobe","bryant");
person.setId("kobe");
person.setAddress(new Address("洛杉磯","美國"));
//將對象以hash的形式放入redis數(shù)據(jù)庫
Map<String,Object> mappedHash = jackson2HashMapper.toHash(person);
jacksonHashOperations.putAll("player:" + person.getId(), mappedHash);
//將對象從數(shù)據(jù)庫取出來
Map<String,Object> loadedHash = jacksonHashOperations.entries("player:" + person.getId());
Object map = jackson2HashMapper.fromHash(loadedHash);
Person getback = new ObjectMapper().convertValue(map,Person.class);
//Junit5,驗證放進去的和取出來的數(shù)據(jù)一致
assertEquals(person.getFirstname(),getback.getFirstname());
}
}
使用這種方式可以一次性的存取java 對象為redis數(shù)據(jù)庫的hash數(shù)據(jù)類型。需要注意的是:執(zhí)行上面的測試用例,Person和Address一定要有public無參構造方法,在將map轉換成Person或Address對象的時候用到,如果沒有的話會報錯。
當new Jackson2HashMapper(false),注意屬性對象Address的存儲格式(兩張圖對比觀察)

當new Jackson2HashMapper(true),注意屬性對象Address的存儲格式(兩張圖對比觀察)

需要注意的是:使用這種方法存儲hash數(shù)據(jù),需要多出一個鍵值對@class說明該hash數(shù)據(jù)對應的java類。在反序列化的時候會使用到,用于將hash數(shù)據(jù)轉換成java對象。
三、使用RedisRepository的對象操作
下面為大家介紹使用RedisRepository進行redis數(shù)據(jù)操作,它不只是能簡單的存取數(shù)據(jù),還可以做很多的CURD操作。使用起來和我們用JPA進行關系型數(shù)據(jù)庫的單表操作,幾乎是一樣的。
首先,我們需要在需要操作的java實體類上面加上@RedisHash注解,并使用@Id為該實體類指定id。是不是和JPA挺像的?
@RedisHash("people") //注意這里的person,下文會說明
public class Person {
@Id
String id;
//其他和上一節(jié)代碼一樣
}
然后寫一個PersonRepository ,繼承CrudRepository,是不是也和JPA差不多?
//泛型第二個參數(shù)是id的數(shù)據(jù)類型
public interface PersonRepository extends CrudRepository<Person, String> {
// 繼承CrudRepository,獲取基本的CRUD操作
}
CrudRepository默認為我們提供了下面的這么多方法,我們直接調用就可以了。

在項目入口方法上加上注解@EnableRedisRepositories(筆者測試,在比較新的版本中這個注解已經(jīng)不需要添加了,默認支持),然后進行下面的測試
@SpringBootTest
public class RedisRepositoryTest {
@Resource
PersonRepository personRepository;
@Test
public void test(){
Person rand = new Person("zimug", "漢神");
rand.setAddress(new Address("杭州", "中國"));
personRepository.save(rand); //存
Optional<Person> op = personRepository.findById(rand.getId()); //取
Person p2 = op.get();
personRepository.count(); //統(tǒng)計Person的數(shù)量
personRepository.delete(rand); //刪除person對象rand
}
}
測試結果:需要注意的是RedisRepository在存取對象數(shù)據(jù)的時候,實際上使用了redis的2種數(shù)據(jù)類型
第一種是Set類型,用于保存每一個存入redis的對象(Person)的id。我們可以利用這個Set實現(xiàn)person對象集合類的操作,比如說:count()統(tǒng)計,統(tǒng)計redis數(shù)據(jù)庫中一共保存了多少個person。注意:下圖中set集合的key名稱,就是通過上文代碼中@RedisHash("people") 指定的。

第二種是Hash類型,是用來保存Java對象的,id是RedisRepository幫我們生成的,這個id和上圖中set集合中保存的id是一致的。

到此這篇關于springboot使用RedisRepository操作數(shù)據(jù)的實現(xiàn)的文章就介紹到這了,更多相關springboot RedisRepository操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot整合SpringSecurityOauth2實現(xiàn)鑒權動態(tài)權限問題
這篇文章主要介紹了SpringBoot整合SpringSecurityOauth2實現(xiàn)鑒權-動態(tài)權限,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
IntelliJ IDEA設置Tabs實現(xiàn)同時打開多個文件且分行顯示
今天小編就為大家分享一篇關于IntelliJ IDEA設置Tabs實現(xiàn)同時打開多個文件且分行顯示,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
java 進程是如何在Linux服務器上進行內(nèi)存分配的
這篇文章主要介紹了java 進程是如何在Linux服務器上進行內(nèi)存分配的,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11
Eclipse轉Itellij IDEA導入Git/svn本地項目的詳細步驟
這篇文章主要介紹了Eclipse轉Itellij IDEA導入Git/svn本地項目,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10

