将多个不同或相同的实体类合并到一个实体类中
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);
}
}