栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java常用BeanCompareUtil 注解 + 反射实现 (实例说明)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java常用BeanCompareUtil 注解 + 反射实现 (实例说明)

文章目录
      • 背景
      • 使用实例说明
      • 使用方式
      • 执行效果

背景

在开发过程中经常会使用到一种场景就是比较内存中的两个bean 数据有什么同。其实这种功能算是一个非常常用的场景。

使用实例说明

一共需要以下的几个模块

  1. FieldComparison 自定义注解,主要是标识需要比较的bean 中的字段。
  2. BeanCompareUtil 核心util 主要是实现compare 方法。
  3. FieldMapping 字段映射关系,选用可以对特定的字段进行转义。
  4. DefaultFieldMapping fieldMapping的实现类。可以扩展。
public class BeanCompareUtil {
    public static List compare(Object oldBean, Object newBean)
            throws InvocationTargetException, IllegalAccessException,
            IntrospectionException, NullPointerException, InstantiationException, NoSuchMethodException {
        if (oldBean == null || newBean == null) {
            throw new IllegalArgumentException("the argument must be not null");
        }
        if (oldBean.getClass() != newBean.getClass()) {
            return Collections.emptyList();
        }
        List list = new ArrayList<>();
        Class aClass = oldBean.getClass();
        while (aClass != null) {
            Field[] fieldList = aClass.getDeclaredFields();
            for (Field field : fieldList) {
                if (field.isAnnotationPresent(FieldComparison.class)) {
                    BeanCompareResult comResult = new BeanCompareResult();
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(), aClass);
                    String name = pd.getName();
                    comResult.setCode(name);
                    Method getMethod = pd.getReadMethod();
                    Object o1 = getMethod.invoke(oldBean);
                    Object o2 = getMethod.invoke(newBean);
                    if (o2 == null) {
                        continue;
                    }
                    String first = null == o1 ? "" : String.valueOf(o1).trim();
                    String second = String.valueOf(o2).trim();
                    if (field.isAnnotationPresent(FieldComparison.class)) {
                        FieldComparison fieldComparison = field.getAnnotation(FieldComparison.class);
                        String value = fieldComparison.codeName();
                        Class mapping = fieldComparison.mapping();
                        Method method = mapping.getMethod("mapping", Map.class);
                        Object invoke = method.invoke(mapping.newInstance(), new HashMap<>());
                        if (invoke != null) {
                            Map map = (Map) invoke;
                            String s = map.get(first);
                            String s2 = map.get(second);
                            comResult.setOldValue(s);
                            comResult.setNewValue(s2);
                        } else {
                            comResult.setOldValue(first);
                            comResult.setNewValue(second);
                        }
                        comResult.setName(value);
                    }
                    if (!first.equals(second)) {
                        list.add(comResult);
                    }
                }
            }
            aClass = aClass.getSuperclass();
        }
        return list;
    }
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldComparison {

    
    String codeName() default "";

    
    Class mapping() default DefaultFieldMapping.class;
}
public interface FieldMapping {
    Map mapping(Map map);
}
public class DefaultFieldMapping implements FieldMapping {
    @Override
    public Map mapping(Map map) {
        return null;
    }
}
使用方式
  1. 在需要compare 的地方加上注释
  2. 定义结果接受的bean
public class BeanCompareResult {
    private String code;
    private String name;
    private String oldValue;
    private String newValue;

    private String extendRemark;

    public String getExtendRemark() {
        return extendRemark;
    }

    public void setExtendRemark(String extendRemark) {
        this.extendRemark = extendRemark;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOldValue() {
        return oldValue;
    }

    public void setOldValue(String oldValue) {
        this.oldValue = oldValue;
    }

    public String getNewValue() {
        return newValue;
    }

    public void setNewValue(String newValue) {
        this.newValue = newValue;
    }

    @Override
    public String toString() {
        return "BeanCompareResult{" +
                "code='" + code + ''' +
                ", name='" + name + ''' +
                ", oldValue='" + oldValue + ''' +
                ", newValue='" + newValue + ''' +
                ", extendRemark='" + extendRemark + ''' +
                '}';
    }
}
  1. 传入oldbean 和 newbean进行比较和接收结果。
执行效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/677738.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号