wip: 代码生成临时提交
This commit is contained in:
@@ -3,6 +3,7 @@ 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.bo.TableMetaData;
|
||||
import com.youlai.system.model.form.GenConfigForm;
|
||||
import com.youlai.system.model.query.TablePageQuery;
|
||||
import com.youlai.system.model.vo.GeneratorPreviewVO;
|
||||
@@ -26,10 +27,10 @@ public class GeneratorController {
|
||||
|
||||
@Operation(summary = "获取数据表分页列表")
|
||||
@GetMapping("/table/page")
|
||||
public PageResult<TablePageVO> getTablePage(
|
||||
public PageResult<TableMetaData> getTablePage(
|
||||
TablePageQuery queryParams
|
||||
) {
|
||||
Page<TablePageVO> result = generatorService.getTablePage(queryParams);
|
||||
Page<TableMetaData> result = generatorService.getTablePage(queryParams);
|
||||
return PageResult.success(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,17 +26,19 @@ public interface GenConfigConverter {
|
||||
@Mapping(source = "fieldConfigs", target = "fieldConfigs")
|
||||
GenConfigForm toGenConfigForm(GenConfig genConfig, List<GenFieldConfig> fieldConfigs);
|
||||
|
||||
List<GenConfigForm.FieldConfig> toFieldConfigList(List<GenFieldConfig> fieldConfigs);
|
||||
List<GenConfigForm.FieldConfig> toGenFieldConfigForm(List<GenFieldConfig> fieldConfigs);
|
||||
|
||||
GenConfigForm.FieldConfig toFieldConfig(GenFieldConfig genFieldConfig);
|
||||
GenConfigForm.FieldConfig toGenFieldConfigForm(GenFieldConfig genFieldConfig);
|
||||
|
||||
|
||||
|
||||
@Mapping(source = "formData", target = "genConfig")
|
||||
@Mapping(source = "formData.fieldConfigs", target = "fieldConfigs")
|
||||
GenConfig toGenConfig(GenConfigForm formData);
|
||||
GenConfig toGenConfigEntity(GenConfigForm formData);
|
||||
|
||||
@Mapping(source = "formData.fieldConfigs", target = "fieldConfigs")
|
||||
List<GenFieldConfig> toGenFieldConfigList(List<GenConfigForm.FieldConfig> fieldConfigs);
|
||||
List<GenFieldConfig> toGenFieldConfigEntity(List<GenConfigForm.FieldConfig> fieldConfigs);
|
||||
|
||||
GenFieldConfig toGenFieldConfigEntity(GenConfigForm.FieldConfig fieldConfig);
|
||||
|
||||
}
|
||||
@@ -2,6 +2,8 @@ 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;
|
||||
@@ -21,9 +23,10 @@ import java.util.List;
|
||||
public interface DatabaseMapper extends BaseMapper<SysDept> {
|
||||
|
||||
|
||||
Page<TablePageVO> getTablePage(Page<TablePageVO> page, TablePageQuery queryParams);
|
||||
Page<TableMetaData> getTablePage(Page<TableMetaData> 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;
|
||||
|
||||
}
|
||||
@@ -4,12 +4,18 @@ 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")
|
||||
@Data
|
||||
@TableName(value = "gen_config")
|
||||
@Getter
|
||||
@Setter
|
||||
public class GenConfig extends BaseEntity {
|
||||
|
||||
/**
|
||||
@@ -28,14 +34,14 @@ public class GenConfig extends BaseEntity {
|
||||
private String moduleName;
|
||||
|
||||
/**
|
||||
* 实体名
|
||||
* 实体类名
|
||||
*/
|
||||
private String entityName;
|
||||
|
||||
/**
|
||||
* 类描述
|
||||
* 业务名
|
||||
*/
|
||||
private String comment;
|
||||
private String businessName;
|
||||
|
||||
/**
|
||||
* 上级菜单ID
|
||||
|
||||
@@ -4,23 +4,23 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
@Schema(description = "数据表分页VO")
|
||||
@Schema(description = "数据表VO")
|
||||
@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")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.youlai.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.youlai.system.model.bo.TableMetaData;
|
||||
import com.youlai.system.model.form.GenConfigForm;
|
||||
import com.youlai.system.model.query.TablePageQuery;
|
||||
import com.youlai.system.model.vo.GeneratorPreviewVO;
|
||||
@@ -23,7 +24,7 @@ public interface GeneratorService {
|
||||
* @param queryParams 查询参数
|
||||
* @return
|
||||
*/
|
||||
Page<TablePageVO> getTablePage(TablePageQuery queryParams);
|
||||
Page<TableMetaData> getTablePage(TablePageQuery queryParams);
|
||||
|
||||
/**
|
||||
* 获取预览生成代码
|
||||
|
||||
@@ -11,17 +11,18 @@ 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.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.GeneratorPreviewVO;
|
||||
import com.youlai.system.model.vo.TableColumnVO;
|
||||
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;
|
||||
@@ -43,15 +44,14 @@ import java.util.*;
|
||||
@RequiredArgsConstructor
|
||||
public class GeneratorServiceImpl implements GeneratorService {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String applicationName;
|
||||
|
||||
private final DatabaseMapper databaseMapper;
|
||||
private final GeneratorProperties generatorProperties;
|
||||
private final GenConfigService genConfigService;
|
||||
private final GenFieldConfigService genFieldConfigService;
|
||||
|
||||
// 注入 spring.application.name
|
||||
@Value("${spring.application.name}")
|
||||
private String applicationName;
|
||||
|
||||
private final GenConfigConverter genConfigConverter;
|
||||
|
||||
/**
|
||||
@@ -60,8 +60,8 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
* @param queryParams 查询参数
|
||||
* @return 分页结果
|
||||
*/
|
||||
public Page<TablePageVO> getTablePage(TablePageQuery queryParams) {
|
||||
Page<TablePageVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());
|
||||
public Page<TableMetaData> getTablePage(TablePageQuery queryParams) {
|
||||
Page<TableMetaData> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());
|
||||
return databaseMapper.getTablePage(page, queryParams);
|
||||
}
|
||||
|
||||
@@ -79,23 +79,73 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
.eq(GenConfig::getTableName, tableName)
|
||||
.last("LIMIT 1")
|
||||
);
|
||||
// 如果没有代码生成配置,则根据表的元数据生成默认配置
|
||||
if (genConfig == null) {
|
||||
TableMetaData tableMetadata = databaseMapper.getTableMetadata(tableName);
|
||||
Assert.isTrue(tableMetadata != null, "未找到表元数据");
|
||||
|
||||
// 查询字段生成配置
|
||||
List<GenFieldConfig> fieldConfigs = genFieldConfigService.list(
|
||||
new LambdaQueryWrapper<>(GenFieldConfig.class)
|
||||
.eq(GenFieldConfig::getConfigId, genConfig.getId())
|
||||
);
|
||||
|
||||
GenConfigForm configFormData = genConfigConverter.toGenConfigForm(genConfig, fieldConfigs);
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
List<GenFieldConfig> genFieldConfigs = null;
|
||||
|
||||
// 获取表的列信息
|
||||
List<ColumnMetaData> tableColumns = databaseMapper.getTableColumns(tableName);
|
||||
|
||||
if (CollectionUtil.isNotEmpty(tableColumns)) {
|
||||
|
||||
// 查询字段生成配置
|
||||
List<GenFieldConfig> configList = genFieldConfigService.list(
|
||||
new LambdaQueryWrapper<>(GenFieldConfig.class)
|
||||
.eq(GenFieldConfig::getConfigId, genConfig.getId())
|
||||
);
|
||||
genFieldConfigs = new ArrayList<>();
|
||||
for (ColumnMetaData tableColumn : tableColumns) {
|
||||
GenFieldConfig fieldConfig = new GenFieldConfig();
|
||||
fieldConfig.setFieldName(tableColumn.getColumnName());
|
||||
fieldConfig.setFieldType(tableColumn.getDataType());
|
||||
fieldConfig.setComment(tableColumn.getColumnComment());
|
||||
|
||||
|
||||
// 如果没有字段生成配置,则根据表的元数据生成默认配置
|
||||
if (CollectionUtil.isNotEmpty(configList)) {
|
||||
for (GenFieldConfig config : configList) {
|
||||
if (StrUtil.equals(config.getFieldName(), fieldConfig.getFieldName())) {
|
||||
fieldConfig = config;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
genFieldConfigs.add(fieldConfig);
|
||||
}
|
||||
}
|
||||
|
||||
GenConfigForm configFormData = genConfigConverter.toGenConfigForm(genConfig, genFieldConfigs);
|
||||
return configFormData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveGenConfig(GenConfigForm formData) {
|
||||
GenConfig genConfig = genConfigConverter.toGenConfig(formData);
|
||||
GenConfig genConfig = genConfigConverter.toGenConfigEntity(formData);
|
||||
genConfigService.saveOrUpdate(genConfig);
|
||||
|
||||
List<GenFieldConfig> genFieldConfigs = genConfigConverter.toGenFieldConfigList(formData.getFieldConfigs());
|
||||
List<GenFieldConfig> genFieldConfigs = genConfigConverter.toGenFieldConfigEntity(formData.getFieldConfigs());
|
||||
|
||||
if (CollectionUtil.isEmpty(genFieldConfigs)) {
|
||||
throw new BusinessException("字段配置不能为空");
|
||||
|
||||
Reference in New Issue
Block a user