Redisの店のカスタム・オブジェクト型

参考記事
https://blog.csdn.net/u011499747/article/details/78762007

入門

カスタマイズされたオブジェクトを保存した場合、文字列のストレージを使用することができる食べます。
今回はそれがシリアル化された方法を採用すべきです。そして、直列化可能クラス継承の順序をさせてください。簡単に利用機能・ジャクソン。使用ObjectMapperジャクソンの基本クラス。

Serializableを、自身の直列化を継承していることがわかります。非常に使いやすいです。

頼ります

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.2</version>
</dependency>

スリー・ジャクソンは、ここで紹介異なり、しかし、我々は依存ジャクソンデータバインドのコアと注釈として、必要はありません。長いの導入などとして、

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.2</version>
</dependency>

旅行で、彼は自動的に依存関係を解決します。

その後、固体のオブジェクトを作成します。

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private Integer id;
    private String name;
    private String location;
}

RedisのにObjectMapper使用のシリアライズ

    @Test
    public void test05(){
        Student s = new Student(1001,"张坤", "上海");
        ObjectMapper obj = new ObjectMapper();
        //Object->json
        try {
            String json = obj.writeValueAsString(s);
            //{"id":"1001","name":"张坤", "location":"上海"}
            redisTemplate.opsForValue().set("student", json);

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

ObjectMapperを取り出した後、デシリアライズに使用します

    @Test
    public void test06() throws IOException {
        ObjectMapper obj = new ObjectMapper();
        String json = redisTemplate.opsForValue().get("student");
        Student s = obj.readValue(json,Student.class);
        System.out.println(s);
    }

完成

それでは、どのストレージを組み合わせてください

コレクションとしてクラスです。そして、プロセスのシーケンスのように同じです。

おすすめ

転載: www.cnblogs.com/macht/p/11845064.html