public static void main(String[] args) throws Exception{
RedisCacheStorageImpl<UserEntity> redisCacheStorageImpl = new RedisCacheStorageImpl<UserEntity>();
IUserService userServiceImpl = new UserServiceImpl(redisCacheStorageImpl);
UserEntity entity = new UserEntity();
entity.setUserId("000001");
entity.setEmpCode("130566");
entity.setEmpName("leonardo-zeng");
entity.setRole("Java Development Engineer");
entity.setTitle("PM");
boolean isTrue = userServiceImpl.addUserEntity(entity);
//entity.setUserId("000001");
UserEntity userEntity = userServiceImpl.queryUserEntityByUserId(entity);
System.out.println(userEntity);
}
@Override
public UserEntity queryUserEntityByUserId(UserEntity userEntity) {
//非空
if(userEntity ==null || Strings.isEmpty(userEntity.getUserId())){
return null;
}
//先去缓存中查询 是否存在,不存在在查询
UserEntity reslut = storageCache.hget(cacheKey, userEntity.getUserId());
if(reslut!=null){
return reslut;
}else{
//查询数据库
System.out.println("查询数据库");
}
return null;
}
/**
* redis 缓存存储器实现
* @author qupengfei
*
* @param <V>
*/
//@IocBean
public class RedisCacheStorageImpl<V> implements RedisCacheStorage<String, V> {
/**
* 获取哈希表数据类型的值
* @param cacheKey
* @param key
* @return
*/
@SuppressWarnings("unchecked")
@Override
public V hget(String cacheKey, String key) {
Jedis jedis =null;
//将key 和value 转换成 json 对象
String jKey = Json.toJson(key);
String jCacheKey = Json.toJson(cacheKey);
V jValue =null;
if(Strings.isEmpty(jCacheKey)){
log.info("cacheKey is empty");
return null;
}
try {
//获取客户端对象
jedis =redisClient.getResource();
//执行查询
String value = jedis.hget(jCacheKey, jKey);
//判断值是否非空
if(Strings.isEmpty(value)){
return null;
}else{
jValue= (V) Json.fromJson(value);
Json.fromJsonAsMap(Object.class, value);
}
//返还连接池
redisClient.close(jedis);
} catch (JedisException e) {
log.info("client can't connect server");
if(null !=jedis){
//释放jedis 对象
redisClient.close(jedis);
}
}
return jValue;
}
其中,这句无法转成目标对象
UserEntity userEntity = userServiceImpl.queryUserEntityByUserId(entity);
后台
Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.shlx.blood.cache.UserEntity
at com.shlx.blood.cache.UserServiceImpl.queryUserEntityByUserId(UserServiceImpl.java:67)
at com.shlx.blood.cache.TestUserServiceImpl.main(TestUserServiceImpl.java:77)