侧边栏壁纸
  • 累计撰写 31 篇文章
  • 累计创建 37 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

【Java】转换工具类

一杯香梨
2025-10-20 / 0 评论 / 0 点赞 / 4 阅读 / 0 字
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);
    }

}

0

评论区