FastJson在博客下方
public class TestRedisDemo3 {
// 存储对象 - 以String形式存储
@Test
public void setStringFastJson(){
//1. 连接Redis
Jedis jedis = new Jedis("127.0.0.1", 6379);
//2.1 准备key(String)-value(User)
String stringKey="fastjsonstringUser";
User user = new User(2,"好妹妹",new Date());
//2.2 使用fastJSON将value转化为json字符串
String stringValue = FastJsonUtils.toJSONNoFeaturesNoConfig(user);
//2.3 存储到Redis中
jedis.set(stringKey,stringValue);
//3. 释放资源
jedis.close();
}
// 获取对象 - 以String形式获取
@Test
public void getStringFastJson(){
//1. 连接Redism
Jedis jedis = new Jedis("127.0.0.1", 6379);
//2.1 准备一个key
String key="fastjsonstringUser";
//2.2 去Redis中查询value
String value = jedis.get(key);
//2.3 将value反序列化为User
User user =(User) FastJsonUtils.toBean(value,User.class);
System.out.println("user"+user);
//3. 释放资源
jedis.close();
}
}