接口IRedisDaoHash:
package com.net.test.redis.base.dao; import com.net.test.redis.base.entity.UserPsg; /** * @author *** * @Time:2017年8月9日 下午1:43:23 * @version 1.0 * @description redis的哈希操作 */ public interface IRedisDaoHash { public void set(String key,UserPsg psg); public void get(String key,String hKye); public void setClazz(String key,Object cls); public void getKeys(String key); public void getValues(String key); public void length(String key); public void exists(String key,String hashKey); public void delete(String key, Object...hashKeys); }
实现类:
package com.net.test.redis.base.dao.imp; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import com.net.test.redis.base.dao.IRedisDaoHash; import com.net.test.redis.base.entity.UserPsg; import com.net.test.redis.base.until.ClassInfo; /** * @author *** * @Time:2017年8月9日 下午2:04:54 * @version 1.0 * @description */ @Component public class RedisDaoHashImp implements IRedisDaoHash { @Autowired private RedisTemplate<String, String> redis; /** * @description 设置hash的键、属性和值。此方法写的比较死,无法做到通用 * @param key * @param psg */ @Override public void set(String key, UserPsg psg) { HashOperations<String, String, Object> oper = redis.opsForHash(); oper.put(key, "address", psg.getAddress()); oper.put(key, "phone", psg.getPhone()); } /** * @description 此方法利用反射技术,做到了任何pojo都可以通用此方法(目前不支持内置对象,好像redis也不支持内置对象) * @param key * @param object */ @Override public void setClazz(String key, Object object) { HashOperations<String, String, Object> oper = redis.opsForHash(); ClassInfo info = new ClassInfo(); Map<String, String> map; try { map = info.getFieldsByClass(object); Set<String> set = map.keySet(); for(Iterator<String> iter = set.iterator();iter.hasNext();) { String hKey = iter.next(); String hValue = map.get(hKey); oper.put(key, hKey, hValue); } } catch (Exception e) { e.printStackTrace(); } } /** * @description 根据hash的键(key)和属性(hKye)查询出对应的属性 * @param key * @param hKye */ @Override public void get( String key, String hKye) { HashOperations<String, String, Object> oper = redis.opsForHash(); String value = (String) oper.get(key, hKye); System.out.println("hash 的get方法获取值 :" + value); } /** * @description 获取键(key)下面的所有属性 * @param key */ @Override public void getKeys(String key) { HashOperations<String, String, Object> oper = redis.opsForHash(); Set<String> set = oper.keys(key); System.out.println( key + "下面的键有:"); for(Iterator<String> iter = set.iterator();iter.hasNext();) { String _key = iter.next(); System.out.println(_key); } } /** * @description 获取键(key)下面的所有值 * @param key */ @Override public void getValues(String key) { HashOperations<String, String, Object> oper = redis.opsForHash(); List<Object> list = oper.values(key); System.out.println( key + "下面的键有:"); for(Iterator<Object> iter = list.iterator();iter.hasNext();) { String _key = iter.next().toString(); System.out.println(_key); } } /** * @description 根据键获取属性个数 * @param key */ @Override public void length(String key) { HashOperations<String, String, Object> oper = redis.opsForHash(); int size = oper.size(key).intValue(); System.out.println(key + "属性共有 :"+ size); } /** * @description 判断键是否存在 * @param key * @param hashKey */ @Override public void exists(String key,String hashKey) { HashOperations<String, String, Object> oper = redis.opsForHash(); boolean b = oper.hasKey(key, hashKey); System.out.println(key + "下面的" +hashKey + "属性:" + b); } /** * @description 删除指定的键(key)下面的属性(hashKeys) * @param key * @param hashKeys */ @Override public void delete(String key, Object...hashKeys) { HashOperations<String, String, Object> oper = redis.opsForHash(); oper.delete(key,hashKeys); } }
反射依赖方法,自己封装的:
package com.net.test.redis.base.until; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; /** * @author **** * @Time:2017年8月9日 下午3:16:37 * @version 1.0 * @description 反射基类 */ public class ClassInfo { /** * @description 利用反射,读取pojo属性以及get**方法 * @param object * @return * @throws Exception */ public Map<String,String> getFieldsByClass(Object object) throws Exception { Map<String,String> map = new HashMap<String,String>(); Class<?> clazz = object.getClass(); Field[] fields = clazz.getDeclaredFields(); for(int i = 0; i < fields.length; i++) { String key = fields[i].getName(); String Ukey = key.replace(key.substring(0, 1), key.substring(0, 1).toUpperCase()); String value = "get" + Ukey ; Method method = clazz.getDeclaredMethod(value); Object obj = method.invoke(object); map.put(key, obj.toString()); } return map; } }