public enum NaturalResourcesEnum { TDDLTB("td_dltb", TdDltb.class), TDGDDB("td_gddb", TdGddb.class), GDZLDJ("gd_zldj", GdZldj.class) ; private String tablename; private Class aClass; NaturalResourcesEnum(String tablename, Class aClass) { this.tablename = tablename; this.aClass = aClass; }Class<T> tableClass = NaturalResourcesEnum.getTableClass(dto.getTablename()); QueryWrapper<?> wrapper = Wrappers.query(); wrapper.likeRight("adcd",dto.getAdcd()); List<Map> map = this.baseMapper.naturalResourcesPageList(dto.getTablename(), wrapper); List<Map> transferMapList = new ArrayList<>(); //将有下划线的转驼峰 map.forEach(x ->{ Map transferMap = MapUtil.formatHumpName(x); transferMapList.add(transferMap); }); List<Map>怎么将枚举对应的实体类把List<Map>转为List<实体类>
时间: 2024-01-16 14:04:25 浏览: 64
enum_processes_1.0.rar_OpenProcess delphi
可以使用Java的反射机制,通过枚举中存储的实体类的Class对象来进行实例化,然后再将List<Map>中的数据逐个转化为实体类对象,并添加到List<实体类>中。以下是示例代码:
```
public List<T> convertMapListToEntityList(List<Map> mapList, Class<T> entityClass) throws Exception {
List<T> entityList = new ArrayList<>();
for (Map map : mapList) {
T entity = entityClass.newInstance();
BeanUtils.populate(entity, map);
entityList.add(entity);
}
return entityList;
}
// 调用方式
NaturalResourcesEnum naturalResource = NaturalResourcesEnum.TDDLTB;
List<Map> mapList = baseMapper.naturalResourcesPageList(naturalResource.getTablename(), wrapper);
List<T> entityList = convertMapListToEntityList(mapList, naturalResource.getAClass());
```
阅读全文