import org.springframework.beans.BeanUtils; import java.util.ArrayList; import java.util.List; import java.util.function.Supplier;
public class BeanCopyUtils extends BeanUtils { /** * 拷贝数组方法 * * @param sources * @param target * @return java.util.List<T> **/ public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) { return copyListProperties(sources, target, null); } /** * 拷贝数组方法 * * @param sources * @param target * @param callBack 回调 * @return java.util.List<T> **/ public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack) { List<T> list = new ArrayList<>(sources.size()); for (S source : sources) { T t = target.get(); copyProperties(source, t); if (callBack != null) { // 回调 callBack.callBack(source, t); } list.add(t); } return list; } /** * 拷贝Bean方法 * * @param sources * @param target * @return T **/ public static <S, T> T copyProperties(S sources, Supplier<T> target) { T t = target.get(); copyProperties(sources, t); return t; }
public static void main(String[] args) {
//转换对象
List<ManageUnit> unitList = BeanCopyUtils.copyListProperties(unitParamList, ManageUnit::new);
//目标对象
//单个对象拷贝使用hutool的BeanUtil.copyProperties(复制Bean对象属性)
OfficeSecondAllocUse officeSecondAllocUse = new OfficeSecondAllocUse();
//源对象 //目标对象
BeanUtil.copyProperties(officeSecondAllocUseParam, officeSecondAllocUse);
}
}
来源:https://blog.csdn.net/weixin_44067464/article/details/123245790
本站部分图文来源于网络,如有侵权请联系删除。