!6 fix: 代码生成字段和模板引用包缺失问题修复

Merge pull request !6 from Kylin/develop
This commit is contained in:
郝先瑞
2024-07-24 11:57:22 +00:00
committed by Gitee
38 changed files with 1077 additions and 137 deletions

View File

@@ -1,8 +1,6 @@
package com.youlai.system.common.base;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
@@ -14,7 +12,7 @@ import java.time.LocalDateTime;
/**
* 基础实体类
*
* <p>实体类的基类,包含了实体类的公共属性,如创建时间、更新时间、逻辑删除标识等</p>
* <p>实体类的基类,包含了实体类的公共属性,如创建时间、更新时间</p>
*
* @author Ray
* @since 2024/6/23
@@ -25,6 +23,13 @@ public class BaseEntity implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 创建时间
*/
@@ -41,9 +46,5 @@ public class BaseEntity implements Serializable {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
/**
* 逻辑删除标识 (0-未删除 1-已删除)
*/
@TableLogic(value = "0", delval = "1")
private Integer isDeleted;
}

View File

@@ -1,5 +1,6 @@
package com.youlai.system.config.property;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.map.MapUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -30,6 +31,11 @@ public class GeneratorProperties {
private String packageName;
/**
* 文件扩展名,如 .java
*/
private String extension= FileNameUtil.EXT_JAVA;
}

View File

