最初采用apache beanutils,性能很低。后来转为hutool,但不能复制指定格式的日期,所以采用性能很高的com.github.yangtu222.BeanUtils
它已经实现了 copyBean、copyList、copyArray、copyCollection。而hutool没有实现copyList、copyArray、copyCollection,需要自行封装实现。
pom.xml
<dependency> <groupId>com.github.yangtu222</groupId> <artifactId>BeanUtils</artifactId> <version>1.0.9</version> </dependency>
项目概览
实现的功能:
1、基本类型
2、Date
3、List/Parent
4、继承
注意要点:
1、如果复制 Date/Pojo,需要在目标类的相应属性上加@CopyProperty,基本类型/List不用。
ToBean
public class ToBean extends MyBase { private Integer id; private Double scope; private String name; @CopyProperty(convertor=DateConvertor.class) private String myDate; @CopyProperty(convertor=DateConvertor2.class) private Date myDate2; private List<ToChild> list; @CopyProperty private ToParent parent; }
反之,如果从ToBean复制到FromBean,则也需要在FromBean的相应属性上加@CopyProperty。
FromBean
public class FromBean extends MyBase { private Integer id; private Double scope; private String name; @CopyProperty(convertor=DateConvertor2.class) private Date myDate; @CopyProperty(convertor=DateConvertor.class) private String myDate2; private List<FromChild> list; @CopyProperty private FromParent parent; }
如果不会从ToBean复制到FromBean,那么FromBean里的相应属性不需要加@CopyProperty。
2、日期转换需要实现BeanCopyConvertor
DateConvertor:Date-->String
public class DateConvertor implements BeanCopyConvertor<Date, String> { @Override public String convertTo(Date object) { if( object == null ) { object = new Date(); } SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.format(object); } }
DateConvertor2:String--->Date
public class DateConvertor2 implements BeanCopyConvertor<String,Date> { @Override public Date convertTo(String object) { if( object == null ) { return null; } return DateUtil.parse(object, "yyyy-MM-dd"); } }
测试1:from--->to
@Test public void from2to() throws Exception { FromBean from = new FromBean(); from.setId(1); from.setScope(98.6); from.setName("遥远2"); from.setMyDate(DateTime.now()); from.setMyDate2(DateUtil.now()); from.setIsDelete(false); from.setList(CollUtil.newArrayList(new FromChild("子1"),new FromChild("子2"),new FromChild("子3"))); from.setParent(new FromParent((byte) 'a')); ToBean toBean = BeanCopyUtils.copyBean(from, ToBean.class); Console.log(toBean); }
输出:
ToBean( super=MyBase(isDelete=false), id=1, scope=98.6, name=遥远2, myDate=2019-01-16, myDate2=2019-01-16 00:00:00, list=[ FromChild(childName=子1), FromChild(childName=子2), FromChild(childName=子3) ], parent=ToParent(sign=97) )
测试2:to-->from
@Test public void to2from() throws Exception { ToBean to = new ToBean(); to.setId(1); to.setScope(98.6); to.setName("遥远2"); to.setMyDate(DateUtil.now()); to.setMyDate2(DateTime.now()); to.setIsDelete(false); to.setList(CollUtil.newArrayList(new ToChild("子1"),new ToChild("子2"),new ToChild("子3"))); to.setParent(new ToParent((byte) 'b')); FromBean fromBean = BeanCopyUtils.copyBean(to, FromBean.class); Console.log(fromBean); }
输出:
FromBean( id=1, scope=98.6, name=遥远2, myDate=2019-01-16 00:00:00, myDate2=2019-01-16, list=[ ToChild(super=com.ebc.beancopy.base.ToChild@6483f5ae,childName=子1), ToChild(super=com.ebc.beancopy.base.ToChild@b9afc07,childName=子2), ToChild(super=com.ebc.beancopy.base.ToChild@382db087,childName=子3) ], parent=FromParent(sign=98))
以上是copyBean。下边是copyList和copyArray的测试
测试3:copyList
@Test public void testCopyList() throws Exception { List<FromChild> fromList = CollUtil.newArrayList(new FromChild("子1"),new FromChild("子2"),new FromChild("子3")); List<ToChild> toList = BeanCopyUtils.copyList(fromList, ToChild.class); Console.log(toList); }
输出:
[ ToChild(super=com.ebc.beancopy.base.ToChild@366e2eef, childName=子1), ToChild(super=com.ebc.beancopy.base.ToChild@6df97b55, childName=子2), ToChild(super=com.ebc.beancopy.base.ToChild@3cbbc1e0, childName=子3) ]
测试4:copyArray
@Test public void testCopyArray() throws Exception { FromChild [] from = {new FromChild("子1"),new FromChild("子2"),new FromChild("子3")}; ToChild [] to = BeanCopyUtils.copyArray(from, ToChild.class); Console.log(to); }
输出:
[ ToChild(super=com.ebc.beancopy.base.ToChild@6537cf78, childName=子1), ToChild(super=com.ebc.beancopy.base.ToChild@67b6d4ae, childName=子2), ToChild(super=com.ebc.beancopy.base.ToChild@34b7bfc0, childName=子3) ]