工具类----DTO转实体类

 DTO要继承该类并提供实体泛型

package com.toolconclusion.transition;

import org.springframework.beans.BeanUtils;

import java.lang.reflect.ParameterizedType;

/**
 * @Author: Sun
 * @DateTime: 2025/4/1 10:31
 * @Description:
 **/

/**
 * DTO转实体
 * @param  实体类型
 *           DTO要继承该类并提供实体泛型
 */
public abstract class BaseParamDTO {
    private final Class tClass;

    protected BaseParamDTO() {
        this.tClass = (Class) ((ParameterizedType)
                this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public T convert2Entity() {
        T entity = null;
        try {
            entity = tClass.newInstance();
            BeanUtils.copyProperties(this, entity);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return entity;
    }
}

你可能感兴趣的:(工具类,java,DTO,dto与实体的转换,工具类,类型转换)