修复 BaseServiceImpl 创建 Entity、VO、DTO 失败的问题
This commit is contained in:
parent
e76ba43dab
commit
8a5de8e22a
@ -24,6 +24,8 @@ import org.springframework.beans.factory.InitializingBean;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -192,24 +194,78 @@ public abstract class BaseServiceImpl<
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
protected Entity createEntity() {
|
protected Entity createEntity() {
|
||||||
return (Entity) Entity.builder().build();
|
try {
|
||||||
|
// 使用反射获取具体的实体类并创建实例
|
||||||
|
Class<?> entityClass = getEntityClass();
|
||||||
|
return (Entity) entityClass.getDeclaredConstructor().newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("创建实体实例失败", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化 entity
|
* 实例化 VO
|
||||||
* @return 获取新实例化 entity 类
|
* @return 获取新实例化 VO 类
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
protected VO createVO() {
|
protected VO createVO() {
|
||||||
return (VO) VO.builder().build();
|
try {
|
||||||
|
// 使用反射获取具体的VO类并创建实例
|
||||||
|
Class<?> voClass = getVOClass();
|
||||||
|
return (VO) voClass.getDeclaredConstructor().newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("创建VO实例失败", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化 entity
|
* 实例化 DTO
|
||||||
* @return 获取新实例化 entity 类
|
* @return 获取新实例化 DTO 类
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
protected DTO createDTO() {
|
protected DTO createDTO() {
|
||||||
return (DTO) DTO.builder().build();
|
try {
|
||||||
|
// 使用反射获取具体的DTO类并创建实例
|
||||||
|
Class<?> dtoClass = getDTOClass();
|
||||||
|
return (DTO) dtoClass.getDeclaredConstructor().newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("创建DTO实例失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取实体类的Class对象
|
||||||
|
* @return 实体类的Class对象
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private Class<Entity> getEntityClass() {
|
||||||
|
// 通过获取泛型参数类型来确定具体的实体类
|
||||||
|
Type superClass = getClass().getGenericSuperclass();
|
||||||
|
ParameterizedType parameterizedType = (ParameterizedType) superClass;
|
||||||
|
return (Class<Entity>) parameterizedType.getActualTypeArguments()[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取VO类的Class对象
|
||||||
|
* @return VO类的Class对象
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private Class<VO> getVOClass() {
|
||||||
|
// 通过获取泛型参数类型来确定具体的VO类
|
||||||
|
Type superClass = getClass().getGenericSuperclass();
|
||||||
|
ParameterizedType parameterizedType = (ParameterizedType) superClass;
|
||||||
|
return (Class<VO>) parameterizedType.getActualTypeArguments()[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取DTO类的Class对象
|
||||||
|
* @return DTO类的Class对象
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private Class<DTO> getDTOClass() {
|
||||||
|
// 通过获取泛型参数类型来确定具体的DTO类
|
||||||
|
Type superClass = getClass().getGenericSuperclass();
|
||||||
|
ParameterizedType parameterizedType = (ParameterizedType) superClass;
|
||||||
|
return (Class<DTO>) parameterizedType.getActualTypeArguments()[2];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ knife4j:
|
|||||||
cors: false
|
cors: false
|
||||||
#是否启用登录认证
|
#是否启用登录认证
|
||||||
basic:
|
basic:
|
||||||
enable: true
|
enable: false
|
||||||
username: admin
|
username: admin
|
||||||
password: 123456
|
password: 123456
|
||||||
setting: # 前端UI的个性化配置属性
|
setting: # 前端UI的个性化配置属性
|
||||||
|
Loading…
x
Reference in New Issue
Block a user