提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.common.utils; |
H |
2 |
|
|
3 |
import org.springframework.stereotype.Component; |
|
4 |
|
|
5 |
import java.lang.reflect.Field; |
|
6 |
@Component |
|
7 |
public class Conversion { |
|
8 |
public static void convert(Object entity, Object dto) { |
|
9 |
Class<?> entityClass = entity.getClass(); |
|
10 |
Class<?> dtoClass = dto.getClass(); |
|
11 |
Field[] entityFields = entityClass.getDeclaredFields(); |
|
12 |
Field[] dtoFields = dtoClass.getDeclaredFields(); |
|
13 |
try { |
|
14 |
for (Field entityField : entityFields) { |
|
15 |
entityField.setAccessible(true); |
|
16 |
Object value = entityField.get(entity); |
|
17 |
String name = entityField.getName(); |
|
18 |
String dtoFieldName = name.substring(0, 1).toUpperCase() + name.substring(1); |
|
19 |
for (Field dtoField : dtoFields) { |
|
20 |
dtoField.setAccessible(true); |
|
21 |
if (dtoField.getName().equals(dtoFieldName)) { |
|
22 |
dtoField.set(dto, value); |
|
23 |
break; |
|
24 |
} |
|
25 |
} |
|
26 |
} |
|
27 |
}catch (Exception e){ |
|
28 |
|
|
29 |
} |
|
30 |
} |
|
31 |
} |