//相同属性名的字段 赋值
private void copyValueToEntity(Entity1 entity,Entity2 toEntity ){
for (Field field : entity.getClass().getDeclaredFields()) {
for (Field field1 : toEntity.getClass().getDeclaredFields()) {
if(field1.getName().equals(field.getName())){
try {
Method m = null;
String name = field.getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
m = entity.getClass().getMethod("get"+name);
String invoke = String.valueOf(m.invoke(entity));
m = toEntity.getClass().getMethod("set"+name,field1.getType());
m.invoke(toEntity, invoke);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
11-22
564
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)