开始开发代码生成器,完成数据库 information_schema 的读取

This commit is contained in:
YunaiV 2021-01-30 20:38:27 +08:00
parent 753c7678ee
commit 67c3a62dcf
25 changed files with 514 additions and 237 deletions

View File

@ -37,7 +37,6 @@
<!-- <commons.fileupload.version>1.3.3</commons.fileupload.version>-->
<!-- <poi.version>4.1.2</poi.version>-->
<!-- <velocity.version>1.7</velocity.version>-->
<!-- <jwt.version>0.9.1</jwt.version>-->
<!-- Web 相关 -->
<knife4j.version>3.0.2</knife4j.version>
@ -169,6 +168,13 @@
<version>${spring-boot-admin.version}</version>
</dependency>
<!-- Test 测试相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 工具类相关 -->
<dependency>
<groupId>org.projectlombok</groupId>

View File

@ -5,105 +5,158 @@ package com.ruoyi.common.constant;
*
* @author ruoyi
*/
public class GenConstants
{
/** 单表(增删改查) */
public static final String TPL_CRUD = "crud";
public class GenConstants {
/** 树表(增删改查) */
public static final String TPL_TREE = "tree";
/** 树编码字段 */
/**
* 树编码字段
*/
public static final String TREE_CODE = "treeCode";
/** 树父编码字段 */
/**
* 树父编码字段
*/
public static final String TREE_PARENT_CODE = "treeParentCode";
/** 树名称字段 */
/**
* 树名称字段
*/
public static final String TREE_NAME = "treeName";
/** 上级菜单ID字段 */
/**
* 上级菜单ID字段
*/
public static final String PARENT_MENU_ID = "parentMenuId";
/** 上级菜单名称字段 */
/**
* 上级菜单名称字段
*/
public static final String PARENT_MENU_NAME = "parentMenuName";
/** 数据库字符串类型 */
public static final String[] COLUMNTYPE_STR = { "char", "varchar", "nvarchar", "varchar2" };
/**
* 数据库字符串类型
*/
public static final String[] COLUMNTYPE_STR = {"char", "varchar", "nvarchar", "varchar2"};
/** 数据库文本类型 */
public static final String[] COLUMNTYPE_TEXT = { "tinytext", "text", "mediumtext", "longtext" };
/**
* 数据库文本类型
*/
public static final String[] COLUMNTYPE_TEXT = {"tinytext", "text", "mediumtext", "longtext"};
/** 数据库时间类型 */
public static final String[] COLUMNTYPE_TIME = { "datetime", "time", "date", "timestamp" };
/**
* 数据库时间类型
*/
public static final String[] COLUMNTYPE_TIME = {"datetime", "time", "date", "timestamp"};
/** 数据库数字类型 */
public static final String[] COLUMNTYPE_NUMBER = { "tinyint", "smallint", "mediumint", "int", "number", "integer",
"bit", "bigint", "float", "double", "decimal" };
/**
* 数据库数字类型
*/
public static final String[] COLUMNTYPE_NUMBER = {"tinyint", "smallint", "mediumint", "int", "number", "integer",
"bit", "bigint", "float", "double", "decimal"};
/** 页面不需要编辑字段 */
public static final String[] COLUMNNAME_NOT_EDIT = { "id", "create_by", "create_time", "del_flag" };
/**
* 页面不需要编辑字段
*/
public static final String[] COLUMNNAME_NOT_EDIT = {"id", "create_by", "create_time", "del_flag"};
/** 页面不需要显示的列表字段 */
public static final String[] COLUMNNAME_NOT_LIST = { "id", "create_by", "create_time", "del_flag", "update_by",
"update_time" };
/**
* 页面不需要显示的列表字段
*/
public static final String[] COLUMNNAME_NOT_LIST = {"id", "create_by", "create_time", "del_flag", "update_by",
"update_time"};
/** 页面不需要查询字段 */
public static final String[] COLUMNNAME_NOT_QUERY = { "id", "create_by", "create_time", "del_flag", "update_by",
"update_time", "remark" };
/**
* 页面不需要查询字段
*/
public static final String[] COLUMNNAME_NOT_QUERY = {"id", "create_by", "create_time", "del_flag", "update_by",
"update_time", "remark"};
/** Entity基类字段 */
public static final String[] BASE_ENTITY = { "createBy", "createTime", "updateBy", "updateTime", "remark" };
/**
* Entity基类字段
*/
public static final String[] BASE_ENTITY = {"createBy", "createTime", "updateBy", "updateTime", "remark"};
/** Tree基类字段 */
public static final String[] TREE_ENTITY = { "parentName", "parentId", "orderNum", "ancestors", "children" };
/**
* Tree基类字段
*/
public static final String[] TREE_ENTITY = {"parentName", "parentId", "orderNum", "ancestors", "children"};
/** 文本框 */
/**
* 文本框
*/
public static final String HTML_INPUT = "input";
/** 文本域 */
/**
* 文本域
*/
public static final String HTML_TEXTAREA = "textarea";
/** 下拉框 */
/**
* 下拉框
*/
public static final String HTML_SELECT = "select";
/** 单选框 */
/**
* 单选框
*/
public static final String HTML_RADIO = "radio";
/** 复选框 */
/**
* 复选框
*/
public static final String HTML_CHECKBOX = "checkbox";
/** 日期控件 */
/**
* 日期控件
*/
public static final String HTML_DATETIME = "datetime";
/** 上传控件 */
/**
* 上传控件
*/
public static final String HTML_UPLOAD_IMAGE = "uploadImage";
/** 富文本控件 */
/**
* 富文本控件
*/
public static final String HTML_EDITOR = "editor";
/** 字符串类型 */
/**
* 字符串类型
*/
public static final String TYPE_STRING = "String";
/** 整型 */
/**
* 整型
*/
public static final String TYPE_INTEGER = "Integer";
/** 长整型 */
/**
* 长整型
*/
public static final String TYPE_LONG = "Long";
/** 浮点型 */
/**
* 浮点型
*/
public static final String TYPE_DOUBLE = "Double";
/** 高精度计算类型 */
/**
* 高精度计算类型
*/
public static final String TYPE_BIGDECIMAL = "BigDecimal";
/** 时间类型 */
/**
* 时间类型
*/
public static final String TYPE_DATE = "Date";
/** 模糊查询 */
/**
* 模糊查询
*/
public static final String QUERY_LIKE = "LIKE";
/** 需要 */
/**
* 需要
*/
public static final String REQUIRE = "1";
}

View File

@ -3,6 +3,7 @@ package com.ruoyi.generator.domain;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import org.apache.commons.lang3.ArrayUtils;
import com.ruoyi.common.constant.GenConstants;
import com.ruoyi.common.core.domain.BaseEntity;
@ -13,108 +14,87 @@ import com.ruoyi.common.utils.StringUtils;
*
* @author ruoyi
*/
public class GenTable extends BaseEntity
{
private static final long serialVersionUID = 1L;
public class GenTable extends BaseEntity {
/** 编号 */
private Long tableId;
/** 表名称 */
@NotBlank(message = "表名称不能为空")
private String tableName;
/** 表描述 */
@NotBlank(message = "表描述不能为空")
private String tableComment;
/** 实体类名称(首字母大写) */
@NotBlank(message = "实体类名称不能为空")
private String className;
/** 使用的模板crud单表操作 tree树表操作 */
private String tplCategory;
/** 生成包路径 */
/**
* 生成包路径
*/
@NotBlank(message = "生成包路径不能为空")
private String packageName;
/** 生成模块名 */
@NotBlank(message = "生成模块名不能为空")
private String moduleName;
/** 生成业务名 */
@NotBlank(message = "生成业务名不能为空")
private String businessName;
/** 生成功能名 */
@NotBlank(message = "生成功能名不能为空")
private String functionName;
/** 生成作者 */
@NotBlank(message = "作者不能为空")
private String functionAuthor;
/** 生成代码方式0zip压缩包 1自定义路径 */
/**
* 生成代码方式0zip压缩包 1自定义路径
*/
private String genType;
/** 生成路径(不填默认项目路径) */
/**
* 生成路径不填默认项目路径
*/
private String genPath;
/** 主键信息 */
/**
* 主键信息
*/
private GenTableColumn pkColumn;
/** 表列信息 */
/**
* 表列信息
*/
@Valid
private List<GenTableColumn> columns;
/** 其它生成选项 */
/**
* 其它生成选项
*/
private String options;
/** 树编码字段 */
/**
* 树编码字段
*/
private String treeCode;
/** 树父编码字段 */
/**
* 树父编码字段
*/
private String treeParentCode;
/** 树名称字段 */
/**
* 树名称字段
*/
private String treeName;
/** 上级菜单ID字段 */
/**
* 上级菜单ID字段
*/
private String parentMenuId;
/** 上级菜单名称字段 */
/**
* 上级菜单名称字段
*/
private String parentMenuName;
public boolean isTree()
{
public boolean isTree() {
return isTree(this.tplCategory);
}
public static boolean isTree(String tplCategory)
{
public static boolean isTree(String tplCategory) {
return tplCategory != null && StringUtils.equals(GenConstants.TPL_TREE, tplCategory);
}
public boolean isCrud()
{
public boolean isCrud() {
return isCrud(this.tplCategory);
}
public static boolean isCrud(String tplCategory)
{
public static boolean isCrud(String tplCategory) {
return tplCategory != null && StringUtils.equals(GenConstants.TPL_CRUD, tplCategory);
}
public boolean isSuperColumn(String javaField)
{
public boolean isSuperColumn(String javaField) {
return isSuperColumn(this.tplCategory, javaField);
}
public static boolean isSuperColumn(String tplCategory, String javaField)
{
if (isTree(tplCategory))
{
public static boolean isSuperColumn(String tplCategory, String javaField) {
if (isTree(tplCategory)) {
return StringUtils.equalsAnyIgnoreCase(javaField,
ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY));
}

View File

@ -6,7 +6,7 @@ import com.ruoyi.common.utils.StringUtils;
/**
* 代码生成业务字段表 gen_table_column
*
*
* @author ruoyi
*/
public class GenTableColumn extends BaseEntity
@ -68,86 +68,6 @@ public class GenTableColumn extends BaseEntity
/** 排序 */
private Integer sort;
public void setColumnId(Long columnId)
{
this.columnId = columnId;
}
public Long getColumnId()
{
return columnId;
}
public void setTableId(Long tableId)
{
this.tableId = tableId;
}
public Long getTableId()
{
return tableId;
}
public void setColumnName(String columnName)
{
this.columnName = columnName;
}
public String getColumnName()
{
return columnName;
}
public void setColumnComment(String columnComment)
{
this.columnComment = columnComment;
}
public String getColumnComment()
{
return columnComment;
}
public void setColumnType(String columnType)
{
this.columnType = columnType;
}
public String getColumnType()
{
return columnType;
}
public void setJavaType(String javaType)
{
this.javaType = javaType;
}
public String getJavaType()
{
return javaType;
}
public void setJavaField(String javaField)
{
this.javaField = javaField;
}
public String getJavaField()
{
return javaField;
}
public void setIsPk(String isPk)
{
this.isPk = isPk;
}
public String getIsPk()
{
return isPk;
}
public boolean isPk()
{
return isPk(this.isPk);
@ -278,46 +198,6 @@ public class GenTableColumn extends BaseEntity
return isQuery != null && StringUtils.equals("1", isQuery);
}
public void setQueryType(String queryType)
{
this.queryType = queryType;
}
public String getQueryType()
{
return queryType;
}
public String getHtmlType()
{
return htmlType;
}
public void setHtmlType(String htmlType)
{
this.htmlType = htmlType;
}
public void setDictType(String dictType)
{
this.dictType = dictType;
}
public String getDictType()
{
return dictType;
}
public void setSort(Integer sort)
{
this.sort = sort;
}
public Integer getSort()
{
return sort;
}
public boolean isSuperColumn()
{
return isSuperColumn(this.javaField);

View File

@ -10,8 +10,12 @@ import lombok.Getter;
@AllArgsConstructor
public enum SysLoginLogTypeEnum {
LOGIN(1),
LOGOUT(2);
LOGIN_USERNAME(100), // 使用账号登录
LOGOUT_SELF(200), // 自己主动登出
LOGOUT_TIMEOUT(201), // 超时登出
LOGOUT_DELETE(202), // 强制退出
;
/**
* 日志类型

View File

@ -137,7 +137,7 @@ public class SysAuthServiceImpl implements SysAuthService {
private void createLoginLog(String username, SysLoginResultEnum loginResult) {
SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO();
reqVO.setLogType(SysLoginLogTypeEnum.LOGIN.getType());
reqVO.setLogType(SysLoginLogTypeEnum.LOGIN_USERNAME.getType());
reqVO.setTraceId(TracerUtils.getTraceId());
reqVO.setUsername(username);
reqVO.setUserAgent(ServletUtils.getUserAgent());

View File

@ -0,0 +1,21 @@
package cn.iocoder.dashboard.modules.tool.controller.codegen;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodeGenTablePageItemRespVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
@RestController
@RequestMapping("/tool/code-gen")
public class ToolCodeGenController {
@GetMapping("/table/page")
public CommonResult<PageResult<ToolCodeGenTablePageItemRespVO>> getCodeGenTablePage() {
return success(null);
}
}

View File

@ -0,0 +1,4 @@
package cn.iocoder.dashboard.modules.tool.controller.codegen.vo;
public class ToolCodeGenTableBaseRespVO {
}

View File

@ -0,0 +1,7 @@
package cn.iocoder.dashboard.modules.tool.controller.codegen.vo;
public class ToolCodeGenTablePageItemRespVO extends ToolCodeGenTableBaseRespVO {
}

View File

@ -0,0 +1 @@
package cn.iocoder.dashboard.modules.tool.controller.codegen.vo;

View File

@ -0,0 +1 @@
package cn.iocoder.dashboard.modules.tool.controller;

View File

@ -0,0 +1,18 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.dao.coegen;
import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen.ToolInformationSchemaColumnDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ToolInformationSchemaColumnMapper extends BaseMapperX<ToolInformationSchemaColumnDO> {
default List<ToolInformationSchemaColumnDO> selectListByTableName(String tableName) {
return selectList(new QueryWrapper<ToolInformationSchemaColumnDO>().eq("table_name", tableName)
.orderByAsc("ordinal_position"));
}
}

View File

@ -0,0 +1,17 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.dao.coegen;
import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen.ToolInformationSchemaTableDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ToolInformationSchemaTableMapper extends BaseMapperX<ToolInformationSchemaTableDO> {
default List<ToolInformationSchemaTableDO> selectListByTableSchema(String tableSchema) {
return selectList(new QueryWrapper<ToolInformationSchemaTableDO>().eq("table_schema", tableSchema));
}
}

View File

@ -0,0 +1 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.dao;

View File

@ -0,0 +1,22 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 代码生成 column 字段定义
*
* @author 芋道源码
*/
@TableName(value = "tool_codegen_table_column", autoResultMap = true)
@Data
@Builder
@EqualsAndHashCode(callSuper = true)
public class ToolCodegenColumnDO extends BaseDO {
}

View File

@ -0,0 +1,87 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.dashboard.modules.tool.enums.codegen.ToolCodeGenTemplateTypeEnum;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 代码生成 table 表定义
*
* @author 芋道源码
*/
@TableName(value = "tool_codegen_table", autoResultMap = true)
@Data
@Builder
@EqualsAndHashCode(callSuper = true)
public class ToolCodegenTableDO extends BaseDO {
// ========== 表相关字段 ==========
/**
* ID 编号
*/
private Long id;
/**
* 表名称
*/
// @NotBlank(message = "表名称不能为空")
private String tableName;
/**
* 表描述
*/
// @NotBlank(message = "表描述不能为空")
private String tableComment;
/**
* 备注
*/
private String remark;
// ========== 类相关字段 ==========
/**
* 模块名即一级目录
*
* 例如说infrasystemtool 等等
*/
// @NotBlank(message = "模块名不能为空")
private String moduleName;
/**
* 业务名即二级目录
*
* 例如说userpermissiondict 等等
*/
// @NotBlank(message = "生成业务名不能为空")
private String businessName;
/**
* 类名称首字母大写
*
* 例如说SysUserSysMenuSysDictData 等等
*/
// @NotBlank(message = "实体类名称不能为空")
private String className;
/**
* 类描述
*/
// @NotBlank(message = "生成功能名不能为空")
private String classComment;
/**
* 作者
*/
// @NotBlank(message = "作者不能为空")
private String author;
// ========== 生成相关字段 ==========
/**
* 模板类型
*
* 枚举 {@link ToolCodeGenTemplateTypeEnum}
*/
private Integer templateType;
// ========== 菜单相关字段 ==========
}

View File

@ -0,0 +1,45 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder;
import lombok.Data;
/**
* MySQL 数据库中的 column 字段定义
*
* @author 芋道源码
*/
@TableName(value = "information_schema.columns", autoResultMap = true)
@Data
@Builder
public class ToolInformationSchemaColumnDO {
/**
* 表名称
*/
private String tableName;
/**
* 字段名
*/
private String columnName;
/**
* 字段类型
*/
private String columnType;
/**
* 是否允许为空
*/
@TableField("case when is_nullable = 'yes' then '1' else '0' end")
private Boolean nullable;
/**
* 是否主键
*/
@TableField("case when column_key = 'PRI' then '1' else '0' end")
private Boolean primaryKey;
/**
* 排序字段
*/
private Integer ordinalPosition;
}

View File

@ -0,0 +1,36 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder;
import lombok.Data;
import java.util.Date;
/**
* MySQL 数据库中的 table 表定义
*
* @author 芋道源码
*/
@TableName(value = "information_schema.tables", autoResultMap = true)
@Data
@Builder
public class ToolInformationSchemaTableDO {
/**
* 数据库
*/
private String tableSchema;
/**
* 表名称
*/
private String tableName;
/**
* 表描述
*/
private String tableComment;
/**
* 创建时间
*/
private Date createTime;
}

View File

@ -0,0 +1 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject;

View File

@ -0,0 +1,25 @@
package cn.iocoder.dashboard.modules.tool.enums.codegen;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 代码生成模板类型
*
* @author 芋道源码
*/
@AllArgsConstructor
@Getter
public enum ToolCodeGenTemplateTypeEnum {
CRUD(1), // 基础 CRUD
TREE(2), // 树形 CRUD
SUB(3) // 子表 CRUD
;
/**
* 类型
*/
private final Integer type;
}

View File

@ -0,0 +1 @@
package cn.iocoder.dashboard.modules.tool.enums;

View File

@ -0,0 +1,7 @@
/**
* tool 包下我们放研发工具提升研发效率与质量
* 例如说代码生成器接口文档等等
*
* 缩写tool
*/
package cn.iocoder.dashboard.modules.tool;

View File

@ -0,0 +1,7 @@
package cn.iocoder.dashboard;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestApplication {
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.dao.coegen;
import cn.iocoder.dashboard.TestApplication;
import cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen.ToolInformationSchemaColumnDO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ToolInformationSchemaColumnMapperTest {
@Resource
private ToolInformationSchemaColumnMapper toolInformationSchemaColumnMapper;
@Test
public void testSelectListByTableName() {
List<ToolInformationSchemaColumnDO> columns = toolInformationSchemaColumnMapper
.selectListByTableName("inf_config");
assertTrue(columns.size() > 0);
}
}

View File

@ -0,0 +1,26 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.dao.coegen;
import cn.iocoder.dashboard.TestApplication;
import cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen.ToolInformationSchemaTableDO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ToolInformationSchemaTableMapperTest {
@Resource
private ToolInformationSchemaTableMapper toolInformationSchemaTableMapper;
@Test
public void tstSelectListByTableSchema() {
List<ToolInformationSchemaTableDO> tables = toolInformationSchemaTableMapper
.selectListByTableSchema("ruoyi-vue-pro");
assertTrue(tables.size() > 0);
}
}