将多个实体类合并到一个实体类中

将多个不同或相同的实体类合并到一个实体类中

package com.config;

import java.lang.reflect.Field;
import java.util.Collection;

/**
 * 类相关工具
 */
public class ClassUtils {

    /**
     * 实体数据合并: 将数据源与目标实体中,命名相同的属性值,合并至目标实体
     * 注:父类属性值不会合并
     * @param sourceBean 数据源实体
     * @param targetBean 目标实体
     * @return targetBean 数据的目标实体
     */
    public static <T> T copy(Object sourceBean, T targetBean) {
        if (sourceBean == null || targetBean == null) {
            return null;
        }
        Field[] sourceFields = sourceBean.getClass().getDeclaredFields();
        Field[] targetFields = targetBean.getClass().getDeclaredFields();
        for (Field sourceField : sourceFields) {
            for (Field targetField : targetFields) {
                if (judgeAssign(sourceField, targetField, sourceBean, targetBean)) {
                    break;
                }
            }
        }
        return targetBean;
    }

    /**
     * 判断并合并属性值
     * @param sourceField 数据源属性
     * @param targetField 目标源属性
     * @param sourceBean 数据源
     * @param targetBean 目标源
     * @return Boolean:是否合并
     */
    private static boolean judgeAssign(Field sourceField, Field targetField, Object sourceBean, Object targetBean) {
        try {
            if (sourceField.getName().equalsIgnoreCase(targetField.getName()) && sourceField.getType().getTypeName().equals(targetField.getType().getTypeName())) {
                sourceField.setAccessible(true);
                Object obj = sourceField.get(sourceBean);
                //集合类型非空判断
                if (obj instanceof Collection) {
                    Collection<?> newValue = (Collection<?>) obj;
                    if (newValue.size() <= 0)
                        return true;
                }
                //数据类型非空判断
                if (obj != null) {
                    targetField.setAccessible(true);
                    targetField.set(targetBean, obj);
                }
                return true;
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }

}

准备几个不同的实体类

package com.bean;

public class car {

    private String car;

    private String car2;

    private String car3;

    public String getCar() {
        return car;
    }

    public void setCar(String car) {
        this.car = car;
    }

    public String getCar2() {
        return car2;
    }

    public void setCar2(String car2) {
        this.car2 = car2;
    }

    public String getCar3() {
        return car3;
    }

    public void setCar3(String car3) {
        this.car3 = car3;
    }
}
package com.bean;

public class room {

    private String room;

    private String room1;

    private int roomNum;

    public String getRoom() {
        return room;
    }

    public void setRoom(String room) {
        this.room = room;
    }

    public String getRoom1() {
        return room1;
    }

    public void setRoom1(String room1) {
        this.room1 = room1;
    }

    public int getRoomNum() {
        return roomNum;
    }

    public void setRoomNum(int roomNum) {
        this.roomNum = roomNum;
    }
}
package com.bean;

public class back {

    private String back;

    public String getBack() {
        return back;
    }

    public void setBack(String back) {
        this.back = back;
    }

}

准备一个实体类,包含以上所有的字段

package com.bean;

public class beanAll {

    private String car;

    private String car2;

    private String car3;

    private String room;

    private String room1;

    private int roomNum;

    private String back;

    public String getCar() {
        return car;
    }

    public void setCar(String car) {
        this.car = car;
    }

    public String getCar2() {
        return car2;
    }

    public void setCar2(String car2) {
        this.car2 = car2;
    }

    public String getCar3() {
        return car3;
    }

    public void setCar3(String car3) {
        this.car3 = car3;
    }

    public String getRoom() {
        return room;
    }

    public void setRoom(String room) {
        this.room = room;
    }

    public String getRoom1() {
        return room1;
    }

    public void setRoom1(String room1) {
        this.room1 = room1;
    }

    public int getRoomNum() {
        return roomNum;
    }

    public void setRoomNum(int roomNum) {
        this.roomNum = roomNum;
    }

    public String getBack() {
        return back;
    }

    public void setBack(String back) {
        this.back = back;
    }

}

使用方法

package com.main;

import com.bean.back;
import com.bean.beanAll;
import com.bean.car;
import com.bean.room;
import com.config.ClassUtils;

public class Main {
    public static void main(String[] args) {
        car car = new car();
        car.setCar("car");
        car.setCar2("car2");
        car.setCar3("car3");

        room room = new room();
        room.setRoom("room");
        room.setRoom1("room1");
        room.setRoomNum(3);

        back back = new back();
        back.setBack("back");

        beanAll all = new beanAll();
        ClassUtils.copy(room,all);
        ClassUtils.copy(car,all);
        ClassUtils.copy(back,all);
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值