反射工具类请参见:https://www.cnblogs.com/threadj/p/10535796.html using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Reflection; namespace ReligionServer.util { public class BeanUtil { /// <summary> /// 两个对象的相同属
开发中经常碰到这样的场景,从数据库查询出来全部的字段,但是有些字段是不想给 客户端看到,这时就需要将属性从DAO复制到传给客户端的DTO对象,如果采用get/set, 那显得很麻烦.可使用反射实现. Spring框架的 org.springframework.beans.BeanUtils 类帮我们实现了这个功能. 例子 @Data class Source{ private String name; private int age; public Source(String name, in
使用场景:两个领域之间对象转换. 比如:在系统分层解耦过程中, 对外facade接口,一般使用VO对象,而内core业务逻辑层或者数据层通常使用Entity实体. VO对象 package com.maven.demo; public class ProductVO{ private Long id; private String name; private String description; public Long getId() { return id; } public void se
实现对象的属性值复制,只会复制命名相同的文件. import org.springframework.beans.BeanUtils; BeanUtils.copyProperties(obj1, obj2); /** * Copy the property values of the given source bean into the given target bean. * <p>Note: The source and target classes do not have to mat
缘起 有一次开发过程中,刚好看到小伙伴在调用 set 方法,将数据库中查询出来的 Po 对象的属性拷贝到 Vo 对象中,类似这样: 可以看出,Po 和 Vo 两个类的字段绝大部分是一样的,我们一个个地调用 set 方法只是做了一些重复的冗长的操作.这种操作非常容易出错,因为对象的属性太多,有可能会漏掉一两个,而且肉眼很难察觉. 类似这样的操作,我们很容易想到可以通过反射来解决.其实,如此普遍通用的功能,一个 BeanUtils 工具类就可以搞定了. 于是我建议这位小伙伴了解一下 BeanUtil