!149 代码生成器支持 MySQL、Oracle、PostgreSQL、SQLServer、DM 等数据库
Merge pull request !149 from 芋道源码/feature/1.6.2plp
commit
99137289e1
File diff suppressed because one or more lines are too long
@ -0,0 +1,22 @@
|
||||
package cn.iocoder.yudao.module.infra.controller.admin.codegen.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
@ApiModel("管理后台 - 基于数据库的表结构,创建代码生成器的表和字段定义 Request VO")
|
||||
@Data
|
||||
public class CodegenCreateListReqVO {
|
||||
|
||||
@ApiModelProperty(value = "数据源配置的编号", required = true, example = "1")
|
||||
@NotNull(message = "数据源配置的编号不能为空")
|
||||
private Long dataSourceConfigId;
|
||||
|
||||
@ApiModelProperty(value = "表名数组", required = true, example = "[1, 2, 3]")
|
||||
@NotNull(message = "表名数组不能为空")
|
||||
private List<String> tableNames;
|
||||
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@ApiModel("管理后台 - 数据库的表定义 Response VO")
|
||||
@Data
|
||||
public class DatabaseTableRespVO {
|
||||
|
||||
@ApiModelProperty(value = "表名称", required = true, example = "yuanma")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "表描述", required = true, example = "芋道源码")
|
||||
private String comment;
|
||||
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
package cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ApiModel("管理后台 - 数据字典的表定义 Response VO")
|
||||
@Data
|
||||
public class SchemaTableRespVO {
|
||||
|
||||
@ApiModelProperty(value = "数据库", required = true, example = "yudao")
|
||||
private String tableSchema;
|
||||
|
||||
@ApiModelProperty(value = "表名称", required = true, example = "yuanma")
|
||||
private String tableName;
|
||||
|
||||
@ApiModelProperty(value = "表描述", required = true, example = "芋道源码")
|
||||
private String tableComment;
|
||||
|
||||
@ApiModelProperty(value = "创建时间", required = true)
|
||||
private Date createTime;
|
||||
|
||||
}
|
||||
@ -1,54 +0,0 @@
|
||||
package cn.iocoder.yudao.module.infra.dal.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 SchemaColumnDO {
|
||||
|
||||
/**
|
||||
* 表名称
|
||||
*/
|
||||
private String tableName;
|
||||
/**
|
||||
* 字段名
|
||||
*/
|
||||
private String columnName;
|
||||
/**
|
||||
* 字段类型
|
||||
*/
|
||||
private String columnType;
|
||||
/**
|
||||
* 字段描述
|
||||
*/
|
||||
private String columnComment;
|
||||
/**
|
||||
* 是否允许为空
|
||||
*/
|
||||
@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;
|
||||
/**
|
||||
* 是否自增
|
||||
*/
|
||||
@TableField("case when extra = 'auto_increment' then '1' else '0' end")
|
||||
private Boolean autoIncrement;
|
||||
/**
|
||||
* 排序字段
|
||||
*/
|
||||
private Integer ordinalPosition;
|
||||
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
package cn.iocoder.yudao.module.infra.dal.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 SchemaTableDO {
|
||||
|
||||
/**
|
||||
* 数据库
|
||||
*/
|
||||
private String tableSchema;
|
||||
/**
|
||||
* 表名称
|
||||
*/
|
||||
private String tableName;
|
||||
/**
|
||||
* 表描述
|
||||
*/
|
||||
private String tableComment;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
package cn.iocoder.yudao.module.infra.dal.mysql.codegen;
|
||||
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.SchemaColumnDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface SchemaColumnMapper extends BaseMapperX<SchemaColumnDO> {
|
||||
|
||||
default List<SchemaColumnDO> selectListByTableName(String tableSchema, String tableName) {
|
||||
return selectList(new QueryWrapper<SchemaColumnDO>().eq("table_name", tableName)
|
||||
.eq("table_schema", tableSchema)
|
||||
.orderByAsc("ordinal_position"));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
package cn.iocoder.yudao.module.infra.dal.mysql.codegen;
|
||||
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.SchemaTableDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface SchemaTableMapper extends BaseMapperX<SchemaTableDO> {
|
||||
|
||||
default List<SchemaTableDO> selectList(Collection<String> tableSchemas, String tableName, String tableComment) {
|
||||
return selectList(new QueryWrapperX<SchemaTableDO>().in("table_schema", tableSchemas)
|
||||
.likeIfPresent("table_name", tableName)
|
||||
.likeIfPresent("table_comment", tableComment));
|
||||
}
|
||||
|
||||
default SchemaTableDO selectByTableSchemaAndTableName(String tableSchema, String tableName) {
|
||||
return selectOne(new QueryWrapper<SchemaTableDO>().eq("table_schema",tableSchema)
|
||||
.eq("table_name", tableName));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package cn.iocoder.yudao.module.infra.enums.codegen;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 代码生成的导入类型
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum CodegenImportTypeEnum {
|
||||
|
||||
DB(1), // 从 information_schema 的 table 和 columns 表导入
|
||||
SQL(2); // 基于建表 SQL 语句导入
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private final Integer type;
|
||||
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package cn.iocoder.yudao.module.infra.service.db;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO;
|
||||
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
|
||||
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
|
||||
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
|
||||
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
||||
import com.baomidou.mybatisplus.generator.config.rules.DateType;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据库表 Service 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Service
|
||||
public class DatabaseTableServiceImpl implements DatabaseTableService {
|
||||
|
||||
@Resource
|
||||
private DataSourceConfigService dataSourceConfigService;
|
||||
|
||||
@Override
|
||||
public List<TableInfo> getTableList(Long dataSourceConfigId, String nameLike, String commentLike) {
|
||||
List<TableInfo> tables = getTableList0(dataSourceConfigId, null);
|
||||
return tables.stream().filter(tableInfo -> (StrUtil.isEmpty(nameLike) || tableInfo.getName().contains(nameLike))
|
||||
&& (StrUtil.isEmpty(commentLike) || tableInfo.getComment().contains(commentLike)))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableInfo getTable(Long dataSourceConfigId, String name) {
|
||||
return CollUtil.getFirst(getTableList0(dataSourceConfigId, name));
|
||||
}
|
||||
|
||||
public List<TableInfo> getTableList0(Long dataSourceConfigId, String name) {
|
||||
// 获得数据源配置
|
||||
DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId);
|
||||
Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId);
|
||||
|
||||
// 使用 MyBatis Plus Generator 解析表结构
|
||||
DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder(config.getUrl(), config.getUsername(),
|
||||
config.getPassword()).build();
|
||||
StrategyConfig.Builder strategyConfig = new StrategyConfig.Builder();
|
||||
if (StrUtil.isNotEmpty(name)) {
|
||||
strategyConfig.addInclude(name);
|
||||
}
|
||||
GlobalConfig globalConfig = new GlobalConfig.Builder().dateType(DateType.ONLY_DATE).build(); // 只使用 Date 类型,不使用 LocalDate
|
||||
ConfigBuilder builder = new ConfigBuilder(null, dataSourceConfig, strategyConfig.build(),
|
||||
null, globalConfig, null);
|
||||
// 按照名字排序
|
||||
List<TableInfo> tables = builder.getTableInfoList();
|
||||
tables.sort(Comparator.comparing(TableInfo::getName));
|
||||
return tables;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package cn.iocoder.yudao.module.infra.service;
|
||||
|
||||
import com.baomidou.mybatisplus.generator.IDatabaseQuery.DefaultDatabaseQuery;
|
||||
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
|
||||
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DefaultDatabaseQueryTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder("jdbc:oracle:thin:@127.0.0.1:1521:xe",
|
||||
"root", "123456").build();
|
||||
// StrategyConfig strategyConfig = new StrategyConfig.Builder().build();
|
||||
|
||||
ConfigBuilder builder = new ConfigBuilder(null, dataSourceConfig, null, null, null, null);
|
||||
|
||||
DefaultDatabaseQuery query = new DefaultDatabaseQuery(builder);
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
List<TableInfo> tableInfos = query.queryTables();
|
||||
System.out.println(tableInfos.size());
|
||||
System.out.println(System.currentTimeMillis() - time);
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
Loading…
Reference in New Issue