Commit 5e8988b73bc76fcbc6e49b2b0a3e3c2b3b922b22
1 parent
fcef0f7f
✨ 增加 jakartaPackage 标识,统一 Spring Boot 2.X 和 3.X 代码生成的模版
Showing
7 changed files
with
27 additions
and
11 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java
| ... | ... | @@ -7,6 +7,7 @@ import cn.hutool.core.util.StrUtil; |
| 7 | 7 | import cn.hutool.extra.template.TemplateConfig; |
| 8 | 8 | import cn.hutool.extra.template.TemplateEngine; |
| 9 | 9 | import cn.hutool.extra.template.engine.velocity.VelocityEngine; |
| 10 | +import cn.hutool.system.SystemUtil; | |
| 10 | 11 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; |
| 11 | 12 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 12 | 13 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| ... | ... | @@ -35,10 +36,11 @@ import com.google.common.annotations.VisibleForTesting; |
| 35 | 36 | import com.google.common.collect.ImmutableTable; |
| 36 | 37 | import com.google.common.collect.Maps; |
| 37 | 38 | import com.google.common.collect.Table; |
| 38 | -import org.springframework.stereotype.Component; | |
| 39 | - | |
| 40 | 39 | import jakarta.annotation.PostConstruct; |
| 41 | 40 | import jakarta.annotation.Resource; |
| 41 | +import lombok.Setter; | |
| 42 | +import org.springframework.stereotype.Component; | |
| 43 | + | |
| 42 | 44 | import java.util.*; |
| 43 | 45 | |
| 44 | 46 | import static cn.hutool.core.map.MapUtil.getStr; |
| ... | ... | @@ -157,6 +159,15 @@ public class CodegenEngine { |
| 157 | 159 | private CodegenProperties codegenProperties; |
| 158 | 160 | |
| 159 | 161 | /** |
| 162 | + * 是否使用 jakarta 包,用于解决 Spring Boot 2.X 和 3.X 的兼容性问题 | |
| 163 | + * | |
| 164 | + * true - 使用 jakarta.validation.constraints.* | |
| 165 | + * false - 使用 javax.validation.constraints.* | |
| 166 | + */ | |
| 167 | + @Setter // 允许设置的原因,是因为单测需要手动改变 | |
| 168 | + private Boolean jakartaEnable; | |
| 169 | + | |
| 170 | + /** | |
| 160 | 171 | * 模板引擎,由 hutool 实现 |
| 161 | 172 | */ |
| 162 | 173 | private final TemplateEngine templateEngine; |
| ... | ... | @@ -170,6 +181,8 @@ public class CodegenEngine { |
| 170 | 181 | TemplateConfig config = new TemplateConfig(); |
| 171 | 182 | config.setResourceMode(TemplateConfig.ResourceMode.CLASSPATH); |
| 172 | 183 | this.templateEngine = new VelocityEngine(config); |
| 184 | + // 设置 javaxEnable,按照是否使用 JDK17 来判断 | |
| 185 | + this.jakartaEnable = SystemUtil.getJavaInfo().isJavaVersionAtLeast(1700); // 17.00 * 100 | |
| 173 | 186 | } |
| 174 | 187 | |
| 175 | 188 | @PostConstruct |
| ... | ... | @@ -179,6 +192,7 @@ public class CodegenEngine { |
| 179 | 192 | globalBindingMap.put("basePackage", codegenProperties.getBasePackage()); |
| 180 | 193 | globalBindingMap.put("baseFrameworkPackage", codegenProperties.getBasePackage() |
| 181 | 194 | + '.' + "framework"); // 用于后续获取测试类的 package 地址 |
| 195 | + globalBindingMap.put("jakartaPackage", jakartaEnable ? "jakarta" : "javax"); | |
| 182 | 196 | // 全局 Java Bean |
| 183 | 197 | globalBindingMap.put("CommonResultClassName", CommonResult.class.getName()); |
| 184 | 198 | globalBindingMap.put("PageResultClassName", PageResult.class.getName()); | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/controller/controller.vm
| 1 | 1 | package ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}; |
| 2 | 2 | |
| 3 | 3 | import org.springframework.web.bind.annotation.*; |
| 4 | -import jakarta.annotation.Resource; | |
| 4 | +import ${jakartaPackage}.annotation.Resource; | |
| 5 | 5 | import org.springframework.validation.annotation.Validated; |
| 6 | 6 | #if ($sceneEnum.scene == 1)import org.springframework.security.access.prepost.PreAuthorize;#end |
| 7 | 7 | |
| ... | ... | @@ -9,9 +9,9 @@ import io.swagger.v3.oas.annotations.tags.Tag; |
| 9 | 9 | import io.swagger.v3.oas.annotations.Parameter; |
| 10 | 10 | import io.swagger.v3.oas.annotations.Operation; |
| 11 | 11 | |
| 12 | -import jakarta.validation.constraints.*; | |
| 13 | -import jakarta.validation.*; | |
| 14 | -import jakarta.servlet.http.*; | |
| 12 | +import ${jakartaPackage}.validation.constraints.*; | |
| 13 | +import ${jakartaPackage}.validation.*; | |
| 14 | +import ${jakartaPackage}.servlet.http.*; | |
| 15 | 15 | import java.util.*; |
| 16 | 16 | import java.io.IOException; |
| 17 | 17 | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/controller/vo/saveReqVO.vm
| ... | ... | @@ -3,7 +3,7 @@ package ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePac |
| 3 | 3 | import io.swagger.v3.oas.annotations.media.Schema; |
| 4 | 4 | import lombok.*; |
| 5 | 5 | import java.util.*; |
| 6 | -import jakarta.validation.constraints.*; | |
| 6 | +import ${jakartaPackage}.validation.constraints.*; | |
| 7 | 7 | ## 处理 BigDecimal 字段的引入 |
| 8 | 8 | #foreach ($column in $columns) |
| 9 | 9 | #if (${column.javaType} == "BigDecimal") | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/service/service.vm
| 1 | 1 | package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; |
| 2 | 2 | |
| 3 | 3 | import java.util.*; |
| 4 | -import jakarta.validation.*; | |
| 4 | +import ${jakartaPackage}.validation.*; | |
| 5 | 5 | import ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}.vo.*; |
| 6 | 6 | import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; |
| 7 | 7 | ## 特殊:主子表专属逻辑 | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/service/serviceImpl.vm
| 1 | 1 | package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; |
| 2 | 2 | |
| 3 | 3 | import org.springframework.stereotype.Service; |
| 4 | -import jakarta.annotation.Resource; | |
| 4 | +import ${jakartaPackage}.annotation.Resource; | |
| 5 | 5 | import org.springframework.validation.annotation.Validated; |
| 6 | 6 | import org.springframework.transaction.annotation.Transactional; |
| 7 | 7 | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/test/serviceTest.vm
| ... | ... | @@ -4,7 +4,7 @@ import org.junit.jupiter.api.Disabled; |
| 4 | 4 | import org.junit.jupiter.api.Test; |
| 5 | 5 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 6 | 6 | |
| 7 | -import jakarta.annotation.Resource; | |
| 7 | +import ${jakartaPackage}.annotation.Resource; | |
| 8 | 8 | |
| 9 | 9 | import ${baseFrameworkPackage}.test.core.ut.BaseDbUnitTest; |
| 10 | 10 | |
| ... | ... | @@ -13,7 +13,7 @@ import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.business |
| 13 | 13 | import ${basePackage}.module.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; |
| 14 | 14 | import ${PageResultClassName}; |
| 15 | 15 | |
| 16 | -import jakarta.annotation.Resource; | |
| 16 | +import ${jakartaPackage}.annotation.Resource; | |
| 17 | 17 | import org.springframework.context.annotation.Import; |
| 18 | 18 | import java.util.*; |
| 19 | 19 | import java.time.LocalDateTime; | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineAbstractTest.java
| ... | ... | @@ -45,7 +45,9 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest { |
| 45 | 45 | |
| 46 | 46 | @BeforeEach |
| 47 | 47 | public void setUp() { |
| 48 | + codegenEngine.setJakartaEnable(true); // 强制使用 jakarta,保证单测可以基于 jakarta 断言 | |
| 48 | 49 | codegenEngine.initGlobalBindingMap(); |
| 50 | + // 单测强制使用 | |
| 49 | 51 | // 获取测试文件 resources 路径 |
| 50 | 52 | String absolutePath = FileUtil.getAbsolutePath("application-unit-test.yaml"); |
| 51 | 53 | // 系统不一样生成的文件也有差异,那就各自生成各自的 | ... | ... |