spring boot data redis如何将hashmap改成Model
spring boot data redis how to change hashmap to a Model
我是 spring 和 spring 引导新手,我现在正在使用 spring data redis。
我有这样的模型
@Entity
@Table(name = "users")
public class User {
private Long id;
@Id
@javax.persistence.Column(name = "id", nullable = false, insertable = true, updatable = true)
private String email;
@Basic
@javax.persistence.Column(name = "email", nullable = false, insertable = true, updatable = true, length = 128)
//some other fields and getters and setters
}
在redis中,我们使用hash store User Model,像这样。
关键是:u_id
redis-cli> hgetall u_19999
1) "id"
2) "19999"
3) "email"
4) "xx@gmail.com"
5) "weblink"
6) "www.google.com"
在spring启动数据redis
HashOperations<String, String, String> hashOperations = this.stringRedisTemplate.opsForHash();
String key = "u_"+userId;
Map<String, String> userMap = hashOperations.entries(key);
现在,userMap 是一个 hashmap,包含 Model User 的字段,现在我想将 userMap 更改为 Model User
我该怎么办?
提前致谢。
首先获取所有字段
HashOperations<String, String, String> hashOperations = this.stringRedisTemplate.opsForHash();
String key = "u_"+userId;
Map<String, String> userMap = hashOperations.entries(key);
使用 BeanUtilsHashMapper
class
HashMapper<User, String, String> mapper = new BeanUtilsHashMapper<User>(User.class);
return mapper.fromHash(userMap);
我是 spring 和 spring 引导新手,我现在正在使用 spring data redis。
我有这样的模型
@Entity @Table(name = "users") public class User { private Long id; @Id @javax.persistence.Column(name = "id", nullable = false, insertable = true, updatable = true) private String email; @Basic @javax.persistence.Column(name = "email", nullable = false, insertable = true, updatable = true, length = 128) //some other fields and getters and setters }
在redis中,我们使用hash store User Model,像这样。 关键是:u_id
redis-cli> hgetall u_19999 1) "id" 2) "19999" 3) "email" 4) "xx@gmail.com" 5) "weblink" 6) "www.google.com"
在spring启动数据redis
HashOperations<String, String, String> hashOperations = this.stringRedisTemplate.opsForHash(); String key = "u_"+userId; Map<String, String> userMap = hashOperations.entries(key);
现在,userMap 是一个 hashmap,包含 Model User 的字段,现在我想将 userMap 更改为 Model User
我该怎么办?
提前致谢。
首先获取所有字段
HashOperations<String, String, String> hashOperations = this.stringRedisTemplate.opsForHash(); String key = "u_"+userId; Map<String, String> userMap = hashOperations.entries(key);
使用
BeanUtilsHashMapper
classHashMapper<User, String, String> mapper = new BeanUtilsHashMapper<User>(User.class); return mapper.fromHash(userMap);