redis-demo/learnCode02/src/test/java/sumu/today/learncode02/demo/L01Test.java
package sumu.today.learncode02.demo;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
public class L01Test {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void testString() {
        // 写入一条String数据
        redisTemplate.opsForValue().set("name", "虎哥");
        // 获取string数据
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println("name = " + name);
    }

    @Test
    void testSaveUser() throws JsonProcessingException {
        // 创建对象
        User user = new User("虎哥", 21);
        // 手动序列化
        String json = JSON.toJSONString(user);
        // 写入数据
        redisTemplate.opsForValue().set("user:200", json);

        // 获取数据
        String jsonUser = redisTemplate.opsForValue().get("user:200").toString();
        System.out.println(jsonUser);
        // 手动反序列化
        User user1 = JSON.parseObject(jsonUser, User.class);
        System.out.println("user1 = " + user1);
    }


}

class User {
    public String name;
    public int age;

    User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
