redis连接工具类

基本连接功能工具类:

public class RedisUtils {

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    public <E> void set(String key, E value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public Boolean del(String key) {
        return redisTemplate.delete(key);
    }

    public <E> E getSingle(String key, Class<E> cls) {
        Object res = redisTemplate.opsForValue().get(key);
        return cast(res, cls);
    }

    public <E> List<E> getList(String key, Class<E> cls) {
        Object res = redisTemplate.opsForValue().get(key);
        return castList(res, cls);
    }

    public Object get(String key) {
        Object res = redisTemplate.opsForValue().get(key);
        return res;
    }

    public String getString(String key) {
        Object resObject = redisTemplate.opsForValue().get(key);
//        if(ObjectUtil.)
        String res = String.valueOf(redisTemplate.opsForValue().get(key));
        return res;
    }

    public Boolean checkIsRedis() {
        try {
            this.set("test", "test");
            if (StringUtil.isNotEmpty(this.getString("test"))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }


    @SuppressWarnings("unchecked")
    private static <T> T cast(Object obj, Class<T> cls) {
        return (T) obj;
    }


    @SuppressWarnings("unchecked")
    private static <T> List<T> castList(Object obj, Class<T> cls) {
        return (List<T>) obj;
    }
}

多功能工具类:


public class ReflectUtil {


    /**
     * 获取字段对应值,并转为String类型,空值返回空字符串
     *
     * @param fieldName
     * @param obj
     * @return
     */
    public static synchronized String getStringValue(String fieldName, Object obj) throws ReflectiveOperationException {
        Object objectValue = getValueByGetter(fieldName, obj);
        if (objectValue == null) {
            return "";
        }
        String result = objectValue.toString();
        //如果类型为BigDecimal,去掉末尾的0
        if (objectValue instanceof BigDecimal) {
            BigDecimal value = (BigDecimal) objectValue;
            value = value.stripTrailingZeros();
            result = value.toPlainString();
        } else if (objectValue instanceof Date) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            result = sdf.format((Date) objectValue).replace(" 00:00:00", "");
        }

        return result.trim();
    }

    public static Object getValueByGetter(String fieldName, Object obj) throws ReflectiveOperationException {
        Method getter = getGetter(fieldName, obj.getClass());
        if (getter != null) {
            return getter.invoke(obj);
        }

        return null;
    }


    public static Object setValueBySetter(String fieldName, Object obj) throws ReflectiveOperationException {
        Method setter = getSetter(fieldName, obj.getClass());
        if (setter == null) {
            throw new ReflectiveOperationException("没有set方法");
        }

        return setter.invoke(obj);
    }

    /**
     * 获取get方法
     *
     * @param fieldName
     * @param cls
     * @return
     */
    public static Method getGetter(String fieldName, Class<?> cls) {
        for (Method method : cls.getMethods()) {
            if (method.getName().equalsIgnoreCase("get".concat(fieldName)) && method.getParameterTypes().length == 0) {
                return method;
            }
        }
        return null;
    }

    /**
     * 获取set方法
     *
     * @param fieldName
     * @param cls
     * @return
     */
    public static Method getSetter(String fieldName, Class<?> cls) {
        for (Method method : cls.getMethods()) {
            if (method.getName().equalsIgnoreCase("set".concat(fieldName)) && method.getParameterTypes().length == 0) {
                return method;
            }
        }
        return null;
    }

    /**
     * 通过属性名获取Field对象
     *
     * @param fieldName
     * @param cls
     * @return
     */
    public static synchronized Field getFieldByName(String fieldName, Class<?> cls) {
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals(fieldName)) {
                return field;
            }
        }

        if (cls.getSuperclass() != null) {
            return getFieldByName(fieldName, cls.getSuperclass());
        }

        return null;
    }

//    /**
//     * 通过属性名获取Field对象
//     *
//     * @param cls
//     * @return
//     */
//    public static synchronized Field getField(Class<?> cls) {
//        Field[] fields = cls.getDeclaredFields();
//        Field f = null;
//        Object paramValue;
//        boolean isNotNull = false;
//        for (Field field : fields) {
//            try {
//                //设置可读写权限
//                field.setAccessible(true);
//
//                if (ObjectUtil.isNotEmpty(paramValue)) {
//                    f = field;
//                    isNotNull = true;
//                    break;
//                }
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            }
//            return field;
//        }
//        if (isNotNull) {
//            return f;
//        }
//        return null;
//    }

    /**
     * 通过对象.class获取所有Fields,包括父类
     *
     * @param cls
     * @return
     */
    public static List<Field> listFields(Class<?> cls) {
        Field[] fs = cls.getDeclaredFields();
        List<Field> fields = new ArrayList<>(Arrays.asList(fs));
        if (cls.getSuperclass() != null) {
            fields.addAll(listFields(cls.getSuperclass()));
        }
        return fields;
    }

    public static boolean fieldExist(String fieldName, Class<?> cls) {
        return getFieldByName(fieldName, cls) != null;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ariliya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值