@@ -3,19 +3,15 @@ package com.youlai.system.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.form.GenConfigForm;
import com.youlai.system.model.query.TablePageQuery;
import com.youlai.system.model.vo.TableColumnVO;
import com.youlai.system.model.vo.TableGeneratePreviewVO;
import com.youlai.system.model.vo.GeneratorPreviewVO;
import com.youlai.system.model.vo.TablePageVO;
import com.youlai.system.service.GeneratorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -36,21 +32,26 @@ public class GeneratorController {
return PageResult.success(result);
}
@Operation(summary = "获取数据表字段列表")
@GetMapping("/table/{tableName}/columns")
public Result<List<TableColumnVO>> getTableColumns(
@Parameter(description = "表名", example = "sys_user") @PathVariable String tableName
) {
List<TableColumnVO> list = generatorService.getTableColumns(tableName);
@Operation(summary = "获取预览生成代码")
@GetMapping("/{tableName}/preview")
public Result<List<GeneratorPreviewVO>> getTablePreviewData(@PathVariable String tableName) {
List<GeneratorPreviewVO> list = generatorService.getTablePreviewData(tableName);
return Result.success(list);
}
@Operation(summary = "获取预览生成代码")
@GetMapping("/table/{tableName}/preview")
public Result<List<TableGeneratePreviewVO>> getTablePreviewData(@PathVariable String tableName) {
List<TableGeneratePreviewVO> list = generatorService.getTablePreviewData(tableName);
return Result.success(list);
@Operation(summary = "获取代码生成配置")
@GetMapping("/{tableName}/config")
public Result<GenConfigForm> getGenConfig(@PathVariable String tableName) {
GenConfigForm formData = generatorService.getGenConfig(tableName);
return Result.success(formData);
}
@Operation(summary = "保存代码生成配置")
@PostMapping("/{tableName}/config")
public Result saveGenCodeConfig(@RequestBody GenConfigForm formData) {
boolean result = generatorService.saveGenCodeConfig(formData);
return Result.judge(result);
}
}

View File

@@ -16,7 +16,7 @@ public interface DeptConverter {
DeptForm toForm(SysDept entity);
DeptVO convertToVo(SysDept entity);
DeptVO toVo(SysDept entity);
SysDept toEntity(DeptForm deptForm);

View File

@@ -7,7 +7,7 @@ import com.youlai.system.model.vo.DictPageVO;
import org.mapstruct.Mapper;
/**
* 字典 对象转换器
* 字典对象转换器
*
* @author Ray Hao
* @since 2022/6/8
@@ -15,7 +15,7 @@ import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface DictConverter {
Page<DictPageVO> convertToPageVo(Page<SysDict> page);
Page<DictPageVO> toPageVo(Page<SysDict> page);
DictForm toForm(SysDict entity);

View File

@@ -0,0 +1,45 @@
package com.youlai.system.converter;
import com.youlai.system.model.entity.GenConfig;
import com.youlai.system.model.entity.GenFieldConfig;
import com.youlai.system.model.form.GenConfigForm;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import java.util.List;
/**
* 代码生成配置转换器
*
* @author Ray
* @since 2.10.0
*/
@Mapper(componentModel = "spring")
public interface GenConfigConverter {
@Mapping(source = "genConfig.tableName", target = "tableName")
@Mapping(source = "genConfig.comment", target = "comment")
@Mapping(source = "genConfig.moduleName", target = "moduleName")
@Mapping(source = "genConfig.packageName", target = "packageName")
@Mapping(source = "genConfig.entityName", target = "entityName")
@Mapping(source = "genConfig.author", target = "author")
@Mapping(source = "fieldConfigs", target = "fieldConfigs")
GenConfigForm toGenConfigForm(GenConfig genConfig, List<GenFieldConfig> fieldConfigs);
List<GenConfigForm.FieldConfig> toFieldConfigList(List<GenFieldConfig> fieldConfigs);
@Mapping(source = "configId", target = "configId")
@Mapping(source = "columnName", target = "columnName")
@Mapping(source = "columnType", target = "columnType")
@Mapping(source = "fieldName", target = "fieldName")
@Mapping(source = "fieldType", target = "fieldType")
@Mapping(source = "comment", target = "comment")
@Mapping(source = "formType", target = "formType")
@Mapping(source = "queryType", target = "queryType")
@Mapping(source = "isShowInList", target = "isShowInList")
@Mapping(source = "isShowInForm", target = "isShowInForm")
@Mapping(source = "isShowInQuery", target = "isShowInQuery")
@Mapping(source = "isRequired", target = "isRequired")
GenConfigForm.FieldConfig toFieldConfig(GenFieldConfig genFieldConfig);
}

View File

@@ -0,0 +1,70 @@
package com.youlai.system.enums;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.youlai.system.common.base.IBaseEnum;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* 表单类型枚举
*
* @author Ray
* @since 2.10.0
*/
@Getter
@RequiredArgsConstructor
public enum FormTypeEnum implements IBaseEnum<Integer> {
/**
* 输入框
*/
INPUT(1, "输入框"),
/**
* 下拉框
*/
SELECT(2, "下拉框"),
/**
* 单选框
*/
RADIO(3, "单选框"),
/**
* 数字输入框
*/
INPUT_NUMBER(4, "数字输入框"),
/**
* 开关
*/
SWITCH(5, "开关"),
/**
* 复选框
*/
CHECK_BOX(6, "复选框"),
/**
* 文本域
*/
TEXT_AREA(7, "文本域"),
/**
* 日期时间框
*/
DATE_TIME(8, "日期时间框"),
/**
* 日期框
*/
DATE(9, "日期框"),;
// Mybatis-Plus 提供注解表示插入数据库时插入该值
@EnumValue
private final Integer value;
// @JsonValue // 表示对枚举序列化时返回此字段
private final String label;
}

View File

@@ -0,0 +1,58 @@
package com.youlai.system.enums;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.youlai.system.common.base.IBaseEnum;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* 查询类型枚举
*
* @author Ray
* @since 2.10.0
*/
@Getter
@RequiredArgsConstructor
public enum QueryTypeEnum implements IBaseEnum<Integer> {
EQ(1, "="),
NE(2, "!="),
GT(3, ">"),
GE(4, ">="),
LT(5, "<"),
LE(6, "<="),
BETWEEN(7, "BETWEEN"),
LIKE(8, "LIKE '%s%'"),
LIKE_LEFT(9, "LIKE '%s'"),
LIKE_RIGHT(10, "LIKE 's%'"),
IN(11, "IN"),
NOT_IN(12, "NOT IN"),
IS_NULL(13, "IS NULL"),
IS_NOT_NULL(14, "IS NOT NULL")
;
// Mybatis-Plus 提供注解表示插入数据库时插入该值
@EnumValue
private final Integer value;
// @JsonValue // 表示对枚举序列化时返回此字段
private final String label;
}

View File

@@ -11,6 +11,12 @@ import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* 数据库 Mapper 接口
*
* @author ray
* @since 2.10.0
*/
@Mapper
public interface DatabaseMapper extends BaseMapper<SysDept> {

View File

@@ -0,0 +1,20 @@
package com.youlai.system.mapper;
import com.youlai.system.model.entity.GenConfig;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* 代码生成基础配置访问层
*
* @author Ray
* @since 2.10.0
*/
@Mapper
public interface GenConfigMapper extends BaseMapper<GenConfig> {
}

View File

@@ -0,0 +1,20 @@
package com.youlai.system.mapper;
import com.youlai.system.model.entity.GenFieldConfig;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* 代码生成字段配置访问层
*
* @author Ray
* @since 2.10.0
*/
@Mapper
public interface GenFieldConfigMapper extends BaseMapper<GenFieldConfig> {
}

View File

@@ -0,0 +1,53 @@
package com.youlai.system.model.entity;
import com.baomidou.mybatisplus.annotation.*;
import com.youlai.system.common.base.BaseEntity;
import lombok.Data;
/**
* 代码生成基础配置
*/
@TableName(value ="gen_config")
@Data
public class GenConfig extends BaseEntity {
/**
* 表名
*/
private String tableName;
/**
* 包名
*/
private String packageName;
/**
* 模块名
*/
private String moduleName;
/**
* 实体名
*/
private String entityName;
/**
* 类描述
*/
private String comment;
/**
* 上级菜单ID
*/
private Long parentMenuId;
/**
* 作者
*/
private String author;
@TableLogic
private Integer isDeleted;
}

View File

@@ -0,0 +1,88 @@
package com.youlai.system.model.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.youlai.system.common.base.BaseEntity;
import com.youlai.system.enums.FormTypeEnum;
import com.youlai.system.enums.QueryTypeEnum;
import lombok.Getter;
import lombok.Setter;
/**
* 字段生成配置实体
*
* @author Ray
* @since 2.10.0
*/
@TableName(value = "gen_field_config")
@Getter
@Setter
public class GenFieldConfig extends BaseEntity {
/**
* 关联的配置ID
*/
private Long configId;
/**
* 列名
*/
private String columnName;
/**
* 列类型
*/
private String columnType;
/**
* 字段长度
*/
private String columnLength;
/**
* 字段名称
*/
private String fieldName;
/**
* 字段类型
*/
private String fieldType;
/**
* 字段描述
*/
private String comment;
/**
* 表单类型
*/
private FormTypeEnum formType;
/**
* 查询方式
*/
private QueryTypeEnum queryType;
/**
* 是否在列表显示
*/
private Integer isShowInList;
/**
* 是否在表单显示
*/
private Integer isShowInForm;
/**
* 是否在查询条件显示
*/
private Integer isShowInQuery;
/**
* 是否必填
*/
private Integer isRequired;
}

View File

@@ -3,6 +3,7 @@ package com.youlai.system.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.youlai.system.common.base.BaseEntity;
import lombok.Getter;
import lombok.Setter;
@@ -16,11 +17,6 @@ import lombok.Setter;
@Getter
@Setter
public class SysDept extends BaseEntity {
/**
* 主键
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 部门名称
@@ -62,4 +58,10 @@ public class SysDept extends BaseEntity {
*/
private Long updateBy;
/**
* 逻辑删除标识 (0-未删除 1-已删除)
*/
@TableLogic
private Boolean isDeleted;
}

View File

@@ -2,6 +2,7 @@ package com.youlai.system.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.youlai.system.common.base.BaseEntity;
import lombok.Data;
@@ -13,11 +14,6 @@ import lombok.Data;
*/
@Data
public class SysDict extends BaseEntity {
/**
* 主键
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 类型名称
@@ -39,4 +35,10 @@ public class SysDict extends BaseEntity {
*/
private String remark;
/**
* 逻辑删除标识 (0-未删除 1-已删除)
*/
@TableLogic(value = "0", delval = "1")
private Integer isDeleted;
}

View File

@@ -2,6 +2,7 @@ package com.youlai.system.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.youlai.system.common.base.BaseEntity;
import lombok.Getter;
import lombok.Setter;
@@ -55,4 +56,10 @@ public class SysRole extends BaseEntity {
* 更新人 ID
*/
private Long updateBy;
/**
* 逻辑删除标识 (0-未删除 1-已删除)
*/
@TableLogic(value = "0", delval = "1")
private Boolean isDeleted;
}

View File

@@ -2,6 +2,7 @@ package com.youlai.system.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.youlai.system.common.base.BaseEntity;
import lombok.Getter;
import lombok.Setter;
@@ -72,4 +73,10 @@ public class SysUser extends BaseEntity {
* 更新人 ID
*/
private Long updateBy;
/**
* 逻辑删除标识 (0-未删除 1-已删除)
*/
@TableLogic(value = "0", delval = "1")
private Integer isDeleted;
}

View File

@@ -1,5 +1,7 @@
package com.youlai.system.model.form;
import com.youlai.system.enums.FormTypeEnum;
import com.youlai.system.enums.QueryTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@@ -7,53 +9,67 @@ import java.util.List;
@Schema(description = "代码生成配置表单")
@Data
public class GeneratorConfigForm {
public class GenConfigForm {
@Schema(description = "表名")
private String tableName;
@Schema(description = "实体名")
private String entityName;
@Schema(description = "包名")
private String packageName;
@Schema(description = "类描述")
private String comment;
@Schema(description = "模块名")
private String moduleName;
@Schema(description = "包名")
private String packageName;
@Schema(description = "实体名")
private String entityName;
@Schema(description = "作者")
private String author;
@Schema(description = "字段配置")
@Schema(description = "字段配置列表")
private List<FieldConfig> fieldConfigs;
@Schema(description = "字段配置")
@Data
public static class FieldConfig {
@Schema(description = "ID")
private Long configId;
@Schema(description = "字段名称")
private String name;
@Schema(description = "列名")
private String columnName;
@Schema(description = "列类型")
private String columnType;
@Schema(description = "字段名")
private String fieldName;
@Schema(description = "字段类型")
private String type;
private String fieldType;
@Schema(description = "字段描述")
private String description;
private String comment;
@Schema(description = "是否在列表显示")
private Boolean showInList;
private Integer isShowInList;
@Schema(description = "是否在表单显示")
private Boolean showInForm;
private Integer isShowInForm;
@Schema(description = "是否在查询条件显示")
private Boolean showInQuery;
private Integer isShowInQuery;
@Schema(description = "是否必填")
private Integer isRequired;
@Schema(description = "表单类型")
private String formType;
private FormTypeEnum formType;
@Schema(description = "查询方式")
private String queryMethod;
@Schema(description = "查询类型")
private QueryTypeEnum queryType;
}
}

View File

@@ -5,7 +5,7 @@ import lombok.Data;
@Schema(description = "表生成代码预览VO")
@Data
public class TableGeneratePreviewVO {
public class GeneratorPreviewVO {
@Schema(description = "生成文件路径")
private String path;

View File

@@ -1,15 +1,16 @@
package com.youlai.system.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.model.form.GenConfigForm;
import com.youlai.system.model.query.TablePageQuery;
import com.youlai.system.model.vo.TableColumnVO;
import com.youlai.system.model.vo.TableGeneratePreviewVO;
import com.youlai.system.model.vo.GeneratorPreviewVO;
import com.youlai.system.model.vo.TablePageVO;
import java.util.List;
/**
* 数据库服务接口
* 代码生成业务接口
*
* @author haoxr
* @since 2.11.0
@@ -39,5 +40,21 @@ public interface GeneratorService {
* @param tableName 表名
* @return
*/
List<TableGeneratePreviewVO> getTablePreviewData(String tableName);
List<GeneratorPreviewVO> getTablePreviewData(String tableName);
/**
* 获取代码生成配置
*
* @param tableName 表名
* @return
*/
GenConfigForm getGenConfig(String tableName);
/**
* 保存代码生成配置
*
* @param formData 表单数据
* @return
*/
boolean saveGenCodeConfig(GenConfigForm formData);
}

View File

@@ -1,18 +1,30 @@
package com.youlai.system.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.config.property.GeneratorProperties;
import com.youlai.system.mapper.DatabaseMapper;
import com.youlai.system.mapper.GenConfigMapper;
import com.youlai.system.mapper.GenFieldConfigMapper;
import com.youlai.system.model.entity.GenConfig;
import com.youlai.system.model.entity.GenFieldConfig;
import com.youlai.system.model.form.GenConfigForm;
import com.youlai.system.model.query.TablePageQuery;
import com.youlai.system.model.vo.TableColumnVO;
import com.youlai.system.model.vo.TableGeneratePreviewVO;
import com.youlai.system.model.vo.GeneratorPreviewVO;
import com.youlai.system.model.vo.TablePageVO;
import com.youlai.system.service.GeneratorService;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import cn.hutool.extra.template.TemplateConfig.ResourceMode;
@@ -31,6 +43,15 @@ public class GeneratorServiceImpl implements GeneratorService {
private final DatabaseMapper databaseMapper;
private final GeneratorProperties generatorProperties;
private final GenConfigMapper genConfigMapper;
private final GenFieldConfigMapper genFieldConfigMapper;
// 注入 spring.application.name
@Value("${spring.application.name}")
private String applicationName;
/**
* 数据表分页列表
*
@@ -53,6 +74,39 @@ public class GeneratorServiceImpl implements GeneratorService {
return databaseMapper.getTableColumns(tableName);
}
/**
* 获取代码生成配置
*
* @param tableName 表名 eg: sys_user
* @return 代码生成配置
*/
@Override
public GenConfigForm getGenConfig(String tableName) {
// 查询表生成配置
GenConfig genConfig = genConfigMapper.selectOne(
new LambdaQueryWrapper<>(GenConfig.class)
.eq(GenConfig::getTableName, tableName)
.last("LIMIT 1")
);
// 查询字段生成配置
List<GenFieldConfig> fieldConfigs = genFieldConfigMapper.selectList(
new LambdaQueryWrapper<>(GenFieldConfig.class)
.eq(GenFieldConfig::getConfigId, genConfig.getId())
);
GenConfigForm genConfigForm = new GenConfigForm();
return null;
}
@Override
public boolean saveGenCodeConfig(GenConfigForm formData) {
return false;
}
/**
* 获取预览生成代码
*
@@ -60,49 +114,133 @@ public class GeneratorServiceImpl implements GeneratorService {
* @return 预览数据
*/
@Override
public List<TableGeneratePreviewVO> getTablePreviewData(String tableName) {
public List<GeneratorPreviewVO> getTablePreviewData(String tableName) {
List<TableGeneratePreviewVO> list = new ArrayList<>();
List<GeneratorPreviewVO> list = new ArrayList<>();
TemplateConfig templateConfig = new TemplateConfig("templates" , ResourceMode.CLASSPATH);
TemplateEngine templateEngine = TemplateUtil.createEngine(templateConfig);
GenConfig genConfig = genConfigMapper.selectOne(new LambdaQueryWrapper<GenConfig>()
.eq(GenConfig::getTableName, tableName)
);
Assert.isTrue(genConfig != null, "未找到表生成配置");
List<GenFieldConfig> fieldConfigs = genFieldConfigMapper.selectList(new LambdaQueryWrapper<GenFieldConfig>()
.eq(GenFieldConfig::getConfigId, genConfig.getId())
);
Assert.isTrue(CollectionUtil.isNotEmpty(fieldConfigs), "未找到字段生成配置");
// 遍历模板配置
Map<String, GeneratorProperties.TemplateConfig> templateConfigs = generatorProperties.getTemplateConfigs();
for (Map.Entry<String, GeneratorProperties.TemplateConfig> templateConfigEntry : templateConfigs.entrySet()) {
GeneratorPreviewVO previewVO = new GeneratorPreviewVO();
GeneratorProperties.TemplateConfig templateConfig = templateConfigEntry.getValue();
/* 1. 生成文件名 UserController */
// User Role Menu Dept
String entityName = genConfig.getEntityName();
// Controller Service Mapper Entity
String templateName = templateConfigEntry.getKey();
// .java .ts .vue
String extension = templateConfig.getExtension();
// 文件名 UserController.java
String fileName = getFileName(entityName, templateName, extension);
previewVO.setFileName(fileName);
Map<String, Object> bindingMap = new HashMap<>();
bindingMap.put("tableName", "sys_user");
bindingMap.put("author", "Ray");
bindingMap.put("date", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));
bindingMap.put("entityName", "User" );
bindingMap.put("lowerFirstEntityName", "user");
bindingMap.put("tableComment", "用户");
/* 2. 生成文件路径 */
// com.youlai.system
String packageName = genConfig.getPackageName();
// controller
String subPackageName = templateConfig.getPackageName();
// 文件路径 com.youlai.system.controller
String filePath = getFilePath(templateName, packageName, subPackageName);
previewVO.setPath(filePath);
// 包路径
bindingMap.put("package", "com.youlai.system");
/* 3. 生成文件内容 */
Template template = templateEngine.getTemplate("generator" + File.separator + "controller.java.vm");
String content = template.render(bindingMap);
TableGeneratePreviewVO controller = new TableGeneratePreviewVO();
controller.setPath("youlai-boot/controller");
controller.setContent(content);
controller.setFileName("UserController.java");
// 生成文件内容
String content = getCodeContent(templateConfig, genConfig, fieldConfigs);
previewVO.setContent(content);
list.add(controller);
TableGeneratePreviewVO vo = new TableGeneratePreviewVO();
vo.setPath("youlai-boot/model/vo");
vo.setContent(content);
vo.setFileName("UserVO.java");
list.add(vo);
list.add(previewVO);
}
return list;
}
private String generatePath(){
private String getFileName(String entityName, String templateName, String extension) {
if (templateName.equals("Entity")) {
return entityName + extension;
}
if (templateName.equals("MapperXml")) {
return entityName + "Mapper" + extension;
}
if (templateName.equals("API") || templateName.equals("VIEW")) {
return StrUtil.toSymbolCase(entityName, '-') + extension;
}
return entityName + templateName + extension;
}
private String getFilePath(String templateName, String packageName, String subPackageName) {
String path;
if (templateName.equals("MapperXml")) {
path = (applicationName
+ File.separator
+ "src" + File.separator + "main" + File.separator + "resources"
+ File.separator + subPackageName
).replace(".", File.separator);
} else if (templateName.equals("API") || templateName.equals("VIEW")) {
path = ("vue3-element-admin"
+ File.separator
+ "src" + File.separator + subPackageName
).replace(".", File.separator);
} else {
path = (applicationName
+ File.separator
+ "src" + File.separator + "main" + File.separator + "java"
+ File.separator + packageName + File.separator + subPackageName
).replace(".", File.separator);
}
return path;
}
/**
* 生成代码内容
*
* @param templateConfig 模板配置
* @param genConfig 生成配置
* @param fieldConfigs 字段配置
* @return 代码内容
*/
private String getCodeContent(GeneratorProperties.TemplateConfig templateConfig, GenConfig genConfig, List<GenFieldConfig> fieldConfigs) {
Map<String, Object> bindMap = new HashMap<>();
String entityName = genConfig.getEntityName();
bindMap.put("package", genConfig.getPackageName());
bindMap.put("subPackage", templateConfig.getPackageName());
bindMap.put("date", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));
bindMap.put("entityName", entityName);
bindMap.put("tableName", genConfig.getTableName());
bindMap.put("author", genConfig.getAuthor());
bindMap.put("lowerFirstEntityName", StrUtil.lowerFirst(entityName));
bindMap.put("tableComment", StrUtil.replace(genConfig.getComment(), "", Strings.EMPTY));
bindMap.put("fieldConfigs", fieldConfigs);
for (GenFieldConfig fieldConfig : fieldConfigs) {
bindMap.put("hasLocalDateTime", "LocalDateTime".equals(fieldConfig.getFieldType()));
bindMap.put("hasBigDecimal", "BigDecimal".equals(fieldConfig.getFieldType()));
bindMap.put("hasRequiredField", Boolean.TRUE.equals(fieldConfig.getIsRequired()));
}
TemplateEngine templateEngine = TemplateUtil.createEngine(new TemplateConfig("templates", ResourceMode.CLASSPATH));
Template template = templateEngine.getTemplate(templateConfig.getTemplatePath());
String content = template.render(bindMap);
return content;
}
}

View File

@@ -85,7 +85,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
return deptList.stream()
.filter(dept -> dept.getParentId().equals(parentId))
.map(dept -> {
DeptVO deptVO = deptConverter.convertToVo(dept);
DeptVO deptVO = deptConverter.toVo(dept);
List<DeptVO> children = recurDeptList(dept.getId(), deptList);
deptVO.setChildren(children);
return deptVO;

View File

@@ -157,7 +157,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
* 删除用户
*
* @param idsStr 用户ID多个以英文逗号(,)分割
* @return true|false
* @return
*/
@Override
public boolean deleteUsers(String idsStr) {
@@ -175,7 +175,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
*
* @param userId 用户ID
* @param password 用户密码
* @return true|false
* @return
*/
@Override
public boolean updatePassword(Long userId, String password) {

View File

@@ -3,7 +3,41 @@ generator:
## 模板配置
templateConfigs:
Controller:
## 模板路径
templatePath: templates/generator/controller.java.vm
## 包名
templatePath: generator/controller.java.vm
packageName: controller
Service:
templatePath: generator/service.java.vm
packageName: service
ServiceImpl:
templatePath: generator/serviceImpl.java.vm
packageName: service.impl
Mapper:
templatePath: generator/mapper.java.vm
packageName: mapper
MapperXml:
templatePath: generator/mapper.xml.vm
packageName: mapper
extension: .xml
Query:
templatePath: generator/query.java.vm
packageName: model.query
Form:
templatePath: generator/form.java.vm
packageName: model.form
VO:
templatePath: generator/vo.java.vm
packageName: model.vo
Entity:
templatePath: generator/entity.java.vm
packageName: model.entity
API:
templatePath: generator/api.ts.vm
packageName: api
extension: .ts
VIEW:
templatePath: generator/index.vue.vm
packageName: views
extension: .vue

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.youlai.system.mapper.GenConfigMapper">
</mapper>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.youlai.system.mapper.GenFieldConfigMapper">
<resultMap id="BaseResultMap" type="com.youlai.system.model.entity.GenFieldConfig">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="configId" column="config_id" jdbcType="BIGINT"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="type" column="type" jdbcType="VARCHAR"/>
<result property="description" column="description" jdbcType="VARCHAR"/>
<result property="showInList" column="show_in_list" jdbcType="TINYINT"/>
<result property="showInForm" column="show_in_form" jdbcType="TINYINT"/>
<result property="showInQuery" column="show_in_query" jdbcType="TINYINT"/>
<result property="formType" column="form_type" jdbcType="VARCHAR"/>
<result property="queryMethod" column="query_method" jdbcType="VARCHAR"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id,config_id,name,
type,description,show_in_list,
show_in_form,show_in_query,form_type,
query_method,create_time,update_time
</sql>
</mapper>

View File

@@ -0,0 +1,121 @@
import request from "@/utils/request";
const ${className.toUpperCase()}_BASE_URL = "/api/v1/${className.toLowerCase()}s";
class ${className}API {
/** 获取${className}分页数据 */
static getPage(queryParams?: ${className}PageQuery) {
return request<any, PageResult<${className}PageVO[]>>({
url: `${${className.toUpperCase()}_BASE_URL}/page`,
method: "get",
params: queryParams,
});
}
/** 获取${className}下拉数据源 */
static getOptions() {
return request<any, OptionType[]>({
url: `${${className.toUpperCase()}_BASE_URL}/options`,
method: "get",
});
}
/**
* 获取${className}的菜单ID集合
*
* @param ${className.toLowerCase()}Id ${className}ID
* @returns ${className}的菜单ID集合
*/
static get${className}MenuIds(${className.toLowerCase()}Id: number) {
return request<any, number[]>({
url: `${${className.toUpperCase()}_BASE_URL}/${${className.toLowerCase()}Id}/menuIds`,
method: "get",
});
}
/**
* 分配菜单权限
*
* @param ${className.toLowerCase()}Id ${className}ID
* @param data 菜单ID集合
*/
static update${className}Menus(${className.toLowerCase()}Id: number, data: number[]) {
return request({
url: `${${className.toUpperCase()}_BASE_URL}/${${className.toLowerCase()}Id}/menus`,
method: "put",
data: data,
});
}
/**
* 获取${className}表单数据
*
* @param id ${className}ID
* @returns ${className}表单数据
*/
static getFormData(id: number) {
return request<any, ${className}Form>({
url: `${${className.toUpperCase()}_BASE_URL}/${id}/form`,
method: "get",
});
}
/** 添加${className} */
static add(data: ${className}Form) {
return request({
url: `${${className.toUpperCase()}_BASE_URL}`,
method: "post",
data: data,
});
}
/**
* 更新${className}
*
* @param id ${className}ID
* @param data ${className}表单数据
*/
static update(id: number, data: ${className}Form) {
return request({
url: `${${className.toUpperCase()}_BASE_URL}/${id}`,
method: "put",
data: data,
});
}
/**
* 批量删除${className},多个以英文逗号(,)分割
*
* @param ids ${className}ID字符串多个以英文逗号(,)分割
*/
static deleteByIds(ids: string) {
return request({
url: `${${className.toUpperCase()}_BASE_URL}/${ids}`,
method: "delete",
});
}
}
export default ${className}API;
/** ${className}分页查询参数 */
export interface ${className}PageQuery extends PageQuery {
/** 搜索关键字 */
keywords?: string;
}
/** ${className}分页对象 */
export interface ${className}PageVO {
#foreach($field in $fields)
/** ${field.comment} */
${field.name}?: ${field.type};
#end
}
/** ${className}表单对象 */
export interface ${className}Form {
#foreach($field in $fields)
/** ${field.comment} */
${field.name}?: ${field.type};
#end
}

View File

@@ -7,8 +7,8 @@ import ${package}.model.form.${entityName}Form;
import ${package}.model.query.${entityName}PageQuery;
import ${package}.model.vo.${entityName}PageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
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;
@@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
/**
* $!{tableComment} 前端控制
* $!{tableComment}前端控制
*
* @author ${author}
* @since ${date}
@@ -44,7 +44,7 @@ public class ${entityName}Controller {
return Result.judge(result);
}
@Operation(summary = "$!{tableComment}表单数据")
@Operation(summary = "获取$!{tableComment}表单数据")
@GetMapping("/{id}/form")
public Result<${entityName}Form> get${entityName}Form(
@Parameter(description = "$!{tableComment}ID") @PathVariable Long id

View File

@@ -6,7 +6,7 @@ import ${package}.model.entity.${entityName};
import ${package}.model.form.${entityName}Form;
/**
* $!{tableComment}转换器
* $!{tableComment}对象转换器
*
* @author ${author}
* @since ${date}

View File

@@ -23,13 +23,14 @@ import com.youlai.system.common.base.BaseEntity;
public class ${entityName} extends BaseEntity {
private static final long serialVersionUID = 1L;
#foreach($field in ${table.fields})
#if("$!field.comment" != "")
#if($fieldConfigs)
#foreach($fieldConfig in ${fieldConfigs})
#if("$!fieldConfig.fieldComment" != "")
/**
* ${field.comment}
* ${fieldConfig.fieldComment}
*/
#end
private ${field.propertyType} ${field.propertyName};
#end
private ${fieldConfig.fieldType} ${fieldConfig.fieldName};
#end
#end
}

View File

@@ -1,4 +1,4 @@
package ${package}.model.form;
package ${package}.${subPackage};
import java.io.Serial;
import java.io.Serializable;
@@ -11,10 +11,12 @@ import java.time.LocalDateTime;
#if(${hasBigDecimal})
import java.math.BigDecimal;
#end
#if(${hasRequiredField})
import jakarta.validation.constraints.*;
#end
/**
* $!{tableComment} 表单对象
* $!{tableComment}表单对象
*
* @author ${author}
* @since ${date}
@@ -28,10 +30,22 @@ public class ${entityName}Form implements Serializable {
private static final long serialVersionUID = 1L;
## ---------- BEGIN 字段循环遍历 ----------
#foreach($field in ${fields})
#if("$!field.comment" != "")
@Schema(description = "${field.comment}")
#if($fieldConfigs)
#foreach($fieldConfig in ${fieldConfigs})
#if($fieldConfig.showInForm)
#if($fieldConfig.isRequired)
#if($fieldConfig.fieldType == 'String')
@NotBlank(message = "$fieldConfig.fieldComment不能为空")
#else
@NotNull(message = "$fieldConfig.fieldComment不能为空")
#end
#end
#if("$!fieldConfig.fieldComment" != "")
@Schema(description = "${fieldConfig.fieldComment}")
#end
private ${fieldConfig.fieldType} ${fieldConfig.fieldName};
#end
#end
private ${field.propertyType} ${field.propertyName};
#end
}

View File

@@ -0,0 +1,178 @@
<template>
<div class="app-container">
<div class="search-container">
<el-form ref="queryFormRef" :model="queryParams" :inline="true">
<el-form-item prop="keywords" label="关键字">
<el-input
v-model="queryParams.keywords"
placeholder="${className}名称"
clearable
@keyup.enter="handleQuery"
/>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="handleQuery">
<i-ep-search />
搜索
</el-button>
<el-button @click="handleResetQuery">
<i-ep-refresh />
重置
</el-button>
</el-form-item>
</el-form>
</div>
<el-card shadow="never" class="table-container">
<template #header>
<el-button type="success" @click="handleOpenDialog()">
<i-ep-plus />
新增
</el-button>
<el-button
type="danger"
:disabled="ids.length === 0"
@click="handleDelete()"
>
<i-ep-delete />
删除
</el-button>
</template>
<el-table
ref="dataTableRef"
v-loading="loading"
:data="${className.toLowerCase()}List"
highlight-current-row
border
@selection-change="handleSelectionChange"
>
<el-table-column type="selection" width="55" align="center" />
#foreach($field in $fields)
<el-table-column label="${field.comment}" prop="${field.name}" min-width="100" />
#end
<el-table-column fixed="right" label="操作" width="220">
<template #default="scope">
<el-button
type="primary"
size="small"
link
@click="handleOpenDialog(scope.row.id)"
>
<i-ep-edit />
编辑
</el-button>
<el-button
type="danger"
size="small"
link
@click="handleDelete(scope.row.id)"
>
<i-ep-delete />
删除
</el-button>
</template>
</el-table-column>
</el-table>
<pagination
v-if="total > 0"
v-model:total="total"
v-model:page="queryParams.pageNum"
v-model:limit="queryParams.pageSize"
@pagination="handleQuery"
/>
</el-card>
<!-- ${className}表单弹窗 -->
<el-dialog
v-model="dialog.visible"
:title="dialog.title"
width="500px"
@close="handleCloseDialog"
>
<el-form
ref="${className.toLowerCase()}FormRef"
:model="formData"
:rules="rules"
label-width="100px"
>
#foreach($field in $fields)
<el-form-item label="${field.comment}" prop="${field.name}">
<el-input v-model="formData.${field.name}" placeholder="请输入${field.comment}" />
</el-form-item>
#end
</el-form>
<template #footer>
<div class="dialog-footer">
<el-button type="primary" @click="handleSubmit">确 定</el-button>
<el-button @click="handleCloseDialog">取 消</el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script setup lang="ts">
defineOptions({
name: "${className}",
inheritAttrs: false,
});
import ${className}API, { ${className}PageVO, ${className}Form, ${className}PageQuery } from "@/api/${className.toLowerCase()}";
const queryFormRef = ref(ElForm);
const ${className.toLowerCase()}FormRef = ref(ElForm);
const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);
const queryParams = reactive<${className}PageQuery>({
pageNum: 1,
pageSize: 10,
});
// ${className}表格数据
const ${className.toLowerCase()}List = ref<${className}PageVO[]>();
// 弹窗
const dialog = reactive({
title: "",
visible: false,
});
// ${className}表单
const formData = reactive<${className}Form>({});
const rules = reactive({
#foreach($field in $fields)
${field.name}: [{ required: true, message: "请输入${field.comment}", trigger: "blur" }],
#end
});
/** 查询 */
function handleQuery() {
loading.value = true;
${className}API.getPage(queryParams)
.then((data) => {
${className.toLowerCase()}List.value = data.list;
total.value = data.total;
})
.finally(() => {
loading.value = false;
});
}
/** 重置查询 */
function handleResetQuery() {
queryFormRef.value.resetFields();
queryParams.pageNum = 1;
handleQuery();
}
/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
ids.value = selection.map((item: any

View File

@@ -7,7 +7,7 @@ import ${package}.model.query.${entityName}Query;
import org.apache.ibatis.annotations.Mapper;
/**
* $!{tableComment} 数据库访问层
* $!{tableComment}Mapper接口
*
* @author ${author}
* @since ${date}

View File

@@ -3,7 +3,7 @@
<mapper namespace="${package}.mapper.${entityName}Mapper">
<!-- 获取${tableComment}分页列表 -->
<select id="listPaged${entityName}s" resultType="${package}.model.entity.${entityName}">
<select id="get${entityName}Page" resultType="${package}.model.vo.${entityName}VO">
SELECT
*
FROM

View File

@@ -11,7 +11,6 @@ import java.time.LocalDateTime;
import java.math.BigDecimal;
#end
/**
* $!{tableComment}分页查询对象
*
@@ -24,11 +23,14 @@ import java.math.BigDecimal;
public class ${entityName}Query extends BasePageQuery {
private static final long serialVersionUID = 1L;
#foreach($field in ${fields})
#if("$!field.comment" != "")
@Schema(description = "${field.comment}")
#if($fieldConfigs)
#foreach($fieldConfig in ${fieldConfigs})
#if($fieldConfig.showInQuery)
#if("$!fieldConfig.fieldComment" != "")
@Schema(description = "${field.fieldComment}")
#end
private ${fieldConfig.fieldType} ${fieldConfig.fieldName};
#end
#end
private ${field.propertyType} ${field.propertyName};
#end
}

View File

@@ -6,15 +6,15 @@ import ${package}.model.query.${entityName}PageQuery;
import ${package}.model.vo.${entityName}PageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* $!{tableComment} 服务类
* $!{tableComment}服务类
*
* @author ${author}
* @since ${date}
*/
public interface ${entityName}Service extends IService<${entityName}> {
/**
*$!{tableComment}分页列表
*
@@ -22,7 +22,6 @@ public interface ${entityName}Service extends IService<${entityName}> {
*/
IPage<${entityName}VO> get${entityName}Page(${entityName}Query queryParams);
/**
* 获取$!{tableComment}表单数据
*
@@ -31,7 +30,6 @@ public interface ${entityName}Service extends IService<${entityName}> {
*/
${entityName}Form get${entityName}FormData(Long id);
/**
* 新增$!{tableComment}
*
@@ -49,7 +47,6 @@ public interface ${entityName}Service extends IService<${entityName}> {
*/
boolean update${entityName}(Long id, ${entityName}Form formData);
/**
* 删除$!{tableComment}
*

View File

@@ -28,7 +28,7 @@ import cn.hutool.core.util.StrUtil;
*/
@Service
@RequiredArgsConstructor
public class ${table.serviceImplName} extends ServiceImpl<${entityName}Mapper, ${entityName}> implements ${entityName}Service {
public class ${entityName}ServiceImpl extends ServiceImpl<${entityName}Mapper, ${entityName}> implements ${entityName}Service {
private final ${entityName}Converter ${lowerFirstEntityName}Converter;
@@ -88,7 +88,7 @@ public class ${table.serviceImplName} extends ServiceImpl<${entityName}Mapper, $
* 删除$!{tableComment}
*
* @param ids $!{tableComment}ID多个以英文逗号(,)分割
* @return true|false
* @return
*/
@Override
public boolean delete${entityName}s(String ids) {

View File

@@ -14,7 +14,7 @@ import java.math.BigDecimal;
#end
/**
* $!{tableComment} 图对象
* $!{tableComment}图对象
*
* @author ${author}
* @since ${date}
@@ -27,11 +27,12 @@ public class ${entityName}VO implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
#foreach($field in ${fields})
#if("$!field.comment" != "")
@Schema(description = "${field.comment}")
#if($fieldConfigs)
#foreach($fieldConfig in ${fieldConfigs})
#if("$!fieldConfig.fieldComment" != "")
@Schema(description = "${fieldConfig.fieldComment}")
#end
private ${fieldConfig.fieldType} ${fieldConfig.fieldName};
#end
private ${field.propertyType} ${field.propertyName};
#end
}