Two identical beans get property names with unequal property values

One: The usage scenario of the code:

 The scenario I use is that the current front page is modified, and the modified fields need to be obtained for log saving

Two: There are a few points to note in the following picture

  1. .getClass() is a method of an object instance, only the object instance has this method, and there is no specific class. Category

    The Class class instance is obtained through .class. Obviously, the class does not have a .getClass() method.

  2. The difference between getFields() and getDeclaredFields()
    (1).getFields(): Get all the public (public) fields of a class, including the fields in the parent class. 
    (2).getDeclaredFields(): Obtain all declared fields of a certain class, including public, private and protected, but not including the declared fields of the parent class.

  3.  PropertyDescriptor proDescriptor = new PropertyDescriptor(field.getName(), object.class); I found three points about the error reported by this line of code as follows [the main problem is the standardization of the get method]
    (1). There is one point that needs to be paid attention to serialization The problem, because serialVersionUID (self-defined name) does not have a get method will cause
    (2). The naming convention of the Bean class,
    (3) The naming convention of the get and set methods must be that the first letter after get/set must be capitalized

/**
第一个参数是你修改后的对象数据
第二个参数是需要传的具体想用的数据
第三个参数是未修改前的对象数据
*/
private void editLog(ChildBaseInfo childBaseInfo, String userName, ChildBaseInfo childBaseInfoById) {
        try {
            ChildBaseInfo childBaseInfo1 = new ChildBaseInfo();
            Field[] fields = childBaseInfo1.getClass().getDeclaredFields();
            for (Field field : fields) {
                if (!"isSProblem".equals(field.getName())) {
                    // 获取Bean的某个属性的描述符
                    PropertyDescriptor proDescriptor = new PropertyDescriptor(field.getName(), ChildBaseInfo.class);
                    // 获得用于读取属性值的方法
                    Method methodGetUserName = proDescriptor.getReadMethod();
                    //这个地方是具体调用的的方法
                    childBaseInfo1 = (ChildBaseInfo) StringUtil.exange(childBaseInfo, childBaseInfoById, childBaseInfo1, field, methodGetUserName);
                }
            }
                    //下面的代码是自己的需求处理
            childBaseInfo1.setId(childBaseInfoById.getId());
            childBaseInfo1.setUpdateTime(childBaseInfo.getUpdateTime());
            childBaseInfo1.setUpdateName(userName);
            ChildBaseInfo childChange = mapper.editLogById(childBaseInfoById.getId());
            if (StringUtil.isEmpty(childChange)) {
                mapper.addChildBaseinfoChange(childBaseInfo1);
            } else {
                mapper.updateEditLogById(childBaseInfo1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 Three: The specific implementation of the code in the figure below

  1. Of course, I use Object on the public method parameters.
  2. * @param object2 The filtered and encapsulated data object. This parameter is not necessary. It can also be encapsulated with the entity or object1 passed in.
  3. field.getGenericType().toString() This is the type to get the current attribute
  4. Use if to judge two attribute value classes of the same attribute to use declaredFields.set(object2, s2); repackage the filtered data
 /**
     * zhaofeng 修改数据的过滤
     *
     * @param entity            修改的数据对象
     * @param object1           根据ID查询的原数据对象
     * @param object2           过滤后封装的数据对象
     * @param field
     * @param methodGetUserName
     * @return
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static Object exange(Object entity, Object object1, Object object2, Field field, Method methodGetUserName) throws IllegalAccessException, InvocationTargetException {
        // 读取属性值
        Object objUserName = methodGetUserName.invoke(object1);
        Object objUserName1 = methodGetUserName.invoke(entity);
        //获取根据ID查询的原数据对象的属性值
        String s1 = objUserName == null ? "" : objUserName.toString();//避免空指针异常
        //获取修改的数据对象
        String s2 = objUserName1 == null ? "" : objUserName1.toString();//避免空指针异常

        try {
            Field declaredFields = null;
            declaredFields = object2.getClass().getDeclaredField(field.getName());
            declaredFields.setAccessible(true);
            if (!"".equals(s2)) {
                System.out.println("get userName:" + field.getName() + "值" + s1 + "=============>" + s2);
                if (!s1.equals(s2)) {
                    if (field.getGenericType().toString().equals(
                            "class java.lang.Integer")) {
                        declaredFields.set(object2, Integer.valueOf(s2));
                    } else if (field.getGenericType().toString().equals(
                            "class java.util.Date")) {
                        String pattern = "EEE MMM dd HH:mm:ss zzz yyyy";
                        SimpleDateFormat df = new SimpleDateFormat(pattern, Locale.US);
                        Date date = df.parse(s2);
                        declaredFields.set(object2, date);
                    } else if (field.getGenericType().toString().equals(
                            "class java.math.BigDecimal")) {
                        declaredFields.set(object2, new BigDecimal(s2));
                    } else {
                        declaredFields.set(object2, s2);
                    }
                }
            } else {
               if (field.getGenericType().toString().equals(
                        "class java.lang.Integer")) {
                    declaredFields.set(object2, 0);
                } else if (field.getGenericType().toString().equals(
                        "class java.util.Date")) {
                    declaredFields.set(object2,"");
                } else if (field.getGenericType().toString().equals(
                        "class java.math.BigDecimal")) {
                    declaredFields.set(object2, new BigDecimal(0));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object2;
    }

Summary:
        This is actually a simple application that uses reflection to obtain attribute names and attribute values, and compares whether the values ​​​​are equal by obtaining attribute names.

Guess you like

Origin blog.csdn.net/zf199805/article/details/122915420