模版编制的相关实现

This commit is contained in:
潘荣晟
2025-09-23 16:27:46 +08:00
parent b3e65d4732
commit 41b8c3c323
4 changed files with 39 additions and 31 deletions

View File

@@ -59,8 +59,8 @@ public class TmplTpFldController {
PageResult<TmplTpFldDO> pageResult = tmplTpFldService.tmplTpFldPage(pageReqVO);
return success(BeanUtils.toBean(pageResult, TmplFldRespVO.class));
}
@GetMapping("/getByClass")
@Operation(summary = "获得类固定模板字段列表")
@GetMapping("/class-fld")
@Operation(summary = "获得类固定模板字段列表", description = "clazz为VO类类名")
@PreAuthorize("@ss.hasPermission('bse:tmpl-tp-fld:list')")
public CommonResult<List<Map<String, Object>>> getTmplTpListByClass(String clazz) {
return success(tmplTpFldService.getTmplTpListByClass(clazz));

View File

@@ -1,19 +1,20 @@
package com.zt.plat.module.base.framework.util.config;
import com.zt.plat.module.base.util.ClassInfoScanner;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "base") // 指定配置前缀
@Slf4j
@Getter
@Setter
public class UtilConfig {
// 固定的包路径,直接写死在代码中
private static final String BASE_PACKAGES = "com.zt.plat.module.base.controller.admin";
private String[] packages;
/**
* 当Spring容器初始化该配置类后自动执行初始化操作
@@ -21,9 +22,8 @@ public class UtilConfig {
@PostConstruct
public void initClassInfoScanner() {
try {
// 使用固定的包路径初始化类信息扫描工具
ClassInfoScanner.init(BASE_PACKAGES);
log.info("ClassInfoScanner初始化成功扫描包路径【{}】" , BASE_PACKAGES);
// 使用从配置获取的包路径初始化类信息扫描工具
ClassInfoScanner.init(packages);
} catch (Exception e) {
log.error("ClassInfoScanner初始化失败{}", e.getMessage());
//throw new RuntimeException("类信息扫描工具初始化失败", e);

View File

@@ -18,6 +18,9 @@ import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.tmpltp.enums.ErrorCodeConstants.*;
/**
* 类信息扫描工具类用于通过简单类名获取类字段及Schema注解信息
* 增强版:支持通配符包路径扫描
@@ -34,28 +37,30 @@ public class ClassInfoScanner {
* 初始化扫描指定包路径下的所有类
* @param basePackages 基础包路径,支持逗号分隔多个包,支持*和**通配符
*/
public static synchronized void init(String basePackages) {
public static synchronized void init(String[] basePackages) {
if (initialized) {
return;
}
if (basePackages == null || basePackages.trim().isEmpty()) {
if (basePackages == null || basePackages.length==0) {
throw new IllegalArgumentException("基础包路径不能为空");
}
String[] packages = basePackages.split("[,;\\s]+");
for (String pkg : packages) {
String trimmedPkg = pkg.trim();
if (trimmedPkg.contains("**") || trimmedPkg.contains("*")) {
// 处理通配符包路径
scanPackageWithWildcard(trimmedPkg);
log.info("已扫描通配符包路径:{}", trimmedPkg);
} else {
// 处理普通包路径
scanPackage(trimmedPkg);
log.info("已扫描普通包路径:{}", trimmedPkg);
Arrays.stream(basePackages).forEach(basePackage -> {
String[] packages = basePackage.split("[,;\\s]+");
for (String pkg : packages) {
String trimmedPkg = pkg.trim();
if (trimmedPkg.contains("**") || trimmedPkg.contains("*")) {
// 处理通配符包路径
scanPackageWithWildcard(trimmedPkg);
log.info("已扫描通配符包路径:{}", trimmedPkg);
} else {
// 处理普通包路径
scanPackage(trimmedPkg);
log.info("已扫描普通包路径:{}", trimmedPkg);
}
}
}
});
initialized = true;
}
@@ -218,21 +223,20 @@ public class ClassInfoScanner {
*/
public static List<Map<String, Object>> getClassFieldInfo(String simpleClassName) {
if (!initialized) {
throw new IllegalStateException("工具类未初始化请先调用init方法");
throw exception(UTIL_NOT_INIT);
}
if (simpleClassName == null || !simpleClassName.endsWith("VO")) {
throw new IllegalArgumentException("参数为空或格式错误");
throw exception(PARAMS_IS_NULL_OR_ERR);
}
Class<?> targetClass = CLASS_CACHE.get(simpleClassName);
if (targetClass == null) {
throw new RuntimeException("未找到类: " + simpleClassName);
throw exception(NOT_FOUND_CLASS);
}
List<Map<String, Object>> resultList = new ArrayList<>();
Field[] fields = targetClass.getDeclaredFields();
// PreAuthorize preAuthorize = targetClass.getAnnotation(PreAuthorize.class);
for (Field field : fields) {
if (field.isAnnotationPresent(Schema.class)) {
Schema schema = field.getAnnotation(Schema.class);
@@ -241,6 +245,8 @@ public class ClassInfoScanner {
fieldMap.put("label", schema.description().isEmpty() ?null:schema.description().split(";")[0]);
fieldMap.put("required", schema.requiredMode() == Schema.RequiredMode.REQUIRED);
fieldMap.put("example", schema.example());
//获取字段的类型
fieldMap.put("type", field.getType().getSimpleName());
fieldMap.put("orgnType", "class");
resultList.add(fieldMap);
}