Java 实体类转换方法聚合

⏹场景

前台提交到后台的数据使用一个实体类进行封装,需要将总实体类的数据放到不同的实体类中.一般会在业务层像下面这么写

UserEntity userEntity = new UserEntity();
// 拷贝jtv010Form到userEntity中
BeanUtils.copyProperties(jtv010Form, userEntity);

PersonEntity personEntity = new PersonEntity();
BeanUtils.copyProperties(jtv010Form, personEntity);

能实现功能,但是语义化不好,可读性低


😁语义化较好的书写方法

⏹前台提交的表单Bean

import org.springframework.beans.BeanUtils;

public class Jtv010Form {

    private String id;

    private String name;

    private String age;

    private String address;

    private String hobby;
	
	// 省略get和set
    
    // 转换为User实体类
    public UserEntity convertToUserEntity(){
        Jtv010FormToUserConverter toUserConverter = new Jtv010FormToUserConverter();
        return toUserConverter.convert(this);
    }
	
	// 转换为Person实体类
    public PersonEntity convertToPersonEntity(){
        Jtv010FormToPersonConverter toPersonConverter = new Jtv010FormToPersonConverter();
        return toPersonConverter.convert(this);
    }
	
	// 定义内部类,实现实体类转换接口,通过内部类进行实体类之间的转换
    private static class Jtv010FormToUserConverter implements FormConvert<Jtv010Form, UserEntity> {

        @Override
        public UserEntity convert(Jtv010Form jtv010Form) {

            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(jtv010Form, userEntity);
            return userEntity;
        }
    }

    private static class Jtv010FormToPersonConverter implements FormConvert<Jtv010Form, PersonEntity> {

        @Override
        public PersonEntity convert(Jtv010Form jtv010Form) {

            PersonEntity personEntity = new PersonEntity();
            BeanUtils.copyProperties(jtv010Form, personEntity);
            return personEntity;
        }
    }
}

⏹实体类转换接口

public interface FormConvert<S,T> {
    T convert(S s);
}

⏹待转换的实体类

public class UserEntity {

    private String id;
    private String name;
    private String age;
	
	// 省略get和set
}
public class PersonEntity {

    private String id;
    private String address;
    private String hobby;

    // 省略get和set
}

⏹测试

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class Test3 implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
		
		// 模拟前台提交到后台的数据
        Jtv010Form jtv010Form = new Jtv010Form();
        jtv010Form.setId("1");
        jtv010Form.setName("贾飞天");
        jtv010Form.setAge("18");
        jtv010Form.setAddress("地球");
        jtv010Form.setHobby("喝水");
		
		// 直接进行转换,语义明了,简单易懂提高可读性
        UserEntity userEntity = jtv010Form.convertToUserEntity();
        System.out.println(userEntity);
		
		// 将实体类转换的代码聚合到需要转换的实体类中
        PersonEntity personEntity = jtv010Form.convertToPersonEntity();
        System.out.println(personEntity);
    }
}

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值