chore: 合并 develop 代码生成分支
This commit is contained in:
17
src/main/java/com/youlai/system/config/JacksonConfig.java
Normal file
17
src/main/java/com/youlai/system/config/JacksonConfig.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.youlai.system.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class JacksonConfig {
|
||||
|
||||
@Bean
|
||||
public ObjectMapper objectMapper() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
|
||||
return objectMapper;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -18,18 +19,55 @@ import java.util.Map;
|
||||
@Data
|
||||
public class GeneratorProperties {
|
||||
|
||||
|
||||
/**
|
||||
* 默认配置
|
||||
*/
|
||||
private DefaultConfig defaultConfig ;
|
||||
|
||||
/**
|
||||
* 模板配置
|
||||
*/
|
||||
private Map<String, TemplateConfig> templateConfigs = MapUtil.newHashMap(true);
|
||||
|
||||
|
||||
/**
|
||||
* 后端应用名
|
||||
*/
|
||||
|
||||
private String backendAppName;
|
||||
|
||||
/**
|
||||
* 前端应用名
|
||||
*/
|
||||
private String frontendAppName;
|
||||
|
||||
/**
|
||||
* 模板配置
|
||||
*/
|
||||
@Data
|
||||
public static class TemplateConfig{
|
||||
public static class TemplateConfig {
|
||||
|
||||
private String templatePath;
|
||||
|
||||
private String packageName;
|
||||
|
||||
/**
|
||||
* 文件扩展名,如 .java
|
||||
*/
|
||||
private String extension = FileNameUtil.EXT_JAVA;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认配置
|
||||
*/
|
||||
@Data
|
||||
public static class DefaultConfig {
|
||||
|
||||
private String author;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,22 +3,25 @@ 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;
|
||||
|
||||
/**
|
||||
* 代码生成器控制层
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@Tag(name = "09.代码生成")
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/generator")
|
||||
@@ -36,20 +39,25 @@ 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);
|
||||
return Result.success(list);
|
||||
@Operation(summary = "获取代码生成配置")
|
||||
@GetMapping("/{tableName}/config")
|
||||
public Result<GenConfigForm> getGenConfigFormData(
|
||||
@Parameter(description = "表名", example = "sys_user") @PathVariable String tableName) {
|
||||
GenConfigForm formData = generatorService.getGenConfigFormData(tableName);
|
||||
return Result.success(formData);
|
||||
}
|
||||
|
||||
@Operation(summary = "保存代码生成配置")
|
||||
@PostMapping("/{tableName}/config")
|
||||
public Result saveGenConfig(@RequestBody GenConfigForm formData) {
|
||||
generatorService.saveGenConfig(formData);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "获取预览生成代码")
|
||||
@GetMapping("/table/{tableName}/preview")
|
||||
public Result<List<TableGeneratePreviewVO>> getTablePreviewData(@PathVariable String tableName) {
|
||||
List<TableGeneratePreviewVO> list = generatorService.getTablePreviewData(tableName);
|
||||
@GetMapping("/{tableName}/preview")
|
||||
public Result<List<GeneratorPreviewVO>> getTablePreviewData(@PathVariable String tableName) {
|
||||
List<GeneratorPreviewVO> list = generatorService.getTablePreviewData(tableName);
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.youlai.system.common.result.PageResult;
|
||||
import com.youlai.system.common.result.Result;
|
||||
import com.youlai.system.common.util.ExcelUtils;
|
||||
import com.youlai.system.util.ExcelUtils;
|
||||
import com.youlai.system.enums.LogModuleEnum;
|
||||
import com.youlai.system.model.dto.UserImportDTO;
|
||||
import com.youlai.system.plugin.norepeat.annotation.PreventRepeatSubmit;
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
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.businessName", target = "businessName")
|
||||
@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> toGenFieldConfigForm(List<GenFieldConfig> fieldConfigs);
|
||||
|
||||
GenConfigForm.FieldConfig toGenFieldConfigForm(GenFieldConfig genFieldConfig);
|
||||
|
||||
|
||||
GenConfig toGenConfig(GenConfigForm formData);
|
||||
|
||||
List<GenFieldConfig> toGenFieldConfig(List<GenConfigForm.FieldConfig> fieldConfigs);
|
||||
|
||||
GenFieldConfig toGenFieldConfig(GenConfigForm.FieldConfig fieldConfig);
|
||||
|
||||
}
|
||||
84
src/main/java/com/youlai/system/enums/FormTypeEnum.java
Normal file
84
src/main/java/com/youlai/system/enums/FormTypeEnum.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package com.youlai.system.enums;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.EnumValue;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
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, "单选框"),
|
||||
|
||||
/**
|
||||
* 复选框
|
||||
*/
|
||||
CHECK_BOX(4, "复选框"),
|
||||
|
||||
/**
|
||||
* 数字输入框
|
||||
*/
|
||||
INPUT_NUMBER(5, "数字输入框"),
|
||||
|
||||
/**
|
||||
* 开关
|
||||
*/
|
||||
SWITCH(6, "开关"),
|
||||
|
||||
/**
|
||||
* 文本域
|
||||
*/
|
||||
TEXT_AREA(7, "文本域"),
|
||||
|
||||
/**
|
||||
* 日期时间框
|
||||
*/
|
||||
DATE_TIME(8, "日期时间框"),
|
||||
|
||||
/**
|
||||
* 日期框
|
||||
*/
|
||||
DATE(9, "日期框");
|
||||
|
||||
|
||||
// Mybatis-Plus 提供注解表示插入数据库时插入该值
|
||||
@EnumValue
|
||||
@JsonValue
|
||||
private final Integer value;
|
||||
|
||||
// @JsonValue // 表示对枚举序列化时返回此字段
|
||||
private final String label;
|
||||
|
||||
|
||||
@JsonCreator
|
||||
public static QueryTypeEnum fromValue(Integer value) {
|
||||
for (QueryTypeEnum type : QueryTypeEnum.values()) {
|
||||
if (type.getValue().equals(value)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("No enum constant with value " + value);
|
||||
}
|
||||
}
|
||||
84
src/main/java/com/youlai/system/enums/JavaTypeEnum.java
Normal file
84
src/main/java/com/youlai/system/enums/JavaTypeEnum.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package com.youlai.system.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 表单类型枚举
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@Getter
|
||||
public enum JavaTypeEnum {
|
||||
|
||||
VARCHAR("varchar", "String", "string"),
|
||||
CHAR("char", "String", "string"),
|
||||
BLOB("blob", "byte[]", "Uint8Array"),
|
||||
TEXT("text", "String", "string"),
|
||||
JSON("json", "String", "any"),
|
||||
INTEGER("int", "Integer", "number"),
|
||||
TINYINT("tinyint", "Integer", "number"),
|
||||
SMALLINT("smallint", "Integer", "number"),
|
||||
MEDIUMINT("mediumint", "Integer", "number"),
|
||||
BIGINT("bigint", "Long", "bigint"),
|
||||
FLOAT("float", "Float", "number"),
|
||||
DOUBLE("double", "Double", "number"),
|
||||
DECIMAL("decimal", "BigDecimal", "number"),
|
||||
DATE("date", "LocalDate", "Date"),
|
||||
DATETIME("datetime", "LocalDateTime", "Date");
|
||||
|
||||
// 数据库类型
|
||||
private final String dbType;
|
||||
// Java类型
|
||||
private final String javaType;
|
||||
// TypeScript类型
|
||||
private final String tsType;
|
||||
|
||||
// 数据库类型和Java类型的映射
|
||||
private static final Map<String, JavaTypeEnum> typeMap = new HashMap<>();
|
||||
|
||||
// 初始化映射关系
|
||||
static {
|
||||
for (JavaTypeEnum javaTypeEnum : JavaTypeEnum.values()) {
|
||||
typeMap.put(javaTypeEnum.getDbType(), javaTypeEnum);
|
||||
}
|
||||
}
|
||||
|
||||
JavaTypeEnum(String dbType, String javaType, String tsType) {
|
||||
this.dbType = dbType;
|
||||
this.javaType = javaType;
|
||||
this.tsType = tsType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数据库类型获取对应的Java类型
|
||||
*
|
||||
* @param dbType 数据库类型
|
||||
* @return 对应的Java类型
|
||||
*/
|
||||
public static String getJavaTypeByDbType(String dbType) {
|
||||
JavaTypeEnum javaTypeEnum = typeMap.get(dbType);
|
||||
if (javaTypeEnum != null) {
|
||||
return javaTypeEnum.getJavaType();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据Java类型获取对应的TypeScript类型
|
||||
*
|
||||
* @param javaType Java类型
|
||||
* @return 对应的TypeScript类型
|
||||
*/
|
||||
public static String getTsTypeByJavaType(String javaType) {
|
||||
for (JavaTypeEnum javaTypeEnum : JavaTypeEnum.values()) {
|
||||
if (javaTypeEnum.getJavaType().equals(javaType)) {
|
||||
return javaTypeEnum.getTsType();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
73
src/main/java/com/youlai/system/enums/QueryTypeEnum.java
Normal file
73
src/main/java/com/youlai/system/enums/QueryTypeEnum.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package com.youlai.system.enums;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.EnumValue;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
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, "="),
|
||||
|
||||
/** 模糊匹配 */
|
||||
LIKE(2, "LIKE '%s%'"),
|
||||
|
||||
/** 包含 */
|
||||
IN(3, "IN"),
|
||||
|
||||
/** 范围 */
|
||||
BETWEEN(4, "BETWEEN"),
|
||||
|
||||
/** 大于 */
|
||||
GT(5, ">"),
|
||||
|
||||
/** 大于等于 */
|
||||
GE(6, ">="),
|
||||
|
||||
/** 小于 */
|
||||
LT(7, "<"),
|
||||
|
||||
/** 小于等于 */
|
||||
LE(8, "<="),
|
||||
|
||||
/** 不等于 */
|
||||
NE(9, "!="),
|
||||
|
||||
/** 左模糊匹配 */
|
||||
LIKE_LEFT(10, "LIKE '%s'"),
|
||||
|
||||
/** 右模糊匹配 */
|
||||
LIKE_RIGHT(11, "LIKE 's%'");
|
||||
|
||||
|
||||
// 存储在数据库中的枚举属性值
|
||||
@EnumValue
|
||||
@JsonValue
|
||||
private final Integer value;
|
||||
|
||||
// 序列化成 JSON 时的属性值
|
||||
private final String label;
|
||||
|
||||
|
||||
@JsonCreator
|
||||
public static QueryTypeEnum fromValue(Integer value) {
|
||||
for (QueryTypeEnum type : QueryTypeEnum.values()) {
|
||||
if (type.getValue().equals(value)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("No enum constant with value " + value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import cn.hutool.captcha.generator.CodeGenerator;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.youlai.system.common.constant.SecurityConstants;
|
||||
import com.youlai.system.common.result.ResultCode;
|
||||
import com.youlai.system.common.util.ResponseUtils;
|
||||
import com.youlai.system.util.ResponseUtils;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
@@ -8,7 +8,7 @@ import cn.hutool.jwt.JWTUtil;
|
||||
import com.youlai.system.common.constant.SecurityConstants;
|
||||
import com.youlai.system.common.result.ResultCode;
|
||||
import com.youlai.system.security.util.JwtUtils;
|
||||
import com.youlai.system.common.util.ResponseUtils;
|
||||
import com.youlai.system.util.ResponseUtils;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
@@ -2,9 +2,10 @@ package com.youlai.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.youlai.system.model.bo.ColumnMetaData;
|
||||
import com.youlai.system.model.bo.TableMetaData;
|
||||
import com.youlai.system.model.entity.SysDept;
|
||||
import com.youlai.system.model.query.TablePageQuery;
|
||||
import com.youlai.system.model.vo.TableColumnVO;
|
||||
import com.youlai.system.model.vo.TablePageVO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@@ -17,7 +18,8 @@ public interface DatabaseMapper extends BaseMapper<SysDept> {
|
||||
|
||||
Page<TablePageVO> getTablePage(Page<TablePageVO> page, TablePageQuery queryParams);
|
||||
|
||||
List<TableColumnVO> getTableColumns(String tableName);
|
||||
List<ColumnMetaData> getTableColumns(String tableName);
|
||||
|
||||
|
||||
TableMetaData getTableMetadata(String tableName);
|
||||
}
|
||||
|
||||
50
src/main/java/com/youlai/system/model/bo/ColumnMetaData.java
Normal file
50
src/main/java/com/youlai/system/model/bo/ColumnMetaData.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.youlai.system.model.bo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Schema(description = "数据表字段VO")
|
||||
@Data
|
||||
public class ColumnMetaData {
|
||||
|
||||
/**
|
||||
* 字段名称
|
||||
*/
|
||||
private String columnName;
|
||||
|
||||
/**
|
||||
* 字段类型
|
||||
*/
|
||||
private String dataType;
|
||||
|
||||
/**
|
||||
* 字段描述
|
||||
*/
|
||||
private String columnComment;
|
||||
|
||||
/**
|
||||
* 字段长度
|
||||
*/
|
||||
private Integer characterMaximumLength;
|
||||
|
||||
/**
|
||||
* 是否主键(1-是 0-否)
|
||||
*/
|
||||
private Integer isPrimaryKey;
|
||||
|
||||
/**
|
||||
* 是否可为空(1-是 0-否)
|
||||
*/
|
||||
private String isNullable;
|
||||
|
||||
/**
|
||||
* 字符集
|
||||
*/
|
||||
private String characterSetName;
|
||||
|
||||
/**
|
||||
* 排序规则
|
||||
*/
|
||||
private String collationName;
|
||||
|
||||
}
|
||||
45
src/main/java/com/youlai/system/model/bo/TableMetaData.java
Normal file
45
src/main/java/com/youlai/system/model/bo/TableMetaData.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package com.youlai.system.model.bo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
/**
|
||||
* 数据表元数据
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@Data
|
||||
public class TableMetaData {
|
||||
|
||||
/**
|
||||
* 表名称
|
||||
*/
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* 表描述
|
||||
*/
|
||||
private String tableComment;
|
||||
|
||||
/**
|
||||
* 排序规则
|
||||
*/
|
||||
private String tableCollation;
|
||||
|
||||
/**
|
||||
* 存储引擎
|
||||
*/
|
||||
private String engine;
|
||||
|
||||
/**
|
||||
* 字符集
|
||||
*/
|
||||
private String charset;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private String createTime;
|
||||
|
||||
}
|
||||
59
src/main/java/com/youlai/system/model/entity/GenConfig.java
Normal file
59
src/main/java/com/youlai/system/model/entity/GenConfig.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package com.youlai.system.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
|
||||
import com.youlai.system.common.base.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 代码生成基础配置
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@TableName(value = "gen_config")
|
||||
@Getter
|
||||
@Setter
|
||||
public class GenConfig extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 表名
|
||||
*/
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* 包名
|
||||
*/
|
||||
private String packageName;
|
||||
|
||||
/**
|
||||
* 模块名
|
||||
*/
|
||||
private String moduleName;
|
||||
|
||||
/**
|
||||
* 实体类名
|
||||
*/
|
||||
private String entityName;
|
||||
|
||||
/**
|
||||
* 业务名
|
||||
*/
|
||||
private String businessName;
|
||||
|
||||
/**
|
||||
* 上级菜单ID
|
||||
*/
|
||||
private Long parentMenuId;
|
||||
|
||||
/**
|
||||
* 作者
|
||||
*/
|
||||
private String author;
|
||||
|
||||
|
||||
@TableLogic
|
||||
private Integer isDeleted;
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.youlai.system.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
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 fieldComment;
|
||||
|
||||
/**
|
||||
* 表单类型
|
||||
*/
|
||||
private FormTypeEnum formType;
|
||||
|
||||
/**
|
||||
* 查询方式
|
||||
*/
|
||||
private QueryTypeEnum queryType;
|
||||
|
||||
/**
|
||||
* 是否在列表显示
|
||||
*/
|
||||
private Integer isShowInList;
|
||||
|
||||
/**
|
||||
* 是否在表单显示
|
||||
*/
|
||||
private Integer isShowInForm;
|
||||
|
||||
/**
|
||||
* 是否在查询条件显示
|
||||
*/
|
||||
private Integer isShowInQuery;
|
||||
|
||||
/**
|
||||
* 是否必填
|
||||
*/
|
||||
private Integer isRequired;
|
||||
|
||||
|
||||
/**
|
||||
* TypeScript类型
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
@JsonIgnore
|
||||
private String tsType;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,59 +1,85 @@
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 代码生成配置表单
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@Schema(description = "代码生成配置表单")
|
||||
@Data
|
||||
public class GeneratorConfigForm {
|
||||
public class GenConfigForm {
|
||||
|
||||
@Schema(description = "表名")
|
||||
@Schema(description = "主键",example = "1")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "表名",example = "sys_user")
|
||||
private String tableName;
|
||||
|
||||
@Schema(description = "实体名")
|
||||
private String entityName;
|
||||
@Schema(description = "业务名",example = "用户")
|
||||
private String businessName;
|
||||
|
||||
@Schema(description = "包名")
|
||||
private String packageName;
|
||||
|
||||
@Schema(description = "模块名")
|
||||
@Schema(description = "模块名",example = "system")
|
||||
private String moduleName;
|
||||
|
||||
@Schema(description = "作者")
|
||||
@Schema(description = "包名",example = "com.youlai.system")
|
||||
private String packageName;
|
||||
|
||||
@Schema(description = "实体名",example = "User")
|
||||
private String entityName;
|
||||
|
||||
@Schema(description = "作者",example = "youlaitech")
|
||||
private String author;
|
||||
|
||||
@Schema(description = "字段配置")
|
||||
@Schema(description = "字段配置列表")
|
||||
private List<FieldConfig> fieldConfigs;
|
||||
|
||||
@Schema(description = "字段配置")
|
||||
@Data
|
||||
public static class FieldConfig {
|
||||
|
||||
@Schema(description = "字段名称")
|
||||
private String name;
|
||||
@Schema(description = "主键")
|
||||
private Long id;
|
||||
|
||||
@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 fieldComment;
|
||||
|
||||
@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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.youlai.system.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Schema(description = "数据表字段VO")
|
||||
@Data
|
||||
public class TableColumnVO {
|
||||
|
||||
@Schema(description = "字段名称", example = "id")
|
||||
private String columnName;
|
||||
|
||||
@Schema(description = "字段类型", example = "bigint")
|
||||
private String dataType;
|
||||
|
||||
@Schema(description = "字段描述", example = "主键")
|
||||
private String columnComment;
|
||||
|
||||
@Schema(description = "字段长度", example = "20")
|
||||
private Integer characterMaximumLength;
|
||||
|
||||
@Schema(description = "是否主键(1-是 0-否)", example = "1")
|
||||
private Integer isPrimaryKey;
|
||||
|
||||
@Schema(description = "是否可为空(1-是 0-否)", example = "1")
|
||||
private String isNullable;
|
||||
|
||||
@Schema(description = "字符集", example = "utf8mb4")
|
||||
private String characterSetName;
|
||||
|
||||
@Schema(description = "字符集排序规则", example = "utf8mb4_general_ci")
|
||||
private String collationName;
|
||||
|
||||
}
|
||||
@@ -4,23 +4,23 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
@Schema(description = "数据表分页VO")
|
||||
@Schema(description = "表视图对象")
|
||||
@Data
|
||||
public class TablePageVO {
|
||||
|
||||
@Schema(description = "数据表名称", example = "sys_user")
|
||||
@Schema(description = "表名称", example = "sys_user")
|
||||
private String tableName;
|
||||
|
||||
@Schema(description = "数据表注释",example = "用户表")
|
||||
@Schema(description = "表描述",example = "用户表")
|
||||
private String tableComment;
|
||||
|
||||
@Schema(description = "数据表排序规则",example = "用户表")
|
||||
@Schema(description = "表排序规则",example = "utf8mb4_general_ci")
|
||||
private String tableCollation;
|
||||
|
||||
@Schema(description = "存储引擎",example = "InnoDB")
|
||||
private String engine;
|
||||
|
||||
@Schema(description = "字符集",example = "utf8mb4_general_ci")
|
||||
@Schema(description = "字符集",example = "utf8mb4")
|
||||
private String charset;
|
||||
|
||||
@Schema(description = "创建时间",example = "2023-08-08 08:08:08")
|
||||
|
||||
@@ -3,12 +3,10 @@ package com.youlai.system.plugin.syslog.aspect;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.TimeInterval;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.useragent.Browser;
|
||||
import cn.hutool.http.useragent.OS;
|
||||
import cn.hutool.http.useragent.UserAgent;
|
||||
import cn.hutool.http.useragent.UserAgentUtil;
|
||||
import com.youlai.system.common.constant.SecurityConstants;
|
||||
import com.youlai.system.common.util.IPUtils;
|
||||
import com.youlai.system.util.IPUtils;
|
||||
import com.youlai.system.model.entity.SysLog;
|
||||
import com.youlai.system.plugin.syslog.annotation.LogAnnotation;
|
||||
import com.youlai.system.security.util.SecurityUtils;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.youlai.system.security.exception;
|
||||
|
||||
import com.youlai.system.common.result.ResultCode;
|
||||
import com.youlai.system.common.util.ResponseUtils;
|
||||
import com.youlai.system.util.ResponseUtils;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.web.access.AccessDeniedHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.youlai.system.security.exception;
|
||||
|
||||
import com.youlai.system.common.result.ResultCode;
|
||||
import com.youlai.system.common.util.ResponseUtils;
|
||||
import com.youlai.system.util.ResponseUtils;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.youlai.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.youlai.system.model.entity.GenConfig;
|
||||
|
||||
/**
|
||||
* 代码生成配置接口
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
public interface GenConfigService extends IService<GenConfig> {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.youlai.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.youlai.system.model.entity.GenConfig;
|
||||
import com.youlai.system.model.entity.GenFieldConfig;
|
||||
|
||||
/**
|
||||
* 代码生成配置接口
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
public interface GenFieldConfigService extends IService<GenFieldConfig> {
|
||||
|
||||
}
|
||||
@@ -1,22 +1,21 @@
|
||||
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
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
public interface GeneratorService {
|
||||
|
||||
|
||||
/**
|
||||
* 获取数据表分页列表
|
||||
*
|
||||
@@ -25,19 +24,29 @@ public interface GeneratorService {
|
||||
*/
|
||||
Page<TablePageVO> getTablePage(TablePageQuery queryParams);
|
||||
|
||||
/**
|
||||
* 获取数据表字段列表
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return
|
||||
*/
|
||||
List<TableColumnVO> getTableColumns(String tableName);
|
||||
|
||||
/**
|
||||
* 获取预览生成代码
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return
|
||||
*/
|
||||
List<TableGeneratePreviewVO> getTablePreviewData(String tableName);
|
||||
List<GeneratorPreviewVO> getTablePreviewData(String tableName);
|
||||
|
||||
/**
|
||||
* 获取代码生成配置
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return
|
||||
*/
|
||||
GenConfigForm getGenConfigFormData(String tableName);
|
||||
|
||||
/**
|
||||
* 保存代码生成配置
|
||||
*
|
||||
* @param formData 表单数据
|
||||
* @return
|
||||
*/
|
||||
void saveGenConfig(GenConfigForm formData);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.mapper.GenConfigMapper;
|
||||
import com.youlai.system.model.entity.GenConfig;
|
||||
import com.youlai.system.service.GenConfigService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 数据库服务实现类
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig> implements GenConfigService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.mapper.GenFieldConfigMapper;
|
||||
import com.youlai.system.model.entity.GenFieldConfig;
|
||||
import com.youlai.system.service.GenFieldConfigService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 代码生成字段配置服务实现类
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GenFieldConfigServiceImpl extends ServiceImpl<GenFieldConfigMapper, GenFieldConfig> implements GenFieldConfigService {
|
||||
|
||||
|
||||
}
|
||||
@@ -1,20 +1,37 @@
|
||||
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.ObjectUtil;
|
||||
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.SystemApplication;
|
||||
import com.youlai.system.config.property.GeneratorProperties;
|
||||
import com.youlai.system.converter.GenConfigConverter;
|
||||
import com.youlai.system.enums.FormTypeEnum;
|
||||
import com.youlai.system.enums.JavaTypeEnum;
|
||||
import com.youlai.system.enums.QueryTypeEnum;
|
||||
import com.youlai.system.exception.BusinessException;
|
||||
import com.youlai.system.mapper.DatabaseMapper;
|
||||
import com.youlai.system.model.bo.ColumnMetaData;
|
||||
import com.youlai.system.model.bo.TableMetaData;
|
||||
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 com.youlai.system.service.GenConfigService;
|
||||
import com.youlai.system.service.GenFieldConfigService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import cn.hutool.extra.template.TemplateConfig.ResourceMode;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
@@ -23,13 +40,17 @@ import java.util.*;
|
||||
* 数据库服务实现类
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.11.0
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GeneratorServiceImpl implements GeneratorService {
|
||||
|
||||
private final DatabaseMapper databaseMapper;
|
||||
private final GeneratorProperties generatorProperties;
|
||||
private final GenConfigService genConfigService;
|
||||
private final GenFieldConfigService genFieldConfigService;
|
||||
private final GenConfigConverter genConfigConverter;
|
||||
|
||||
/**
|
||||
* 数据表分页列表
|
||||
@@ -43,16 +64,115 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据表字段列表
|
||||
* 获取代码生成配置
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return 字段列表
|
||||
* @param tableName 表名 eg: sys_user
|
||||
* @return 代码生成配置
|
||||
*/
|
||||
@Override
|
||||
public List<TableColumnVO> getTableColumns(String tableName) {
|
||||
return databaseMapper.getTableColumns(tableName);
|
||||
public GenConfigForm getGenConfigFormData(String tableName) {
|
||||
// 查询表生成配置
|
||||
GenConfig genConfig = genConfigService.getOne(
|
||||
new LambdaQueryWrapper<>(GenConfig.class)
|
||||
.eq(GenConfig::getTableName, tableName)
|
||||
.last("LIMIT 1")
|
||||
);
|
||||
// 如果没有代码生成配置,则根据表的元数据生成默认配置
|
||||
if (genConfig == null) {
|
||||
TableMetaData tableMetadata = databaseMapper.getTableMetadata(tableName);
|
||||
Assert.isTrue(tableMetadata != null, "未找到表元数据");
|
||||
|
||||
genConfig = new GenConfig();
|
||||
genConfig.setTableName(tableName);
|
||||
|
||||
String tableComment = tableMetadata.getTableComment();
|
||||
if (StrUtil.isNotBlank(tableComment)) {
|
||||
genConfig.setBusinessName(tableComment.replace("表", ""));
|
||||
}
|
||||
// 实体类名 = 表名去掉前缀后转驼峰,前缀默认为下划线分割的第一个元素
|
||||
String entityName = StrUtil.toCamelCase(StrUtil.removePrefix(tableName, tableName.split("_")[0]));
|
||||
genConfig.setEntityName(entityName);
|
||||
|
||||
String packageName = SystemApplication.class.getPackageName();
|
||||
genConfig.setPackageName(packageName);
|
||||
|
||||
genConfig.setAuthor(generatorProperties.getDefaultConfig().getAuthor());
|
||||
|
||||
}
|
||||
|
||||
// 根据表的列 + 已经存在的字段生成配置 得到 组合后的字段生成配置
|
||||
List<GenFieldConfig> genFieldConfigs = new ArrayList<>();
|
||||
|
||||
// 获取表的列
|
||||
List<ColumnMetaData> tableColumns = databaseMapper.getTableColumns(tableName);
|
||||
if (CollectionUtil.isNotEmpty(tableColumns)) {
|
||||
// 查询字段生成配置
|
||||
List<GenFieldConfig> fieldConfigList = genFieldConfigService.list(
|
||||
new LambdaQueryWrapper<>(GenFieldConfig.class)
|
||||
.eq(GenFieldConfig::getConfigId, genConfig.getId())
|
||||
);
|
||||
for (ColumnMetaData tableColumn : tableColumns) {
|
||||
// 根据列名获取字段生成配置
|
||||
String columnName = tableColumn.getColumnName();
|
||||
GenFieldConfig genFieldConfig = fieldConfigList.stream()
|
||||
.filter(item -> StrUtil.equals(item.getColumnName(), columnName))
|
||||
.findFirst()
|
||||
.orElseGet(() -> createDefaultFieldConfig(tableColumn));
|
||||
|
||||
// 根据列类型设置字段类型
|
||||
String fieldType = genFieldConfig.getFieldType();
|
||||
if (StrUtil.isBlank(fieldType)) {
|
||||
String javaType = JavaTypeEnum.getJavaTypeByDbType(genFieldConfig.getColumnType());
|
||||
genFieldConfig.setFieldType(javaType);
|
||||
}
|
||||
genFieldConfigs.add(genFieldConfig);
|
||||
}
|
||||
}
|
||||
GenConfigForm configFormData = genConfigConverter.toGenConfigForm(genConfig, genFieldConfigs);
|
||||
return configFormData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建默认字段配置
|
||||
*
|
||||
* @param tableColumn 表字段元数据
|
||||
* @return
|
||||
*/
|
||||
private GenFieldConfig createDefaultFieldConfig(ColumnMetaData tableColumn) {
|
||||
GenFieldConfig fieldConfig = new GenFieldConfig();
|
||||
fieldConfig.setColumnName(tableColumn.getColumnName());
|
||||
fieldConfig.setColumnType(tableColumn.getDataType());
|
||||
fieldConfig.setFieldComment(tableColumn.getColumnComment());
|
||||
fieldConfig.setFieldName(StrUtil.toCamelCase(tableColumn.getColumnName()));
|
||||
fieldConfig.setIsRequired("YES".equals(tableColumn.getIsNullable()) ? 1 : 0);
|
||||
fieldConfig.setFormType(FormTypeEnum.INPUT);
|
||||
fieldConfig.setQueryType(QueryTypeEnum.EQ);
|
||||
return fieldConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存代码生成配置
|
||||
*
|
||||
* @param formData 代码生成配置表单
|
||||
*/
|
||||
@Override
|
||||
public void saveGenConfig(GenConfigForm formData) {
|
||||
GenConfig genConfig = genConfigConverter.toGenConfig(formData);
|
||||
genConfigService.saveOrUpdate(genConfig);
|
||||
|
||||
List<GenFieldConfig> genFieldConfigs = genConfigConverter.toGenFieldConfig(formData.getFieldConfigs());
|
||||
|
||||
if (CollectionUtil.isEmpty(genFieldConfigs)) {
|
||||
throw new BusinessException("字段配置不能为空");
|
||||
}
|
||||
genFieldConfigs.forEach(genFieldConfig -> {
|
||||
genFieldConfig.setConfigId(genConfig.getId());
|
||||
});
|
||||
genFieldConfigService.saveOrUpdateBatch(genFieldConfigs);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取预览生成代码
|
||||
*
|
||||
@@ -60,49 +180,151 @@ 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 = genConfigService.getOne(new LambdaQueryWrapper<GenConfig>()
|
||||
.eq(GenConfig::getTableName, tableName)
|
||||
);
|
||||
Assert.isTrue(genConfig != null, "未找到表生成配置");
|
||||
|
||||
List<GenFieldConfig> fieldConfigs = genFieldConfigService.list(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,entityName);
|
||||
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 ("Entity".equals(templateName)) {
|
||||
return entityName + extension;
|
||||
}
|
||||
if ("MapperXml".equals(templateName)) {
|
||||
return entityName + "Mapper" + extension;
|
||||
}
|
||||
if ("API".equals(templateName)) {
|
||||
return StrUtil.toSymbolCase(entityName, '-') + extension;
|
||||
}
|
||||
|
||||
if ("VIEW".equals(templateName)) {
|
||||
return "index.vue";
|
||||
}
|
||||
|
||||
return entityName + templateName + extension;
|
||||
}
|
||||
|
||||
private String getFilePath(String templateName, String packageName, String subPackageName,String entityName) {
|
||||
String path;
|
||||
if ("MapperXml".equals(templateName)) {
|
||||
path = (generatorProperties.getBackendAppName()
|
||||
+ File.separator
|
||||
+ "src" + File.separator + "main" + File.separator + "resources"
|
||||
+ File.separator + subPackageName
|
||||
);
|
||||
} else if ("API".equals(templateName) ) {
|
||||
path = (generatorProperties.getFrontendAppName()
|
||||
+ File.separator
|
||||
+ "src" + File.separator + subPackageName
|
||||
);
|
||||
} else if("VIEW".equals(templateName)){
|
||||
path = (generatorProperties.getFrontendAppName()
|
||||
+ File.separator
|
||||
+ "src" + File.separator + subPackageName
|
||||
+ File.separator
|
||||
+ StrUtil.toSymbolCase(entityName, '-')
|
||||
);
|
||||
}else {
|
||||
path = (generatorProperties.getBackendAppName()
|
||||
+ File.separator
|
||||
+ "src" + File.separator + "main" + File.separator + "java"
|
||||
+ File.separator + packageName + File.separator + subPackageName
|
||||
);
|
||||
}
|
||||
|
||||
// subPackageName = model.entity => model/entity
|
||||
path = path.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("businessName", genConfig.getBusinessName());
|
||||
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", ObjectUtil.equals(fieldConfig.getIsRequired(), 1));
|
||||
fieldConfig.setTsType(JavaTypeEnum.getTsTypeByJavaType(fieldConfig.getFieldType()));
|
||||
}
|
||||
|
||||
TemplateEngine templateEngine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
|
||||
Template template = templateEngine.getTemplate(templateConfig.getTemplatePath());
|
||||
String content = template.render(bindMap);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.youlai.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.common.util.DateUtils;
|
||||
import com.youlai.system.util.DateUtils;
|
||||
import com.youlai.system.model.bo.VisitCount;
|
||||
import com.youlai.system.model.entity.SysLog;
|
||||
import com.youlai.system.model.query.LogPageQuery;
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.common.constant.SystemConstants;
|
||||
import com.youlai.system.common.util.DateUtils;
|
||||
import com.youlai.system.util.DateUtils;
|
||||
import com.youlai.system.converter.UserConverter;
|
||||
import com.youlai.system.security.util.SecurityUtils;
|
||||
import com.youlai.system.mapper.SysUserMapper;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
package com.youlai.system.common.util;
|
||||
package com.youlai.system.util;
|
||||
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.youlai.system.common.util;
|
||||
package com.youlai.system.util;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.youlai.system.plugin.easyexcel.MyAnalysisEventListener;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.youlai.system.common.util;
|
||||
package com.youlai.system.util;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.youlai.system.common.util;
|
||||
package com.youlai.system.util;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.youlai.system.common.result.Result;
|
||||
Reference in New Issue
Block a user