entityclass = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 为null
时间: 2024-06-05 14:08:28 浏览: 12
这个问题涉及到Java的泛型机制。这段代码的意思是获取当前类的泛型类型参数,并将其赋值给entityclass变量。但是如果当前类没有定义泛型类型参数,就会导致这里获取的泛型参数为null。可能的原因是你在使用这段代码的时候,没有给当前类定义泛型类型参数,或者定义的方式不正确。你可以检查一下当前类是否定义了泛型,如果有,确保定义方式正确。
相关问题
Class<?> entityClass = entity.getClass(); Field parameterField = entityClass.getDeclaredField(parameterName);
这段代码用于获取实体对象的类,并获取指定的字段。但是,请注意,通过射直接访问私有字段可能会导致安全问题。如果您希望访问私有字段,可以使用以下代码:
```java
Class<?> entityClass = entity.getClass();
try {
Field parameterField = entityClass.getDeclaredField(parameterName);
parameterField.setAccessible(true);
parameterField.set(entity, parameterValue);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
```
在上面的示例中,我们通过调用 `setAccessible(true)` 方法来设置字段为可访问。这样就可以在私有字段上执行操作。然后,我们使用 `set` 方法将参数值设置到实体对象的字段中。
请注意,使用反射需要谨慎操作,并确保您对实体对象和字段有足够的了解,以避免潜在的问题。
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<实体类>
可以使用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());
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)