项目中数据结构比较规范,Po和Vo结构相似,不断的get,set?
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DataUtil {
@SneakyThrows
public <S1, T1> T1 covertBean(S1 pos, Class<T1> cl) {
if (pos == null) {
return null;
}
T1 target = cl.newInstance();
BeanUtils.copyProperties(pos, target);
return target;
}
@SneakyThrows
public <S1, T1> List<T1> covertList(List<S1> pos, Class<T1> cl) {
List<T1> targetList = new ArrayList<>();
if (pos == null || pos.isEmpty()) {
return targetList;
}
for (S1 source : pos) {
T1 vo = covertBean(source, cl);
targetList.add(vo);
}
return targetList;
}
@SuppressWarnings("unchecked")
public Map<String, Object> covertBeanToMap(Object bean) {
if (bean == null) {
return new HashMap<>();
}
String json = JSON.toJSONString(bean);
return JSON.parseObject(json, Map.class);
}
public <T2> T2 covertMapToBean(Map map, Class<T2> cl) {
if (map == null || map.isEmpty()) {
return null;
}
String json = JSON.toJSONString(map);
return JSON.parseObject(json, cl);
}
}