DTO传参转换工具

本文介绍了一种在Java中实现对象转换的方法,通过定义接口DTOConvert和具体实现类StudentParamsConvert,实现了从StudentParams到StudentEntity的对象转换。使用了Lombok注解简化代码,并通过BeanUtils工具类完成属性复制。

首先贴上源码

public interface DTOConvert<T, S> {
    /**
     * 转化
     * @param s
     * @return
     */
    T convert(S s);
}

我们将所用的数据分成三种类型 Entity实体类,Params传参,DTO出参

@Data
@Accessors(chain = true)
public class StudentParams{

    private String unid;

    private String name;

    private String phone;

    public StudentEntity converToEntity() {
        StudentParamsConvert convert = StudentParamsConvert.newConvert();
        converToEntityentity = convert.convert(this);
        return entity;
    }

    @NoArgsConstructor(staticName = "newConvert")//构造无参函数
    private static class StudentParamsConvert implements DTOConvert<StudentEntity, StudentParams>{
        @Override
        public StudentEntity convert(StudentParams params) {
            StudentEntityentity  = StudentEntity.newEntity();
            BeanUtils.copyProperties(params, entity);//parmas为来源,entity为目标
//将source数据导入转换到entity中
            return entity;
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值