反射获取字段以及值问题

原创
2020/07/21 09:45
阅读数 377
  /**
     * 根据反射获取到对象的值 拼接sql使用
     *
     * @param model
     * @param ignoreField 不使用字段
     * @return
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     */
    public static String Reflect(Object model, String[] ignoreField)
            throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        // 获取实体类的所有属性,返回Field数组
        Field[] field = model.getClass().getDeclaredFields();
        String content = "";
        // 遍历所有属性
        for (int j = 0; j < field.length; j++) {
            // 获取属性的名字
            String name = field[j].getName();
            // 将属性的首字符大写,方便构造get,set方法
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
            // 获取属性的类型
            String type = field[j].getGenericType().toString();
            // 如果type是类类型,则前面包含"class
            boolean isIgnore = false;
            for (String ignore : ignoreField) {
                if (ignore.equalsIgnoreCase(name)) {
                    isIgnore = true;
                    break;
                }
            }
            if (isIgnore) {
                continue;
            }
            if (type.equals("class java.lang.String")) {
                // ",后面跟类名
                Method m = model.getClass().getMethod("get" + name);
                String value = (String) m.invoke(model);
                // 调用getter方法获取属性值
                if (value != null) {
                    content += "'" + value + "',";
                } else {
                    content += "'',";
                }
            }
            if (type.equals("class java.lang.Integer")) {
                Method m = model.getClass().getMethod("get" + name);
                Integer value = (Integer) m.invoke(model);
                if (value != null) {
                    content += value + ",";
                } else {
                    content += "'',";
                }
            }
            if (type.equals("class java.lang.Long")) {
                Method m = model.getClass().getMethod("get" + name);
                Long value = (Long) m.invoke(model);
                if (value != null) {
                    content += value + ",";
                } else {
                    content += "'',";
                }
            }
            if (type.equals("class java.lang.Double")) {
                Method m = model.getClass().getMethod("get" + name);
                Double value = (Double) m.invoke(model);
                if (value != null) {
                    content += value + ",";
                } else {
                    content += "'',";
                }
            }
            if (type.equals("class java.lang.Boolean")) {
                Method m = model.getClass().getMethod("get" + name);
                Boolean value = (Boolean) m.invoke(model);
                if (value != null) {
                    String cc = value == true ? "1" : "0";
                    content += cc + ',';
                } else {
                    content += "'',";
                }
            }
            if (type.equals("class java.util.Date")) {
                Method m = model.getClass().getMethod("get" + name);
                Date value = (Date) m.invoke(model);
                if (value != null) {
                    content += value + ",";
                } else {
                    content += "'',";
                }
            }
        }
        // 去除末尾的,
        content = content.substring(0, content.length() - 1);
        return content;
    }
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部