[ Java ]分享一个做数据更新的小工具

2019-01-02 17:34:18 +08:00
 yeyuexia

项目上经常要用到把 DTO 转换成 PO,更新数据的操作,而且转换过程中经常有一些比较繁琐的 case 比如说当我遇到订单状态变化的时候,需要做一些处理之类的。写 if 判断逻辑写烦了,基于 java 反射就做了个小工具来帮忙做这个事情:Merge

compile "io.github.yeyuexia:merge:1.4"

首先要做的事情和常用的 mapper 工具比如 orika 呀,mapstruct 呀一样,让它可以从 source structure 转换成 target data structure。不过稍微有点区别的是只有当 source 和 target 的 field value 不同时才会进行 merge。

public class A {
    private AnyTypeA a;
    private AnyTypeB b;
}

public class B {
    private AnyTypeA a;
    private AnyTypeB b;
}

A from = new A();
from.setA("a");
B to = new B();

merge(from, to);

assertEqual(from.getA(), to.getA());

然后支持一定程度的定制化,比如如果 source field 是 null 的话就不进行 merge

A from = new A();
from.setA("a");
B to = new B();
from.setA("b");

merge(from, to, true);

assertEqual(to.getB(), "b");

或者 customize 转化的过程: 懒人版

A from = new A();
from.setA("a");
B to = new B();

MergeConfiguration configuration = new MergeConfiguration<>().custom(TargetTypeInB.class, from -> "c");
withConfiguration(configuration).merge(from, to);

assertEquals(to.getA(), "c");

严格控制目标和源 field 类型

A from = new A();
from.setA("a");
B to = new B();

MergeConfiguration configuration = new MergeConfiguration<>()
  .custom(TargetTypeInB.class, SourTypeInA.class, from -> "c");
withConfiguration(configuration).merge(from, to);

assertEquals(to.getA(), "c");

当然,上面的事情其实 orika,mapstruct 都可以轻易做到,甚至可以做的更好,所以最关键的就是针对特殊字段的通知。 我们可以订阅通知,只要发现数据字段有变化就会回调。当然,所有的回调操作都是在 merge 操作完成之后,所以可以放心的在回调里魔改 source 或者 target instance。 全局配置,只要有改变就会触发回调

A from = new A();
from.setA("a");
B to = new B();
withConfiguration(new MergeConfiguration<>().updateNotify(() -> mock.notifyUpdate())).merge(from, to);

verify(mock, times(1)).notifyUpdate();

回调的同时得到 source 和 target instance

A from = new A();
from.setA("a");
B to = new B();
withConfiguration(new MergeConfiguration<A, B>()
  .updateNotify((source, target) -> target.setUpdateDate(now()))).merge(from, to);

verify(mock, times(1)).notifyUpdate();

针对字段的通知

A from = new A();
from.setA("a");
B to = new B();
withConfiguration(new MergeConfiguration<>()
  .updateNotify("a", (path, f, t) -> mock.notifyUpdate())).merge(from, to);

verify(mock, times(1)).notifyUpdate();
A from = new A();
from.setA("a");
B to = new B();
withConfiguration(new MergeConfiguration<A, B>()
  .updateNotify("a", (path, source, target, f, t) -> target.setUpdateDate(now()))).merge(from, to);

verify(mock, times(1)).notifyUpdate();

项目地址: https://github.com/yeyuexia/merge 欢迎大家试用,提 issue。喜欢的话,感谢 star:)

2158 次点击
所在节点    分享创造
3 条回复
yuanfnadi
2019-01-02 18:22:06 +08:00
beanutils.copyproperties ?
fox0001
2019-01-02 21:19:45 +08:00
@yuanfnadi 对啊,我经常用这个
yeyuexia
2019-01-03 12:09:49 +08:00
@yuanfnadi ???看帖都看个标题就可以回贴了?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/523191

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX