fix: 自动代码生成模板纠正和定义全局参数

This commit is contained in:
haoxr
2024-04-12 23:21:55 +08:00
parent d610731853
commit d18df15795
4 changed files with 41 additions and 29 deletions

View File

@@ -6,9 +6,7 @@ import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;
/**
* 代码交互式生成
@@ -22,7 +20,7 @@ import java.util.List;
public class FastAutoGeneratorTest {
private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
.Builder("jdbc:mysql://localhost:3306/youlai_boot?serverTimezone=Asia/Shanghai" , "root" , "123456");
.Builder("jdbc:mysql://localhost:3306/youlai_boot?serverTimezone=Asia/Shanghai", "root", "123456");
/**
* 执行 run
@@ -37,14 +35,14 @@ public class FastAutoGeneratorTest {
})
// 包配置
.packageConfig(builder -> {
builder
.parent("com.youlai.system")
.entity("model.entity")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.controller("controller")
.pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/src/main/resources/mapper"));
builder
.parent("com.youlai.system")
.entity("model.entity")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.controller("controller")
.pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/src/main/resources/mapper"));
}
)
// 注入配置(设置扩展类的模板路径和包路径)
@@ -58,14 +56,24 @@ public class FastAutoGeneratorTest {
customFiles.add(new CustomFile.Builder().fileName("Form.java").templatePath("/templates/form.java.vm").packageName("model.form").build());
customFiles.add(new CustomFile.Builder().fileName("Converter.java").templatePath("/templates/converter.java.vm").packageName("converter").build());
consumer.customFile(customFiles);
consumer.beforeOutputFile((tableInfo, objectMap) -> {
// 为每个表生成首字母小写的实体名
String entityName = tableInfo.getEntityName();
String lowerCaseEntity = entityName.substring(0, 1).toLowerCase() + entityName.substring(1);
// 注入自定义参数
objectMap.put("firstCharLowerCaseEntity", lowerCaseEntity);
});
})
// 策略配置
.strategyConfig((scanner, builder) -> {
builder.entityBuilder()
.enableLombok() // 是否使用lombok
//.enableFileOverride() // 是否覆盖文件
//.enableFileOverride() // 开启覆盖已生成的文件
.logicDeleteColumnName("deleted") // 逻辑删除字段名
.enableRemoveIsPrefix() // 开启移除is前缀
;
builder.mapperBuilder()
@@ -73,6 +81,11 @@ public class FastAutoGeneratorTest {
.enableBaseResultMap()
;
builder.serviceBuilder()
.formatServiceFileName("%sService"
);
builder.addTablePrefix("sys_") // 过滤移除表前缀 sys_user 表生成的实体类 User.java
.addInclude(scanner.apply("请输入表名,多个表名用,隔开"));
}

View File

@@ -6,20 +6,16 @@ import org.springframework.web.bind.annotation.RestController;
import ${package.Parent}.model.form.${entity}Form;
import ${package.Parent}.model.query.${entity}PageQuery;
import ${package.Parent}.model.vo.${entity}PageVO;
import ${package.Parent}.service.${entity}Service;
import ${package.Parent}.service.${table.serviceName};
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.List;
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
@@ -28,10 +24,12 @@ import ${superControllerClassPackage};
/**
* $!{table.comment} 前端控制器
*
* @author${author}
* @since${date}
* @author ${author}
* @since ${date}
*/
@Tag(name = "${table.comment}接口")
@RestController
@RequestMapping("/api/v1/${firstCharLowerCaseEntity}s")
@RequiredArgsConstructor
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
@@ -39,21 +37,19 @@ public class ${table.controllerName} extends ${superControllerClass} {
public class ${table.controllerName} {
#end
#set($entityLower = $string.toLowerCase($entity))
private final ${table.serviceName} $entityLowerService;
private final ${table.serviceName} ${firstCharLowerCaseEntity}Service;
@Operation(summary = "$!{table.comment}分页列表")
@GetMapping("/page")
public PageResult<${entity}PageVO> listPaged${entity}s(${entity}PageQuery queryParams ) {
IPage<${entity}PageVO> result = ${entityLower}Service.listPaged${entity}s(queryParams);
IPage<${entity}PageVO> result = ${firstCharLowerCaseEntity}Service.listPaged${entity}s(queryParams);
return PageResult.success(result);
}
@Operation(summary = "新增$!{table.comment}")
@PostMapping
@PreventDuplicateSubmit
public Result save${entity}(@RequestBody @Valid ${entity}Form formData ) {
boolean result = ${entityLower}Service.save${entity}(userForm);
boolean result = ${firstCharLowerCaseEntity}Service.save${entity}(formData);
return Result.judge(result);
}
@@ -62,7 +58,7 @@ public class ${table.controllerName} {
public Result<${entity}Form> get${entity}Form(
@Parameter(description = "$!{table.comment}ID") @PathVariable Long id
) {
${entity}Form formData = ${entityLower}Service.get${entity}FormData(id);
${entity}Form formData = ${firstCharLowerCaseEntity}Service.get${entity}FormData(id);
return Result.success(formData);
}
@@ -70,7 +66,7 @@ public class ${table.controllerName} {
@PutMapping(value = "/{id}")
public Result update${entity}(@Parameter(description = "$!{table.comment}ID") @PathVariable Long id,
@RequestBody @Validated ${entity}Form formData) {
boolean result = ${entityLower}Service.update${entity}(userId, formData);
boolean result = ${firstCharLowerCaseEntity}Service.update${entity}(id, formData);
return Result.judge(result);
}
@@ -79,7 +75,7 @@ public class ${table.controllerName} {
public Result delete${entity}s(
@Parameter(description = "$!{table.comment}ID多个以英文逗号(,)分割") @PathVariable String ids
) {
boolean result = ${entityLower}Service.delete${entity}s(ids);
boolean result = ${firstCharLowerCaseEntity}Service.delete${entity}s(ids);
return Result.judge(result);
}
}

View File

@@ -26,7 +26,7 @@ public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
/**
* 获取$!{table.comment}表单数据
*
* @param userId
* @param id $!{table.comment}ID
* @return
*/
${entity}Form get${entity}FormData(Long id);

View File

@@ -21,6 +21,9 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
/**
* $!{table.comment} 服务实现类
*