# Conflicts:
#	yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java
This commit is contained in:
YunaiV 2024-01-20 00:56:22 +08:00
commit 446935fc72
7 changed files with 27 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.TemplateConfig; import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine; import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.engine.velocity.VelocityEngine; import cn.hutool.extra.template.engine.velocity.VelocityEngine;
import cn.hutool.system.SystemUtil;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageParam;
@ -35,10 +36,11 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableTable; import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Table; import com.google.common.collect.Table;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.Setter;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*; import java.util.*;
import static cn.hutool.core.map.MapUtil.getStr; import static cn.hutool.core.map.MapUtil.getStr;
@ -156,6 +158,15 @@ public class CodegenEngine {
@Resource @Resource
private CodegenProperties codegenProperties; private CodegenProperties codegenProperties;
/**
* 是否使用 jakarta 用于解决 Spring Boot 2.X 3.X 的兼容性问题
*
* true - 使用 jakarta.validation.constraints.*
* false - 使用 javax.validation.constraints.*
*/
@Setter // 允许设置的原因是因为单测需要手动改变
private Boolean jakartaEnable;
/** /**
* 模板引擎 hutool 实现 * 模板引擎 hutool 实现
*/ */
@ -170,6 +181,8 @@ public class CodegenEngine {
TemplateConfig config = new TemplateConfig(); TemplateConfig config = new TemplateConfig();
config.setResourceMode(TemplateConfig.ResourceMode.CLASSPATH); config.setResourceMode(TemplateConfig.ResourceMode.CLASSPATH);
this.templateEngine = new VelocityEngine(config); this.templateEngine = new VelocityEngine(config);
// 设置 javaxEnable按照是否使用 JDK17 来判断
this.jakartaEnable = SystemUtil.getJavaInfo().isJavaVersionAtLeast(1700); // 17.00 * 100
} }
@PostConstruct @PostConstruct
@ -179,6 +192,7 @@ public class CodegenEngine {
globalBindingMap.put("basePackage", codegenProperties.getBasePackage()); globalBindingMap.put("basePackage", codegenProperties.getBasePackage());
globalBindingMap.put("baseFrameworkPackage", codegenProperties.getBasePackage() globalBindingMap.put("baseFrameworkPackage", codegenProperties.getBasePackage()
+ '.' + "framework"); // 用于后续获取测试类的 package 地址 + '.' + "framework"); // 用于后续获取测试类的 package 地址
globalBindingMap.put("jakartaPackage", jakartaEnable ? "jakarta" : "javax");
// 全局 Java Bean // 全局 Java Bean
globalBindingMap.put("CommonResultClassName", CommonResult.class.getName()); globalBindingMap.put("CommonResultClassName", CommonResult.class.getName());
globalBindingMap.put("PageResultClassName", PageResult.class.getName()); globalBindingMap.put("PageResultClassName", PageResult.class.getName());

View File

@ -1,7 +1,7 @@
package ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}; package ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName};
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource; import ${jakartaPackage}.annotation.Resource;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
#if ($sceneEnum.scene == 1)import org.springframework.security.access.prepost.PreAuthorize;#end #if ($sceneEnum.scene == 1)import org.springframework.security.access.prepost.PreAuthorize;#end
@ -9,9 +9,9 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*; import ${jakartaPackage}.validation.constraints.*;
import jakarta.validation.*; import ${jakartaPackage}.validation.*;
import jakarta.servlet.http.*; import ${jakartaPackage}.servlet.http.*;
import java.util.*; import java.util.*;
import java.io.IOException; import java.io.IOException;

View File

@ -3,7 +3,7 @@ package ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePac
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*; import lombok.*;
import java.util.*; import java.util.*;
import jakarta.validation.constraints.*; import ${jakartaPackage}.validation.constraints.*;
## 处理 BigDecimal 字段的引入 ## 处理 BigDecimal 字段的引入
#foreach ($column in $columns) #foreach ($column in $columns)
#if (${column.javaType} == "BigDecimal") #if (${column.javaType} == "BigDecimal")

View File

@ -1,7 +1,7 @@
package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; package ${basePackage}.module.${table.moduleName}.service.${table.businessName};
import java.util.*; import java.util.*;
import jakarta.validation.*; import ${jakartaPackage}.validation.*;
import ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}.vo.*; import ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}.vo.*;
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
## 特殊:主子表专属逻辑 ## 特殊:主子表专属逻辑

View File

@ -1,7 +1,7 @@
package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; package ${basePackage}.module.${table.moduleName}.service.${table.businessName};
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import jakarta.annotation.Resource; import ${jakartaPackage}.annotation.Resource;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;

View File

@ -4,7 +4,7 @@ import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import jakarta.annotation.Resource; import ${jakartaPackage}.annotation.Resource;
import ${baseFrameworkPackage}.test.core.ut.BaseDbUnitTest; import ${baseFrameworkPackage}.test.core.ut.BaseDbUnitTest;
@ -13,7 +13,7 @@ import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.business
import ${basePackage}.module.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; import ${basePackage}.module.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper;
import ${PageResultClassName}; import ${PageResultClassName};
import jakarta.annotation.Resource; import ${jakartaPackage}.annotation.Resource;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import java.util.*; import java.util.*;
import java.time.LocalDateTime; import java.time.LocalDateTime;

View File

@ -45,7 +45,9 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
codegenEngine.setJakartaEnable(true); // 强制使用 jakarta保证单测可以基于 jakarta 断言
codegenEngine.initGlobalBindingMap(); codegenEngine.initGlobalBindingMap();
// 单测强制使用
// 获取测试文件 resources 路径 // 获取测试文件 resources 路径
String absolutePath = FileUtil.getAbsolutePath("application-unit-test.yaml"); String absolutePath = FileUtil.getAbsolutePath("application-unit-test.yaml");
// 系统不一样生成的文件也有差异那就各自生成各自的 // 系统不一样生成的文件也有差异那就各自生成各自的