fix: 自动代码生成错误问题修复

This commit is contained in:
haoxr
2024-04-11 23:59:25 +08:00
parent 0ea36e3f45
commit 124b60f27a
12 changed files with 248 additions and 83 deletions

View File

@@ -6,7 +6,6 @@ import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -37,14 +36,16 @@ 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"))
.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"));
}
)
// 注入配置(设置扩展类的模板路径和包路径)
.injectionConfig(consumer -> {
@@ -53,6 +54,7 @@ public class FastAutoGeneratorTest {
customFiles.add(new CustomFile.Builder().fileName("VO.java").templatePath("/templates/vo.java.vm").packageName("model.vo").build());
customFiles.add(new CustomFile.Builder().fileName("BO.java").templatePath("/templates/bo.java.vm").packageName("model.bo").build());
customFiles.add(new CustomFile.Builder().fileName("PageQuery.java").templatePath("/templates/pageQuery.java.vm").packageName("model.query").build());
customFiles.add(new CustomFile.Builder().fileName("PageVO.java").templatePath("/templates/pageVO.java.vm").packageName("model.vo").build());
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);

View File

@@ -1,4 +1,4 @@
package ${package.Entity};
package ${package.Parent}.model.bo;
#foreach($pkg in ${table.importPackages})
import ${pkg};
@@ -24,17 +24,14 @@ import lombok.experimental.Accessors;
@Accessors(chain = true)
#end
#end
#if(${table.convert})
@TableName("${schemaName}${table.name}")
#end
#if(${superEntityClass})
public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
public class ${entity}BO extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
#elseif(${activeRecord})
public class ${entity} extends Model<${entity}> {
public class ${entity}BO extends Model<${entity}> {
#elseif(${entitySerialVersionUID})
public class ${entity} implements Serializable {
public class ${entity}BO implements Serializable {
#else
public class ${entity} {
public class ${entity}BO {
#end
#if(${entitySerialVersionUID})

View File

@@ -1,11 +1,12 @@
package ${package.Controller};
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
#if(${restControllerStyle})
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
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;
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
@@ -13,24 +14,58 @@ import ${superControllerClassPackage};
/**
* $!{table.comment} 前端控制器
*
* @author ${author}
* @since ${date}
* @author${author}
* @since${date}
*/
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
@RequiredArgsConstructor
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
public class${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end
#set($entityLower = $string.toLowerCase($entity))
private final ${entity}Service $entityLowerService;
@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);
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);
return Result.judge(result);
}
@Operation(summary = "$!{table.comment}表单数据")
@GetMapping("/{id}/form")
public Result<${entity}Form> get${entity}Form(
@Parameter(description = "$!{table.comment}ID") @PathVariable Long id
) {
${entity}Form formData = ${entityLower}Service.get${entity}FormData(id);
return Result.success(formData);
}
@Operation(summary = "修改$!{table.comment}")
@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);
return Result.judge(result);
}
@Operation(summary = "删除$!{table.comment}")
@DeleteMapping("/{ids}")
public Result delete${entity}s(
@Parameter(description = "$!{table.comment}ID多个以英文逗号(,)分割") @PathVariable String ids
) {
boolean result = ${entityLower}Service.delete${entity}s(ids);
return Result.judge(result);
}
}

View File

@@ -1,22 +1,17 @@
package ${package}.converter;
package ${package.Parent}.converter;
#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import ${package}.dto.${entity}DTO;
import ${package}.entity.${entity};
import ${package}.vo.${entity}PageVO;
import ${package}.form.${entity}Form;
import ${package}.bo.${entity}BO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Parent}.model.dto.${entity}DTO;
import ${package.Parent}.model.entity.${entity};
import ${package.Parent}.model.vo.${entity}PageVO;
import ${package.Parent}.model.form.${entity}Form;
import ${package.Parent}.model.bo.${entity}BO;
@Mapper(componentModel = "spring")
public interface ${entity}Converter}{
public interface ${entity}Converter{
${entity}PageVO bo2PageVo(${entity}BO bo);

View File

@@ -1,8 +1,5 @@
package ${package}.model.dto;
package ${package.Parent}.model.dto;
#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
#if(${springdoc})
import io.swagger.v3.oas.annotations.media.Schema;
#elseif(${swagger})
@@ -16,7 +13,7 @@ import lombok.Setter;
import lombok.experimental.Accessors;
#end
#end
import java.io.Serializable;
/**
* $!{table.comment} DTO
*
@@ -27,25 +24,22 @@ import lombok.Setter;
@Getter
@Setter
#if(${chainModel})
@Accessors(chain = true)
@Accessors(chain = true)
#end
#end
#if(${table.convert})
@TableName("${schemaName}${table.name}")
#end
#if(${springdoc})
@Schema(name = "${entity}", description = "$!{table.comment}")
#elseif(${swagger})
@ApiModel(value = "${entity}对象", description = "$!{table.comment}")
#end
#if(${superEntityClass})
public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
public class ${entity}DTO extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
#elseif(${activeRecord})
public class ${entity} extends Model<${entity}> {
public class ${entity}DTO extends Model<${entity}> {
#elseif(${entitySerialVersionUID})
public class ${entity} implements Serializable {
public class ${entity}DTO implements Serializable {
#else
public class ${entity} {
public class ${entity}DTO {
#end
#if(${entitySerialVersionUID})

View File

@@ -1,4 +1,4 @@
package ${package}.model.dto;
package ${package.Parent}.model.form;
#foreach($pkg in ${table.importPackages})
import ${pkg};
@@ -30,22 +30,19 @@ import lombok.Setter;
@Accessors(chain = true)
#end
#end
#if(${table.convert})
@TableName("${schemaName}${table.name}")
#end
#if(${springdoc})
@Schema(name = "${entity}", description = "$!{table.comment}")
#elseif(${swagger})
@ApiModel(value = "${entity}对象", description = "$!{table.comment}")
#end
#if(${superEntityClass})
public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
public class ${entity}Form extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
#elseif(${activeRecord})
public class ${entity} extends Model<${entity}> {
public class ${entity}Form extends Model<${entity}> {
#elseif(${entitySerialVersionUID})
public class ${entity} implements Serializable {
public class ${entity}Form implements Serializable {
#else
public class ${entity} {
public class ${entity}Form {
#end
#if(${entitySerialVersionUID})

View File

@@ -6,6 +6,8 @@ import ${superMapperClassPackage};
import ${mapperAnnotationClass.name};
#end
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Parent}.model.bo.${entity}BO;
import ${package.Parent}.model.query.${entity}PageQuery;
/**
* $!{table.comment} Mapper 接口

View File

@@ -1,6 +1,6 @@
package ${package}.model.query;
package ${package.Parent}.model.query;
import ${package}.common.base.BasePageQuery;
import ${package.Parent}.common.base.BasePageQuery;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

View File

@@ -0,0 +1,130 @@
package ${package.Parent}.model.vo;
#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
#if(${springdoc})
import io.swagger.v3.oas.annotations.media.Schema;
#elseif(${swagger})
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
#if(${entityLombokModel})
import lombok.Getter;
import lombok.Setter;
#if(${chainModel})
import lombok.experimental.Accessors;
#end
#end
/**
* $!{table.comment} 分页VO
*
* @author ${author}
* @since ${date}
*/
#if(${entityLombokModel})
@Getter
@Setter
#if(${chainModel})
@Accessors(chain = true)
#end
#end
#if(${springdoc})
@Schema(name = "${entity}", description = "$!{table.comment}")
#elseif(${swagger})
@ApiModel(value = "${entity}分页视图对象", description = "$!{table.comment}")
#end
#if(${superEntityClass})
public class ${entity}PageVO extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
#elseif(${activeRecord})
public class ${entity}PageVO extends Model<${entity}> {
#elseif(${entitySerialVersionUID})
public class ${entity}PageVO implements Serializable {
#else
public class ${entity}PageVO {
#end
#if(${entitySerialVersionUID})
private static final long serialVersionUID = 1L;
#end
## ---------- BEGIN 字段循环遍历 ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")
#if(${springdoc})
@Schema(description = "${field.comment}")
#elseif(${swagger})
@ApiModelProperty("${field.comment}")
#else
/**
* ${field.comment}
*/
#end
#end
private ${field.propertyType} ${field.propertyName};
#end
## ---------- END 字段循环遍历 ----------
#if(!${entityLombokModel})
#foreach($field in ${table.fields})
#if(${field.propertyType.equals("boolean")})
#set($getprefix="is")
#else
#set($getprefix="get")
#end
public ${field.propertyType} ${getprefix}${field.capitalName}() {
return ${field.propertyName};
}
#if(${chainModel})
public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
#else
public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
#end
this.${field.propertyName} = ${field.propertyName};
#if(${chainModel})
return this;
#end
}
#end
## --foreach end---
#end
## --end of #if(!${entityLombokModel})--
#if(${entityColumnConstant})
#foreach($field in ${table.fields})
public static final String ${field.name.toUpperCase()} = "${field.name}";
#end
#end
#if(${activeRecord})
@Override
public Serializable pkVal() {
#if(${keyPropertyName})
return this.${keyPropertyName};
#else
return null;
#end
}
#end
#if(!${entityLombokModel})
@Override
public String toString() {
return "${entity}{" +
#foreach($field in ${table.fields})
#if($!{foreach.index}==0)
"${field.propertyName} = " + ${field.propertyName} +
#else
", ${field.propertyName} = " + ${field.propertyName} +
#end
#end
"}";
}
#end
}

View File

@@ -2,14 +2,17 @@ package ${package.Service};
import ${package.Entity}.${entity};
import ${superServiceClassPackage};
import ${package.Parent}.model.form.${entity}Form;
import ${package.Parent}.model.query.${entity}PageQuery;
import ${package.Parent}.model.vo.${entity}PageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
/**
* $!{table.comment} 服务类
*
* @author ${author}
* @since ${date}
*/
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
public interface ${entity}Service extends ${superServiceClass}<${entity}> {
/**
@@ -44,7 +47,7 @@ public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
* @param formData $!{table.comment}表单对象
* @return
*/
boolean update${entity}(Long id, UserForm formData);
boolean update${entity}(Long id, ${entity}Form formData);
/**

View File

@@ -7,6 +7,19 @@ import ${superServiceImplClassPackage};
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.youlai.system.common.util.DateUtils;
import ${package.Parent}.model.form.${entity}Form;
import ${package.Parent}.model.query.${entity}PageQuery;
import ${package.Parent}.model.bo.${entity}BO;
import ${package.Parent}.model.vo.${entity}PageVO;
import ${package.Parent}.converter.${entity}Converter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* $!{table.comment} 服务实现类
@@ -47,12 +60,13 @@ public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.m
/**
* 获取$!{table.comment}表单数据
*
* @param userId $!{table.comment}ID
* @param id $!{table.comment}ID
* @return
*/
@Override
public ${entity}Form get${entity}FormData(Long userId) {
return this.baseMapper.get${entity}FormData(userId);
public ${entity}Form get${entity}FormData(Long id) {
${entity} entity = this.getById(id);
return converter.entity2Form(entity);
}
/**
@@ -64,19 +78,18 @@ public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.m
@Override
public boolean save${entity}(${entity}Form formData) {
// 实体转换 form->entity
SysUser entity = converter.form2Entity(formData);
${entity} entity = converter.form2Entity(formData);
return this.save(entity);
}
/**
* 更新$!{table.comment}
*
* @param userId $!{table.comment}ID
* @param id $!{table.comment}ID
* @param formData $!{table.comment}表单对象
* @return
*/
@Override
@Transactional
public boolean update${entity}(Long id,${entity}Form formData) {
${entity} entity = converter.form2Entity(formData);
return this.updateById(entity);

View File

@@ -1,4 +1,4 @@
package ${package}.model.vo;
package ${package.Parent}.model.vo;
#foreach($pkg in ${table.importPackages})
import ${pkg};
@@ -30,22 +30,19 @@ import lombok.Setter;
@Accessors(chain = true)
#end
#end
#if(${table.convert})
@TableName("${schemaName}${table.name}")
#end
#if(${springdoc})
@Schema(name = "${entity}", description = "$!{table.comment}")
#elseif(${swagger})
@ApiModel(value = "${entity}对象", description = "$!{table.comment}")
@ApiModel(value = "${entity}视图对象", description = "$!{table.comment}")
#end
#if(${superEntityClass})
public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
public class ${entity}VO extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
#elseif(${activeRecord})
public class ${entity} extends Model<${entity}> {
#elseif(${entitySerialVersionUID})
public class ${entity} implements Serializable {
public class ${entity}VO implements Serializable {
#else
public class ${entity} {
public class ${entity}VO {
#end
#if(${entitySerialVersionUID})