public class BeanConvertUtils {
/**
* 转换对象
* @param source
* @param targetSupplier
* @return
* @param <S>
* @param <T>
*/
public static <S, T> T convert(S source, Supplier<T> targetSupplier) {
if (source == null) return null;
T target = targetSupplier.get();
BeanUtils.copyProperties(source, target);
return target;
}
/**
* 转换分页数据
* @param page
* @param classType
* @return
* @param <T>
*/
public static <T extends Serializable> Page<T> transPageForm(IPage<?> page, Class<T> classType) {
Page<T> resultPage = new Page<>();
BeanUtils.copyProperties(page, resultPage);
try {
resultPage.setRecords(copyList(page.getRecords(), classType));
} catch (Exception e) {
log.error("transform error", e);
}
return resultPage;
}
/**
* @param source 源数组
* @param clazz 目标数组类
* @return 目标数组
*/
public static <T> List<T> copyList(List<?> source, Class<T> clazz) {
if (source == null || source.isEmpty()) {
return Collections.emptyList();
}
return BeanUtil.copyToList(source, clazz);
}
}
版权归属:
一杯香梨
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区