多模块重构 11:修改代码生成器的实现

This commit is contained in:
YunaiV
2022-02-02 13:57:25 +08:00
parent 8d59384904
commit b0c25ea9f4
52 changed files with 195 additions and 163 deletions

View File

@ -23,6 +23,11 @@
<artifactId>yudao-module-tool-api</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-module-system-api</artifactId>
<version>${revision}</version>
</dependency>
<!-- 业务组件 -->
<dependency>

View File

@ -4,6 +4,7 @@ import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ZipUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.tool.controller.admin.codegen.vo.CodegenDetailRespVO;
import cn.iocoder.yudao.module.tool.controller.admin.codegen.vo.CodegenPreviewRespVO;
import cn.iocoder.yudao.module.tool.controller.admin.codegen.vo.CodegenUpdateReqVO;
@ -36,6 +37,7 @@ import java.util.Map;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Api(tags = "管理后台 - 代码生成器")
@RestController
@ -88,7 +90,7 @@ public class CodegenController {
@PostMapping("/create-list-from-db")
@PreAuthorize("@ss.hasPermission('tool:codegen:create')")
public CommonResult<List<Long>> createCodegenListFromDB(@RequestParam("tableNames") List<String> tableNames) {
return success(codegenService.createCodegenListFromDB(tableNames));
return success(codegenService.createCodegenListFromDB(getLoginUserId(), tableNames));
}
@ApiOperation("基于 SQL 建表语句,创建代码生成器的表和字段定义")
@ -96,7 +98,7 @@ public class CodegenController {
@PostMapping("/create-list-from-sql")
@PreAuthorize("@ss.hasPermission('tool:codegen:create')")
public CommonResult<Long> createCodegenListFromSQL(@RequestParam("sql") String sql) {
return success(codegenService.createCodegenListFromSQL(sql));
return success(codegenService.createCodegenListFromSQL(getLoginUserId(), sql));
}
@ApiOperation("更新数据库的表和字段定义")

View File

@ -16,6 +16,10 @@ public class CodegenTableBaseVO {
@NotNull(message = "导入类型不能为空")
private Integer importType;
@ApiModelProperty(value = "生成场景", required = true, example = "1", notes = "参见 CodegenSceneEnum 枚举")
@NotNull(message = "导入类型不能为空")
private Integer scene;
@ApiModelProperty(value = "表名称", required = true, example = "yudao")
@NotNull(message = "表名称不能为空")
private String tableName;

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.tool.dal.dataobject.codegen;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.tool.enums.codegen.CodegenSceneEnum;
import cn.iocoder.yudao.module.tool.enums.codegen.CodegenTemplateTypeEnum;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@ -29,6 +30,12 @@ public class CodegenTableDO extends BaseDO {
* 枚举 {@link CodegenTemplateTypeEnum}
*/
private Integer importType;
/**
* 生成场景
*
* 枚举 {@link CodegenSceneEnum}
*/
private Integer scene;
// ========== 表相关字段 ==========

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.tool.enums.codegen;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 代码生成的场景枚举
*
* @author 芋道源码
*/
@AllArgsConstructor
@Getter
public enum CodegenSceneEnum {
ADMIN(1, "管理后台", "admin"),
APP(2, "用户 APP", "app");
/**
* 场景
*/
private final Integer scene;
/**
* 场景名
*/
private final String name;
/**
* 基础包名
*/
private final String basePackage;
}

View File

@ -20,26 +20,29 @@ public interface CodegenService {
/**
* 基于 SQL 建表语句,创建代码生成器的表定义
*
* @param userId 用户编号
* @param sql SQL 建表语句
* @return 创建的表定义的编号
*/
Long createCodegenListFromSQL(String sql);
Long createCodegenListFromSQL(Long userId, String sql);
/**
* 基于数据库的表结构,创建代码生成器的表定义
*
* @param userId 用户编号
* @param tableName 表名称
* @return 创建的表定义的编号
*/
Long createCodegen(String tableName);
Long createCodegen(Long userId, String tableName);
/**
* 基于 {@link #createCodegen(String)} 的批量创建
* 基于 {@link #createCodegen(Long, String)} 的批量创建
*
* @param userId 用户编号
* @param tableNames 表名称数组
* @return 创建的表定义的编号数组
*/
List<Long> createCodegenListFromDB(List<String> tableNames);
List<Long> createCodegenListFromDB(Long userId, List<String> tableNames);
/**
* 更新数据库的表和字段定义

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.tool.service.codegen;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.tool.framework.codegen.config.CodegenProperties;
import cn.iocoder.yudao.module.tool.controller.admin.codegen.vo.CodegenUpdateReqVO;
import cn.iocoder.yudao.module.tool.controller.admin.codegen.vo.table.CodegenTablePageReqVO;
@ -50,6 +51,9 @@ public class CodegenServiceImpl implements CodegenService {
@Resource
private CodegenColumnMapper codegenColumnMapper;
@Resource
private AdminUserApi userApi;
@Resource
private CodegenBuilder codegenBuilder;
@Resource
@ -58,7 +62,7 @@ public class CodegenServiceImpl implements CodegenService {
@Resource
private CodegenProperties codegenProperties;
private Long createCodegen0(CodegenImportTypeEnum importType,
private Long createCodegen0(Long userId, CodegenImportTypeEnum importType,
SchemaTableDO schemaTable, List<SchemaColumnDO> schemaColumns) {
// 校验导入的表和字段非空
if (schemaTable == null) {
@ -75,6 +79,7 @@ public class CodegenServiceImpl implements CodegenService {
// 构建 CodegenTableDO 对象,插入到 DB 中
CodegenTableDO table = codegenBuilder.buildTable(schemaTable);
table.setImportType(importType.getType());
table.setAuthor(userApi.getUser(userId).getNickname());
codegenTableMapper.insert(table);
// 构建 CodegenColumnDO 数组,插入到 DB 中
List<CodegenColumnDO> columns = codegenBuilder.buildColumns(schemaColumns);
@ -86,7 +91,7 @@ public class CodegenServiceImpl implements CodegenService {
}
@Override
public Long createCodegenListFromSQL(String sql) {
public Long createCodegenListFromSQL(Long userId, String sql) {
// 从 SQL 中,获得数据库表结构
SchemaTableDO schemaTable;
List<SchemaColumnDO> schemaColumns;
@ -98,26 +103,26 @@ public class CodegenServiceImpl implements CodegenService {
throw exception(CODEGEN_PARSE_SQL_ERROR);
}
// 导入
return this.createCodegen0(CodegenImportTypeEnum.SQL, schemaTable, schemaColumns);
return this.createCodegen0(userId, CodegenImportTypeEnum.SQL, schemaTable, schemaColumns);
}
@Override
public Long createCodegen(String tableName) {
public Long createCodegen(Long userId, String tableName) {
// 获取当前schema
String tableSchema = codegenProperties.getDbSchemas().iterator().next();
// 从数据库中,获得数据库表结构
SchemaTableDO schemaTable = schemaTableMapper.selectByTableSchemaAndTableName(tableSchema, tableName);
List<SchemaColumnDO> schemaColumns = schemaColumnMapper.selectListByTableName(tableSchema, tableName);
// 导入
return this.createCodegen0(CodegenImportTypeEnum.DB, schemaTable, schemaColumns);
return this.createCodegen0(userId, CodegenImportTypeEnum.DB, schemaTable, schemaColumns);
}
@Override
@Transactional(rollbackFor = Exception.class)
public List<Long> createCodegenListFromDB(List<String> tableNames) {
public List<Long> createCodegenListFromDB(Long userId, List<String> tableNames) {
List<Long> ids = new ArrayList<>(tableNames.size());
// 遍历添加。虽然效率会低一点,但是没必要做成完全批量,因为不会这么大量
tableNames.forEach(tableName -> ids.add(createCodegen(tableName)));
tableNames.forEach(tableName -> ids.add(createCodegen(userId, tableName)));
return ids;
}

View File

@ -27,18 +27,6 @@ import static cn.hutool.core.text.CharSequenceUtil.*;
@Component
public class CodegenBuilder {
/**
* Module 名字的映射 TODO 后续梳理到配置类
*
* key模块的完整名
* value模块的缩写名
*/
private static final Map<String, String> moduleNames = MapUtil.<String, String>builder()
.put("system", "sys")
.put("infra", "inf")
.put("tool", "tool")
.build();
/**
* 字段名与 {@link CodegenColumnListConditionEnum} 的默认映射
* 注意,字段的匹配以后缀的方式
@ -125,8 +113,10 @@ public class CodegenBuilder {
* @param table 表定义
*/
private void initTableDefault(CodegenTableDO table) {
table.setModuleName(getFullModuleName(StrUtil.subBefore(table.getTableName(),
'_', false))); // 第一个 _ 前缀的前面,作为 module 名字
// 以 system_dept 举例子。moduleName 为 system、businessName 为 dept、className 为 SystemDept
// 如果不希望 System 前缀,则可以手动在【代码生成 - 修改生成配置 - 基本信息】,将实体类名称改为 Dept 即可
table.setModuleName(StrUtil.subBefore(table.getTableName(),
'_', false)); // 第一个 _ 前缀的前面,作为 module 名字
table.setBusinessName(toCamelCase(subAfter(table.getTableName(),
'_', false))); // 第一步,第一个 _ 前缀的后面,作为 module 名字; 第二步,可能存在多个 _ 的情况,转换成驼峰
table.setClassName(upperFirst(toCamelCase(table.getTableName()))); // 驼峰 + 首字母大写
@ -208,27 +198,4 @@ public class CodegenBuilder {
}
}
/**
* 获得模块的缩略名
*
* @param fullModuleName 模块的完整名
* @return 缩略名
*/
public String getSimpleModuleName(String fullModuleName) {
return moduleNames.getOrDefault(fullModuleName, fullModuleName);
}
/**
* 获得模块的完整名
*
* @param shortModuleName 模块的缩略名
* @return 完整名
*/
public String getFullModuleName(String shortModuleName) {
return moduleNames.entrySet().stream()
.filter(entry -> entry.getValue().equals(shortModuleName)) // 匹配
.findFirst().map(Map.Entry::getKey) // 返回 key
.orElse(shortModuleName); // 兜底返回 shortModuleName
}
}

View File

@ -53,38 +53,39 @@ public class CodegenEngine {
* value生成的路径
*/
private static final Map<String, String> TEMPLATES = MapUtil.<String, String>builder(new LinkedHashMap<>()) // 有序
// Java Main
// Java module-impl Main
.put(javaTemplatePath("controller/vo/baseVO"),
javaFilePath("controller/${table.businessName}/vo/${table.className}BaseVO"))
javaModuleImplMainFilePath("controller/${table.businessName}/vo/${table.className}BaseVO"))
.put(javaTemplatePath("controller/vo/createReqVO"),
javaFilePath("controller/${table.businessName}/vo/${table.className}CreateReqVO"))
javaModuleImplMainFilePath("controller/${table.businessName}/vo/${table.className}CreateReqVO"))
.put(javaTemplatePath("controller/vo/pageReqVO"),
javaFilePath("controller/${table.businessName}/vo/${table.className}PageReqVO"))
javaModuleImplMainFilePath("controller/${table.businessName}/vo/${table.className}PageReqVO"))
.put(javaTemplatePath("controller/vo/respVO"),
javaFilePath("controller/${table.businessName}/vo/${table.className}RespVO"))
javaModuleImplMainFilePath("controller/${table.businessName}/vo/${table.className}RespVO"))
.put(javaTemplatePath("controller/vo/updateReqVO"),
javaFilePath("controller/${table.businessName}/vo/${table.className}UpdateReqVO"))
javaModuleImplMainFilePath("controller/${table.businessName}/vo/${table.className}UpdateReqVO"))
.put(javaTemplatePath("controller/vo/exportReqVO"),
javaFilePath("controller/${table.businessName}/vo/${table.className}ExportReqVO"))
javaModuleImplMainFilePath("controller/${table.businessName}/vo/${table.className}ExportReqVO"))
.put(javaTemplatePath("controller/vo/excelVO"),
javaFilePath("controller/${table.businessName}/vo/${table.className}ExcelVO"))
javaModuleImplMainFilePath("controller/${table.businessName}/vo/${table.className}ExcelVO"))
.put(javaTemplatePath("controller/controller"),
javaFilePath("controller/${table.businessName}/${table.className}Controller"))
javaModuleImplMainFilePath("controller/${table.businessName}/${table.className}Controller"))
.put(javaTemplatePath("convert/convert"),
javaFilePath("convert/${table.businessName}/${table.className}Convert"))
javaModuleImplMainFilePath("convert/${table.businessName}/${table.className}Convert"))
.put(javaTemplatePath("dal/do"),
javaFilePath("dal/dataobject/${table.businessName}/${table.className}DO"))
javaModuleImplMainFilePath("dal/dataobject/${table.businessName}/${table.className}DO"))
.put(javaTemplatePath("dal/mapper"),
javaFilePath("dal/mysql/${table.businessName}/${table.className}Mapper"))
.put(javaTemplatePath("enums/errorcode"),
javaFilePath("enums/${simpleModuleName_upperFirst}ErrorCodeConstants"))
javaModuleImplMainFilePath("dal/mysql/${table.businessName}/${table.className}Mapper"))
.put(javaTemplatePath("service/serviceImpl"),
javaFilePath("service/${table.businessName}/impl/${table.className}ServiceImpl"))
javaModuleImplMainFilePath("service/${table.businessName}/${table.className}ServiceImpl"))
.put(javaTemplatePath("service/service"),
javaFilePath("service/${table.businessName}/${table.className}Service"))
// Java Test
javaModuleImplMainFilePath("service/${table.businessName}/${table.className}Service"))
// Java module-impl Test
.put(javaTemplatePath("test/serviceTest"),
javaFilePath("service/${table.businessName}/${table.className}ServiceTest"))
javaModuleImplTestFilePath("service/${table.businessName}/${table.className}ServiceTest"))
// Java module-api Main
.put(javaTemplatePath("enums/errorcode"),
javaModuleApiMainFilePath("enums/ErrorCodeConstants"))
// Vue
.put(vueTemplatePath("views/index.vue"),
vueFilePath("views/${table.moduleName}/${classNameVar}/index.vue"))
@ -121,7 +122,7 @@ public class CodegenEngine {
// 全局配置
globalBindingMap.put("basePackage", codegenProperties.getBasePackage());
globalBindingMap.put("baseFrameworkPackage", StrUtil.subBefore(codegenProperties.getBasePackage(),
'.', true) + '.' + "framework");
'.', true) + '.' + "framework"); // 用于后续获取测试类的 package 地址
// 全局 Java Bean
globalBindingMap.put("CommonResultClassName", CommonResult.class.getName());
globalBindingMap.put("PageResultClassName", PageResult.class.getName());
@ -149,13 +150,9 @@ public class CodegenEngine {
bindingMap.put("table", table);
bindingMap.put("columns", columns);
bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, CodegenColumnDO::getPrimaryKey)); // 主键字段
// moduleName 相关
String simpleModuleName = codegenBuilder.getSimpleModuleName(table.getModuleName());
bindingMap.put("simpleModuleName", simpleModuleName); // 将 system 转成 sys
bindingMap.put("simpleModuleName_upperFirst", upperFirst(simpleModuleName)); // 将 sys 转成 Sys
// className 相关
// 去掉指定前缀 将 TestDictType 转换成 DictType. 因为在 create 等方法后,不需要带上 Test 前缀
String simpleClassName = removePrefix(table.getClassName(), upperFirst(simpleModuleName));
String simpleClassName = removePrefix(table.getClassName(), upperFirst(table.getModuleName()));
bindingMap.put("simpleClassName", simpleClassName);
bindingMap.put("simpleClassName_underlineCase", toUnderlineCase(simpleClassName)); // 将 DictType 转换成 dict_type
bindingMap.put("classNameVar", lowerFirst(simpleClassName)); // 将 DictType 转换成 dictType用于变量
@ -177,8 +174,6 @@ public class CodegenEngine {
private String formatFilePath(String filePath, Map<String, Object> bindingMap) {
filePath = StrUtil.replace(filePath, "${basePackage}",
getStr(bindingMap, "basePackage").replaceAll("\\.", "/"));
filePath = StrUtil.replace(filePath, "${simpleModuleName_upperFirst}",
getStr(bindingMap, "simpleModuleName_upperFirst"));
filePath = StrUtil.replace(filePath, "${classNameVar}",
getStr(bindingMap, "classNameVar"));
@ -194,8 +189,23 @@ public class CodegenEngine {
return "codegen/java/" + path + ".vm";
}
private static String javaFilePath(String path) {
return "java/${basePackage}/modules/${table.moduleName}/" + path + ".java";
private static String javaModuleImplMainFilePath(String path) {
return javaModuleFilePath(path, "impl", "main");
}
private static String javaModuleApiMainFilePath(String path) {
return javaModuleFilePath(path, "api", "main");
}
private static String javaModuleImplTestFilePath(String path) {
return javaModuleFilePath(path, "impl", "test");
}
private static String javaModuleFilePath(String path, String module, String src) {
return "yudao-module-${table.moduleName}/" + // 顶级模块
"yudao-module-${table.moduleName}-" + module + "/" + // 子模块
"src/" + src + "/java/${basePackage}/module/${table.moduleName}/" + path + ".java";
}
private static String vueTemplatePath(String path) {

View File

@ -0,0 +1,102 @@
package ${basePackage}.module.${table.moduleName}.controller.${table.businessName};
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import ${PageResultClassName};
import ${CommonResultClassName};
import static ${CommonResultClassName}.success;
import ${ExcelUtilsClassName};
import ${OperateLogClassName};
import static ${OperateTypeEnumClassName}.*;
import ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${basePackage}.module.${table.moduleName}.convert.${table.businessName}.${table.className}Convert;
import ${basePackage}.module.${table.moduleName}.service.${table.businessName}.${table.className}Service;
@Api(tags = "${table.classComment}")
@RestController
##二级的 businessName 暂时不算在 HTTP 路径上,可以根据需要写
@RequestMapping("/${table.moduleName}/${simpleClassName_strikeCase}")
@Validated
public class ${table.className}Controller {
@Resource
private ${table.className}Service ${classNameVar}Service;
@PostMapping("/create")
@ApiOperation("创建${table.classComment}")
@PreAuthorize("@ss.hasPermission('${permissionPrefix}:create')")
public CommonResult<${primaryColumn.javaType}> create${simpleClassName}(@Valid @RequestBody ${table.className}CreateReqVO createReqVO) {
return success(${classNameVar}Service.create${simpleClassName}(createReqVO));
}
@PutMapping("/update")
@ApiOperation("更新${table.classComment}")
@PreAuthorize("@ss.hasPermission('${permissionPrefix}:update')")
public CommonResult<Boolean> update${simpleClassName}(@Valid @RequestBody ${table.className}UpdateReqVO updateReqVO) {
${classNameVar}Service.update${simpleClassName}(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@ApiOperation("删除${table.classComment}")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = ${primaryColumn.javaType}.class)
@PreAuthorize("@ss.hasPermission('${permissionPrefix}:delete')")
public CommonResult<Boolean> delete${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) {
${classNameVar}Service.delete${simpleClassName}(id);
return success(true);
}
@GetMapping("/get")
@ApiOperation("获得${table.classComment}")
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = ${primaryColumn.javaType}.class)
@PreAuthorize("@ss.hasPermission('${permissionPrefix}:query')")
public CommonResult<${table.className}RespVO> get${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) {
${table.className}DO ${classNameVar} = ${classNameVar}Service.get${simpleClassName}(id);
return success(${table.className}Convert.INSTANCE.convert(${classNameVar}));
}
@GetMapping("/list")
@ApiOperation("获得${table.classComment}列表")
@ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
@PreAuthorize("@ss.hasPermission('${permissionPrefix}:query')")
public CommonResult<List<${table.className}RespVO>> get${simpleClassName}List(@RequestParam("ids") Collection<${primaryColumn.javaType}> ids) {
List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(ids);
return success(${table.className}Convert.INSTANCE.convertList(list));
}
@GetMapping("/page")
@ApiOperation("获得${table.classComment}分页")
@PreAuthorize("@ss.hasPermission('${permissionPrefix}:query')")
public CommonResult<PageResult<${table.className}RespVO>> get${simpleClassName}Page(@Valid ${table.className}PageReqVO pageVO) {
PageResult<${table.className}DO> pageResult = ${classNameVar}Service.get${simpleClassName}Page(pageVO);
return success(${table.className}Convert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/export-excel")
@ApiOperation("导出${table.classComment} Excel")
@PreAuthorize("@ss.hasPermission('${permissionPrefix}:export')")
@OperateLog(type = EXPORT)
public void export${simpleClassName}Excel(@Valid ${table.className}ExportReqVO exportReqVO,
HttpServletResponse response) throws IOException {
List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(exportReqVO);
// 导出 Excel
List<${table.className}ExcelVO> datas = ${table.className}Convert.INSTANCE.convertList02(list);
ExcelUtils.write(response, "${table.classComment}.xls", "数据", ${table.className}ExcelVO.class, datas);
}
}

View File

@ -0,0 +1,13 @@
## 提供给 baseVO、createVO、updateVO 生成字段
@ApiModelProperty(value = "${column.columnComment}"#if (!${column.nullable}), required = true#end#if ("$!column.example" != ""), example = "${column.example}"#end)
#if (!${column.nullable})## 判断 @NotEmpty 和 @NotNull 注解
#if (${field.fieldType} == 'String')
@NotEmpty(message = "${column.columnComment}不能为空")
#else
@NotNull(message = "${column.columnComment}不能为空")
#end
#end
#if (${column.javaType} == "Date")## 时间类型
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
#end
private ${column.javaType} ${column.javaField};

View File

@ -0,0 +1,31 @@
package ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
## 处理 Date 字段的引入
#foreach ($column in $columns)
#if (${column.createOperation} && ${column.updateOperation} && ${column.listOperationResult}
&& ${column.javaType} == "Date")## 时间类型
import org.springframework.format.annotation.DateTimeFormat;
import static ${DateUtilsClassName}.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
#break
#end
#end
/**
* ${table.classComment} Base VO提供给添加、修改、详细的子 VO 使用
* 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成
*/
@Data
public class ${table.className}BaseVO {
#foreach ($column in $columns)
#if (${column.createOperation} && ${column.updateOperation} && ${column.listOperationResult})##通用操作
#parse("codegen/java/controller/vo/_column.vm")
#end
#end
}

View File

@ -0,0 +1,30 @@
package ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
## 处理 Date 字段的引入
#foreach ($column in $columns)
#if (${column.createOperation} && (!${column.updateOperation} || !${column.listOperationResult})
&& ${column.javaType} == "Date")## 时间类型
import org.springframework.format.annotation.DateTimeFormat;
import static ${DateUtilsClassName}.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
#break
#end
#end
@ApiModel("${table.classComment}创建 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ${table.className}CreateReqVO extends ${table.className}BaseVO {
#foreach ($column in $columns)
#if (${column.createOperation} && (!${column.updateOperation} || !${column.listOperationResult}))##不是通用字段
#parse("codegen/java/controller/vo/_column.vm")
#end
#end
}

View File

@ -0,0 +1,37 @@
package ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import com.alibaba.excel.annotation.ExcelProperty;
#foreach ($column in $columns)
#if ("$!column.dictType" != "")## 有设置数据字典
import ${DictFormatClassName};
import ${DictConvertClassName};
#break
#end
#end
/**
* ${table.classComment} Excel VO
*
* @author ${table.author}
*/
@Data
public class ${table.className}ExcelVO {
#foreach ($column in $columns)
#if (${column.listOperationResult})##返回字段
#if ("$!column.dictType" != "")##处理枚举值
@ExcelProperty(value = "${column.columnComment}", converter = DictConvert.class)
@DictFormat("${column.dictType}") // TODO 代码优化:建议设置到对应的 XXXDictTypeConstants 枚举类中
#else
@ExcelProperty("${column.columnComment}")
#end
private ${column.javaType} ${column.javaField};
#end
#end
}

View File

@ -0,0 +1,42 @@
package ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import ${PageParamClassName};
## 处理 Date 字段的引入
#foreach ($column in $columns)
#if (${column.listOperation} && ${column.javaType} == "Date")## 时间类型
import org.springframework.format.annotation.DateTimeFormat;
import static ${DateUtilsClassName}.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
#break
#end
#end
## 字段模板
#macro(columnTpl $prefix $prefixStr)
#if (${column.javaType} == "Date")## 时间类型
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
#end
@ApiModelProperty(value = "${prefixStr}${column.columnComment}"#if ("$!column.example" != ""), example = "${column.example}"#end)
private ${column.javaType}#if ("$!prefix" != "") ${prefix}${JavaField}#else ${column.javaField}#end;
#end
@ApiModel(value = "${table.classComment} Excel 导出 Request VO", description = "参数和 ${table.className}PageReqVO 是一致的")
@Data
public class ${table.className}ExportReqVO {
#foreach ($column in $columns)
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
#if (${column.listOperation})##查询操作
#if (${column.listOperationCondition} == "BETWEEN")## 情况一Between 的时候
#columnTpl('begin', '开始')
#columnTpl('end', '结束')
#else##情况二,非 Between 的时间
#columnTpl('', '')
#end
#end
#end
}

View File

@ -0,0 +1,44 @@
package ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import ${PageParamClassName};
## 处理 Date 字段的引入
#foreach ($column in $columns)
#if (${column.listOperation} && ${column.javaType} == "Date")## 时间类型
import org.springframework.format.annotation.DateTimeFormat;
import static ${DateUtilsClassName}.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
#break
#end
#end
## 字段模板
#macro(columnTpl $prefix $prefixStr)
#if (${column.javaType} == "Date")## 时间类型
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
#end
@ApiModelProperty(value = "${prefixStr}${column.columnComment}"#if ("$!column.example" != ""), example = "${column.example}"#end)
private ${column.javaType}#if ("$!prefix" != "") ${prefix}${JavaField}#else ${column.javaField}#end;
#end
@ApiModel("${table.classComment}分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ${table.className}PageReqVO extends PageParam {
#foreach ($column in $columns)
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
#if (${column.listOperation})##查询操作
#if (${column.listOperationCondition} == "BETWEEN")## 情况一Between 的时候
#columnTpl('begin', '开始')
#columnTpl('end', '结束')
#else##情况二,非 Between 的时间
#columnTpl('', '')
#end
#end
#end
}

View File

@ -0,0 +1,20 @@
package ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
@ApiModel("${table.classComment} Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ${table.className}RespVO extends ${table.className}BaseVO {
#foreach ($column in $columns)
#if (${column.listOperationResult} && (!${column.createOperation} || !${column.updateOperation}))##不是通用字段
@ApiModelProperty(value = "${column.columnComment}"#if (!${column.nullable}), required = true#end#if ("$!column.example" != ""), example = "${column.example}"#end)
private ${column.javaType} ${column.javaField};
#end
#end
}

View File

@ -0,0 +1,30 @@
package ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
## 处理 Date 字段的引入
#foreach ($column in $columns)
#if (${column.updateOperation} && (!${column.createOperation} || !${column.listOperationResult})
&& ${column.javaType} == "Date"))## 时间类型
import org.springframework.format.annotation.DateTimeFormat;
import static ${DateUtilsClassName}.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
#break
#end
#end
@ApiModel("${table.classComment}更新 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ${table.className}UpdateReqVO extends ${table.className}BaseVO {
#foreach ($column in $columns)
#if (${column.updateOperation} && (!${column.createOperation} || !${column.listOperationResult}))##不是通用字段
#parse("codegen/java/controller/vo/_column.vm")
#end
#end
}

View File

@ -0,0 +1,34 @@
package ${basePackage}.module.${table.moduleName}.convert.${table.businessName};
import java.util.*;
import ${PageResultClassName};
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
/**
* ${table.classComment} Convert
*
* @author ${table.author}
*/
@Mapper
public interface ${table.className}Convert {
${table.className}Convert INSTANCE = Mappers.getMapper(${table.className}Convert.class);
${table.className}DO convert(${table.className}CreateReqVO bean);
${table.className}DO convert(${table.className}UpdateReqVO bean);
${table.className}RespVO convert(${table.className}DO bean);
List<${table.className}RespVO> convertList(List<${table.className}DO> list);
PageResult<${table.className}RespVO> convertPage(PageResult<${table.className}DO> page);
List<${table.className}ExcelVO> convertList02(List<${table.className}DO> list);
}

View File

@ -0,0 +1,38 @@
package ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName};
import lombok.*;
import java.util.*;
import com.baomidou.mybatisplus.annotation.*;
import ${BaseDOClassName};
/**
* ${table.classComment} DO
*
* @author ${table.author}
*/
@TableName("${table.tableName}")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ${table.className}DO extends BaseDO {
#foreach ($column in $columns)
#if (!${baseDOFields.contains(${column.javaField})})##排除 BaseDO 的字段
/**
* ${column.columnComment}
#if ("$!column.dictType" != "")##处理枚举值
*
* 枚举 {@link TODO ${column.dictType} 对应的类}
#end
*/
#if (${column.primaryKey})##处理主键
@TableId#if (${column.javaType} == 'String')type = IdType.INPUT)#end
#end
private ${column.javaType} ${column.javaField};
#end
#end
}

View File

@ -0,0 +1,66 @@
package ${basePackage}.module.${table.moduleName}.dal.mysql.${table.businessName};
import java.util.*;
import ${PageResultClassName};
import ${QueryWrapperClassName};
import ${BaseMapperClassName};
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import org.apache.ibatis.annotations.Mapper;
import ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo.*;
## 字段模板
#macro(listCondition)
#foreach ($column in $columns)
#if (${column.listOperation})
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
#if (${column.listOperationCondition} == "=")##情况一,= 的时候
.eqIfPresent("${column.columnName}", reqVO.get${JavaField}())
#end
#if (${column.listOperationCondition} == "!=")##情况二,!= 的时候
.neIfPresent("${column.columnName}", reqVO.get${JavaField}())
#end
#if (${column.listOperationCondition} == ">")##情况三,> 的时候
.gtIfPresent("${column.columnName}", reqVO.get${JavaField}())
#end
#if (${column.listOperationCondition} == ">=")##情况四,>= 的时候
.geIfPresent("${column.columnName}", reqVO.get${JavaField}())
#end
#if (${column.listOperationCondition} == "<")##情况五,< 的时候
.ltIfPresent("${column.columnName}", reqVO.get${JavaField}())
#end
#if (${column.listOperationCondition} == "<=")##情况五,<= 的时候
.leIfPresent("${column.columnName}", reqVO.get${JavaField}())
#end
#if (${column.listOperationCondition} == "LIKE")##情况七Like 的时候
.likeIfPresent("${column.columnName}", reqVO.get${JavaField}())
#end
#if (${column.listOperationCondition} == "BETWEEN")##情况八Between 的时候
.betweenIfPresent("${column.columnName}", reqVO.getBegin${JavaField}(), reqVO.getEnd${JavaField}())
#end
#end
#end
#end
/**
* ${table.classComment} Mapper
*
* @author ${table.author}
*/
@Mapper
public interface ${table.className}Mapper extends BaseMapperX<${table.className}DO> {
default PageResult<${table.className}DO> selectPage(${table.className}PageReqVO reqVO) {
return selectPage(reqVO, new QueryWrapperX<${table.className}DO>()
#listCondition()
.orderByDesc("id")## 大多数情况下id 倒序
);
}
default List<${table.className}DO> selectList(${table.className}ExportReqVO reqVO) {
return selectList(new QueryWrapperX<${table.className}DO>()
#listCondition()
.orderByDesc("id")## 大多数情况下id 倒序
);
}
}

View File

@ -0,0 +1,2 @@
// ========== ${table.classComment} TODO 补充编号 ==========
ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS = new ErrorCode(TODO 补充编号, "${table.classComment}不存在");

View File

@ -0,0 +1,70 @@
package ${basePackage}.module.${table.moduleName}.service.${table.businessName};
import java.util.*;
import javax.validation.*;
import ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${PageResultClassName};
/**
* ${table.classComment} Service 接口
*
* @author ${table.author}
*/
public interface ${table.className}Service {
/**
* 创建${table.classComment}
*
* @param createReqVO 创建信息
* @return 编号
*/
${primaryColumn.javaType} create${simpleClassName}(@Valid ${table.className}CreateReqVO createReqVO);
/**
* 更新${table.classComment}
*
* @param updateReqVO 更新信息
*/
void update${simpleClassName}(@Valid ${table.className}UpdateReqVO updateReqVO);
/**
* 删除${table.classComment}
*
* @param id 编号
*/
void delete${simpleClassName}(${primaryColumn.javaType} id);
/**
* 获得${table.classComment}
*
* @param id 编号
* @return ${table.classComment}
*/
${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id);
/**
* 获得${table.classComment}列表
*
* @param ids 编号
* @return ${table.classComment}列表
*/
List<${table.className}DO> get${simpleClassName}List(Collection<${primaryColumn.javaType}> ids);
/**
* 获得${table.classComment}分页
*
* @param pageReqVO 分页查询
* @return ${table.classComment}分页
*/
PageResult<${table.className}DO> get${simpleClassName}Page(${table.className}PageReqVO pageReqVO);
/**
* 获得${table.classComment}列表, 用于 Excel 导出
*
* @param exportReqVO 查询条件
* @return ${table.classComment}列表
*/
List<${table.className}DO> get${simpleClassName}List(${table.className}ExportReqVO exportReqVO);
}

View File

@ -0,0 +1,83 @@
package ${basePackage}.module.${table.moduleName}.service.${table.businessName};
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${PageResultClassName};
import ${basePackage}.module.${table.moduleName}.convert.${table.businessName}.${table.className}Convert;
import ${basePackage}.module.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper;
import ${basePackage}.module.${table.moduleName}.service.${table.businessName}.${table.className}Service;
import static ${ServiceExceptionUtilClassName}.exception;
import static ${basePackage}.module.${table.moduleName}.enums.ErrorCodeConstants.*;
/**
* ${table.classComment} Service 实现类
*
* @author ${table.author}
*/
@Service
@Validated
public class ${table.className}ServiceImpl implements ${table.className}Service {
@Resource
private ${table.className}Mapper ${classNameVar}Mapper;
@Override
public ${primaryColumn.javaType} create${simpleClassName}(${table.className}CreateReqVO createReqVO) {
// 插入
${table.className}DO ${classNameVar} = ${table.className}Convert.INSTANCE.convert(createReqVO);
${classNameVar}Mapper.insert(${classNameVar});
// 返回
return ${classNameVar}.getId();
}
@Override
public void update${simpleClassName}(${table.className}UpdateReqVO updateReqVO) {
// 校验存在
this.validate${simpleClassName}Exists(updateReqVO.getId());
// 更新
${table.className}DO updateObj = ${table.className}Convert.INSTANCE.convert(updateReqVO);
${classNameVar}Mapper.updateById(updateObj);
}
@Override
public void delete${simpleClassName}(${primaryColumn.javaType} id) {
// 校验存在
this.validate${simpleClassName}Exists(id);
// 删除
${classNameVar}Mapper.deleteById(id);
}
private void validate${simpleClassName}Exists(${primaryColumn.javaType} id) {
if (${classNameVar}Mapper.selectById(id) == null) {
throw exception(${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
}
}
@Override
public ${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id) {
return ${classNameVar}Mapper.selectById(id);
}
@Override
public List<${table.className}DO> get${simpleClassName}List(Collection<${primaryColumn.javaType}> ids) {
return ${classNameVar}Mapper.selectBatchIds(ids);
}
@Override
public PageResult<${table.className}DO> get${simpleClassName}Page(${table.className}PageReqVO pageReqVO) {
return ${classNameVar}Mapper.selectPage(pageReqVO);
}
@Override
public List<${table.className}DO> get${simpleClassName}List(${table.className}ExportReqVO exportReqVO) {
return ${classNameVar}Mapper.selectList(exportReqVO);
}
}

View File

@ -0,0 +1,161 @@
package ${basePackage}.module.${table.moduleName}.service.${table.businessName};
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import javax.annotation.Resource;
import ${basePackage}.BaseDbUnitTest;
import ${basePackage}.module.${table.moduleName}.service.${table.businessName}.impl.${table.className}ServiceImpl;
import ${basePackage}.module.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${basePackage}.module.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper;
import ${PageResultClassName};
import javax.annotation.Resource;
import org.springframework.context.annotation.Import;
import java.util.*;
import static cn.hutool.core.util.RandomUtil.*;
import static ${basePackage}.module.${table.moduleName}.enums.ErrorCodeConstants.*;
import static ${baseFrameworkPackage}.test.core.util.AssertUtils.*;
import static ${baseFrameworkPackage}.test.core.util.RandomUtils.*;
import static ${ObjectUtilsClassName}.*;
import static ${DateUtilsClassName}.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
## 字段模板
#macro(getPageCondition $VO)
// mock 数据
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class, o -> { // 等会查询到
#foreach ($column in $columns)
#if (${column.listOperation})
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
o.set$JavaField(null);
#end
#end
});
${classNameVar}Mapper.insert(db${simpleClassName});
#foreach ($column in $columns)
#if (${column.listOperation})
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
// 测试 ${column.javaField} 不匹配
${classNameVar}Mapper.insert(cloneIgnoreId(db${simpleClassName}, o -> o.set$JavaField(null)));
#end
#end
// 准备参数
${table.className}${VO} reqVO = new ${table.className}${VO}();
#foreach ($column in $columns)
#if (${column.listOperation})
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
#if (${column.listOperationCondition} == "BETWEEN")## BETWEEN 的情况
reqVO.setBegin${JavaField}(null);
reqVO.setEnd${JavaField}(null);
#else
reqVO.set$JavaField(null);
#end
#end
#end
#end
/**
* {@link ${table.className}ServiceImpl} 的单元测试类
*
* @author ${table.author}
*/
@Import(${table.className}ServiceImpl.class)
public class ${table.className}ServiceTest extends BaseDbUnitTest {
@Resource
private ${table.className}ServiceImpl ${classNameVar}Service;
@Resource
private ${table.className}Mapper ${classNameVar}Mapper;
@Test
public void testCreate${simpleClassName}_success() {
// 准备参数
${table.className}CreateReqVO reqVO = randomPojo(${table.className}CreateReqVO.class);
// 调用
${primaryColumn.javaType} ${classNameVar}Id = ${classNameVar}Service.create${simpleClassName}(reqVO);
// 断言
assertNotNull(${classNameVar}Id);
// 校验记录的属性是否正确
${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(${classNameVar}Id);
assertPojoEquals(reqVO, ${classNameVar});
}
@Test
public void testUpdate${simpleClassName}_success() {
// mock 数据
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class);
${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据
// 准备参数
${table.className}UpdateReqVO reqVO = randomPojo(${table.className}UpdateReqVO.class, o -> {
o.setId(db${simpleClassName}.getId()); // 设置更新的 ID
});
// 调用
${classNameVar}Service.update${simpleClassName}(reqVO);
// 校验是否更新正确
${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, ${classNameVar});
}
@Test
public void testUpdate${simpleClassName}_notExists() {
// 准备参数
${table.className}UpdateReqVO reqVO = randomPojo(${table.className}UpdateReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> ${classNameVar}Service.update${simpleClassName}(reqVO), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
}
@Test
public void testDelete${simpleClassName}_success() {
// mock 数据
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class);
${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据
// 准备参数
${primaryColumn.javaType} id = db${simpleClassName}.getId();
// 调用
${classNameVar}Service.delete${simpleClassName}(id);
// 校验数据不存在了
assertNull(${classNameVar}Mapper.selectById(id));
}
@Test
public void testDelete${simpleClassName}_notExists() {
// 准备参数
${primaryColumn.javaType} id = random${primaryColumn.javaType}Id();
// 调用, 并断言异常
assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
}
@Test // TODO 请修改 null 为需要的值
public void testGet${simpleClassName}Page() {
#getPageCondition("PageReqVO")
// 调用
PageResult<${table.className}DO> pageResult = ${classNameVar}Service.get${simpleClassName}Page(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(db${simpleClassName}, pageResult.getList().get(0));
}
@Test // TODO 请修改 null 为需要的值
public void testGet${simpleClassName}List() {
#getPageCondition("ExportReqVO")
// 调用
List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(db${simpleClassName}, list.get(0));
}
}

View File

@ -0,0 +1,27 @@
-- 菜单 SQL
INSERT INTO `system_menu`(
`name`, `permission`, `menu_type`, `sort`, `parent_id`,
`path`, `icon`, `component`, `status`
)
VALUES (
'${table.classComment}管理', '', 2, 0, ${table.parentMenuId},
'${simpleClassName_strikeCase}', '', '${table.moduleName}/${classNameVar}/index', 0
);
-- 按钮父菜单ID
SELECT @parentId := LAST_INSERT_ID();
-- 按钮 SQL
#set ($functionNames = ['查询', '创建', '更新', '删除', '导出'])
#set ($functionOps = ['query', 'create', 'update', 'delete', 'export'])
#foreach ($functionName in $functionNames)
#set ($index = $foreach.count - 1)
INSERT INTO `system_menu`(
`name`, `permission`, `menu_type`, `sort`, `parent_id`,
`path`, `icon`, `component`, `status`
)
VALUES (
'${table.classComment}${functionName}', '${permissionPrefix}:${functionOps.get($index)}', 3, $foreach.count, @parentId,
'', '', '', 0
);
#end

View File

@ -0,0 +1,55 @@
import request from '@/utils/request'
#set ($baseURL = "/${table.moduleName}/${simpleClassName_strikeCase}")
// 创建${table.classComment}
export function create${simpleClassName}(data) {
return request({
url: '${baseURL}/create',
method: 'post',
data: data
})
}
// 更新${table.classComment}
export function update${simpleClassName}(data) {
return request({
url: '${baseURL}/update',
method: 'put',
data: data
})
}
// 删除${table.classComment}
export function delete${simpleClassName}(id) {
return request({
url: '${baseURL}/delete?id=' + id,
method: 'delete'
})
}
// 获得${table.classComment}
export function get${simpleClassName}(id) {
return request({
url: '${baseURL}/get?id=' + id,
method: 'get'
})
}
// 获得${table.classComment}分页
export function get${simpleClassName}Page(query) {
return request({
url: '${baseURL}/page',
method: 'get',
params: query
})
}
// 导出${table.classComment} Excel
export function export${simpleClassName}Excel(query) {
return request({
url: '${baseURL}/export-excel',
method: 'get',
params: query,
responseType: 'blob'
})
}

View File

@ -0,0 +1,403 @@
<template>
<div class="app-container">
<!-- 搜索工作栏 -->
<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
#foreach($column in $columns)
#if ($column.listOperation)
#set ($dictType=$column.dictType)
#set ($javaField = $column.javaField)
#set ($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#set ($comment=$column.columnComment)
#if ($column.htmlType == "input")
<el-form-item label="${comment}" prop="${javaField}">
<el-input v-model="queryParams.${javaField}" placeholder="请输入${comment}" clearable size="small" @keyup.enter.native="handleQuery"/>
</el-form-item>
#elseif ($column.htmlType == "select" || $column.htmlType == "radio")
<el-form-item label="${comment}" prop="${javaField}">
<el-select v-model="queryParams.${javaField}" placeholder="请选择${comment}" clearable size="small">
#if ("" != $dictType)## 设置了 dictType 数据字典的情况
<el-option v-for="dict in this.getDictDatas(DICT_TYPE.$dictType.toUpperCase())"
:key="dict.value" :label="dict.label" :value="dict.value"/>
#else## 未设置 dictType 数据字典的情况
<el-option label="请选择字典生成" value="" />
#end
</el-select>
</el-form-item>
#elseif($column.htmlType == "datetime")
#if ($column.listOperationCondition != "BETWEEN")## 非范围
<el-form-item label="${comment}" prop="${javaField}">
<el-date-picker clearable size="small" v-model="queryParams.${javaField}" type="date" value-format="yyyy-MM-dd" placeholder="选择${comment}" />
</el-form-item>
#else## 范围
<el-form-item label="${comment}">
<el-date-picker v-model="dateRange${AttrName}" size="small" style="width: 240px" value-format="yyyy-MM-dd"
type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
</el-form-item>
#end
#end
#end
#end
<el-form-item>
<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
</el-form-item>
</el-form>
<!-- 操作工具栏 -->
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
v-hasPermi="['${permissionPrefix}:create']">新增</el-button>
</el-col>
<el-col :span="1.5">
<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
v-hasPermi="['${permissionPrefix}:export']">导出</el-button>
</el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
</el-row>
<!-- 列表 -->
<el-table v-loading="loading" :data="list">
#foreach($column in $columns)
#if ($column.listOperationResult)
#set ($dictType=$column.dictType)
#set ($javaField = $column.javaField)
#set ($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#set ($comment=$column.columnComment)
#if ($column.javaType == "Date")## 时间类型
<el-table-column label="${comment}" align="center" prop="${javaField}" width="180">
<template slot-scope="scope">
<span>{{ parseTime(scope.row.${javaField}) }}</span>
</template>
</el-table-column>
#elseif("" != $column.dictType)## 数据字典
<el-table-column label="${comment}" align="center" prop="${javaField}">
<template slot-scope="scope">
<span>{{ getDictDataLabel(DICT_TYPE.$dictType.toUpperCase(), scope.row.${column.javaField}) }}</span>
</template>
</el-table-column>
#else
<el-table-column label="${comment}" align="center" prop="${javaField}" />
#end
#end
#end
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope">
<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
v-hasPermi="['${permissionPrefix}:update']">修改</el-button>
<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
v-hasPermi="['${permissionPrefix}:delete']">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页组件 -->
<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
@pagination="getList"/>
<!-- 对话框(添加 / 修改) -->
<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
#foreach($column in $columns)
#if ($column.createOperation || $column.updateOperation)
#set ($dictType = $column.dictType)
#set ($javaField = $column.javaField)
#set ($AttrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#set ($comment = $column.columnComment)
#if ($column.htmlType == "input")
#if (!$column.primaryKey)## 忽略主键,不用在表单里
<el-form-item label="${comment}" prop="${javaField}">
<el-input v-model="form.${javaField}" placeholder="请输入${comment}" />
</el-form-item>
#end
#elseif($column.htmlType == "imageUpload")## 图片上传
#set ($hasImageUploadColumn = true)
<el-form-item label="${comment}">
<imageUpload v-model="form.${javaField}"/>
</el-form-item>
#elseif($column.htmlType == "fileUpload")## 文件上传
#set ($hasFileUploadColumn = true)
<el-form-item label="${comment}">
<fileUpload v-model="form.${javaField}"/>
</el-form-item>
#elseif($column.htmlType == "editor")## 文本编辑器
#set ($hasEditorColumn = true)
<el-form-item label="${comment}">
<editor v-model="form.${javaField}" :min-height="192"/>
</el-form-item>
#elseif($column.htmlType == "select")## 下拉框
<el-form-item label="${comment}" prop="${javaField}">
<el-select v-model="form.${javaField}" placeholder="请选择${comment}">
#if ("" != $dictType)## 有数据字典
<el-option v-for="dict in this.getDictDatas(DICT_TYPE.$dictType.toUpperCase())"
:key="dict.value" :label="dict.label" #if ($column.javaType == "Integer" || $column.javaType == "Long"):value="parseInt(dict.value)"#else:value="dict.value"#end />
#else##没数据字典
<el-option label="请选择字典生成" value="" />
#end
</el-select>
</el-form-item>
#elseif($column.htmlType == "checkbox")## 多选框
<el-form-item label="${comment}" prop="${javaField}">
<el-checkbox-group v-model="form.${javaField}">
#if ("" != $dictType)## 有数据字典
<el-checkbox v-for="dict in this.getDictDatas(DICT_TYPE.$dictType.toUpperCase())"
:key="dict.value" #if($column.javaType == "Integer" || $column.javaType == "Long"):label="parseInt(dict.value)"#else:label="dict.value"#end>{{dict.label}}</el-checkbox>
#else##没数据字典
<el-checkbox>请选择字典生成</el-checkbox>
#end
</el-checkbox-group>
</el-form-item>
#elseif($column.htmlType == "radio")## 单选框
<el-form-item label="${comment}" prop="${javaField}">
<el-radio-group v-model="form.${javaField}">
#if ("" != $dictType)## 有数据字典
<el-radio v-for="dict in this.getDictDatas(DICT_TYPE.$dictType.toUpperCase())"
:key="dict.value" #if($column.javaType == "Integer" || $column.javaType == "Long"):label="parseInt(dict.value)"#else:label="dict.value"#end>{{dict.label}}</el-radio>
#else##没数据字典
<el-radio label="1">请选择字典生成</el-radio>
#end
</el-radio-group>
</el-form-item>
#elseif($column.htmlType == "datetime")## 时间框
<el-form-item label="${comment}" prop="${javaField}">
<el-date-picker clearable size="small" v-model="form.${javaField}" type="date" value-format="yyyy-MM-dd" placeholder="选择${comment}" />
</el-form-item>
#elseif($column.htmlType == "textarea")## 文本框
<el-form-item label="${comment}" prop="${javaField}">
<el-input v-model="form.${javaField}" type="textarea" placeholder="请输入内容" />
</el-form-item>
#end
#end
#end
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitForm">确 定</el-button>
<el-button @click="cancel">取 消</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { create${simpleClassName}, update${simpleClassName}, delete${simpleClassName}, get${simpleClassName}, get${simpleClassName}Page, export${simpleClassName}Excel } from "@/api/${table.moduleName}/${classNameVar}";
#if ($hasImageUploadColumn)
import ImageUpload from '@/components/ImageUpload';
#end
#if ($hasFileUploadColumn)
import FileUpload from '@/components/FileUpload';
#end
#if ($hasEditorColumn)
import Editor from '@/components/Editor';
#end
export default {
name: "${simpleClassName}",
components: {
#if ($hasImageUploadColumn)
ImageUpload,
#end
#if ($hasFileUploadColumn)
FileUpload,
#end
#if ($hasEditorColumn)
Editor,
#end
},
data() {
return {
// 遮罩层
loading: true,
// 显示搜索条件
showSearch: true,
// 总条数
total: 0,
// ${table.classComment}列表
list: [],
// 弹出层标题
title: "",
// 是否显示弹出层
open: false,
#foreach ($column in $columns)## 时间范围
#if ($column.listOperation)
#if ($column.htmlType == "datetime" && $column.listOperationCondition == "BETWEEN")
#set ($AttrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
dateRange${AttrName}: [],
#end
#end
#end
// 查询参数
queryParams: {
pageNo: 1,
pageSize: 10,
#foreach ($column in $columns)
#if ($column.listOperation && $column.listOperationCondition != 'BETWEEN')
$column.javaField: null,
#end
#end
},
// 表单参数
form: {},
// 表单校验
rules: {
#foreach ($column in $columns)
#if (($column.createOperation || $column.updateOperation) && !$column.nullable && !${column.primaryKey})## 创建或者更新操作 && 要求非空 && 非主键
#set($comment=$column.columnComment)
$column.javaField: [{ required: true, message: "${comment}不能为空", trigger: #if($column.htmlType == "select")"change"#else"blur"#end }],
#end
#end
}
};
},
created() {
this.getList();
},
methods: {
/** 查询列表 */
getList() {
this.loading = true;
// 处理查询参数
let params = {...this.queryParams};
#foreach ($column in $columns)
#if ($column.listOperation)
#if ($column.htmlType == "datetime" && $column.listOperationCondition == "BETWEEN")
#set ($AttrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
this.addBeginAndEndTime(params, this.dateRange${AttrName}, '${column.javaField}');
#end
#end
#end
// 执行查询
get${simpleClassName}Page(params).then(response => {
this.list = response.data.list;
this.total = response.data.total;
this.loading = false;
});
},
/** 取消按钮 */
cancel() {
this.open = false;
this.reset();
},
/** 表单重置 */
reset() {
this.form = {
#foreach ($column in $columns)
#if ($column.createOperation || $column.updateOperation)
#if ($column.htmlType == "checkbox")
$column.javaField: [],
#else
$column.javaField: undefined,
#end
#end
#end
};
this.resetForm("form");
},
/** 搜索按钮操作 */
handleQuery() {
this.queryParams.pageNo = 1;
this.getList();
},
/** 重置按钮操作 */
resetQuery() {
#foreach ($column in $columns)
#if ($column.listOperation)
#if ($column.htmlType == "datetime" && $column.listOperationCondition == "BETWEEN")
#set ($AttrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
this.dateRange${AttrName} = [];
#end
#end
#end
this.resetForm("queryForm");
this.handleQuery();
},
/** 新增按钮操作 */
handleAdd() {
this.reset();
this.open = true;
this.title = "添加${table.classComment}";
},
/** 修改按钮操作 */
handleUpdate(row) {
this.reset();
const ${primaryColumn.javaField} = row.${primaryColumn.javaField};
get${simpleClassName}(${primaryColumn.javaField}).then(response => {
this.form = response.data;
#foreach ($column in $columns)
#if($column.htmlType == "checkbox")## checkbox 特殊处理
this.form.$column.javaField = this.form.${column.javaField}.split(",");
#end
#end
this.open = true;
this.title = "修改${table.classComment}";
});
},
/** 提交按钮 */
submitForm() {
this.#[[$]]#refs["form"].validate(valid => {
if (!valid) {
return;
}
#foreach ($column in $columns)
#if($column.htmlType == "checkbox")
this.form.$column.javaField = this.form.${column.javaField}.join(",");
#end
#end
// 修改的提交
if (this.form.${primaryColumn.javaField} != null) {
update${simpleClassName}(this.form).then(response => {
this.msgSuccess("修改成功");
this.open = false;
this.getList();
});
return;
}
// 添加的提交
create${simpleClassName}(this.form).then(response => {
this.msgSuccess("新增成功");
this.open = false;
this.getList();
});
});
},
/** 删除按钮操作 */
handleDelete(row) {
const ${primaryColumn.javaField} = row.${primaryColumn.javaField};
this.$confirm('是否确认删除${table.classComment}编号为"' + ${primaryColumn.javaField} + '"的数据项?', "警告", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(function() {
return delete${simpleClassName}(${primaryColumn.javaField});
}).then(() => {
this.getList();
this.msgSuccess("删除成功");
})
},
/** 导出按钮操作 */
handleExport() {
// 处理查询参数
let params = {...this.queryParams};
params.pageNo = undefined;
params.pageSize = undefined;
#foreach ($column in $columns)
#if ($column.listOperation)
#if ($column.htmlType == "datetime" && $column.listOperationCondition == "BETWEEN")
#set ($AttrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
this.addBeginAndEndTime(params, this.dateRange${AttrName}, '${column.javaField}');
#end
#end
#end
// 执行导出
this.$confirm('是否确认导出所有${table.classComment}数据项?', "警告", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(function() {
return export${simpleClassName}Excel(params);
}).then(response => {
this.downloadExcel(response, '${table.classComment}.xls');
})
}
}
};
</script>