public static <A, B> B beanA2beanB(A beanA, Class<B> bClass, String... ignoreProperties) {
try {
B b = bClass.newInstance();
cn.hutool.core.bean.BeanUtil.copyProperties(
beanA,
b,
CopyOptions.create().setIgnoreProperties(ignoreProperties).ignoreError().ignoreNullValue()
);
return b;
} catch (Exception e) {
e.printStackTrace();
}
return (B) new Object();
}
/** * 可实现由 BeanA List 转换为 BeanB List<br> * tip1: 转换的规则是 实体内属性一致的进行转换<br> * tip2: 转换会忽略 Null 和错误 * * @param listA A 实体 * @param bClass B 类 * @param ignoreProperties 要忽略转换的字段 数组类型<br> * 由该属性可解决同一个Vo 在不同需求中要返回的实体不一致问题 列入UserListVO 在后台和前台使用的列表是同一个,但是返回的字段不一致 * @param <A> 泛型A * @param <B> 泛型 * @return 转换后的BList实体 */
public static <A, B> List<B> listA2ListB(Collection<A> listA, Class<B> bClass, String... ignoreProperties) {
List<B> listB = new ArrayList<>();
if (ObjectUtils.isEmpty(listA)) {
return listB;
}
try {
for (A a : listA) {
listB.add(beanA2beanB(a, bClass, ignoreProperties));
}
} catch (Exception e) {
e.printStackTrace();
}
return listB;
}
还没有评论,来说两句吧...