fix(generator): 适配Kingbase V2元数据与代码模板
This commit is contained in:
1 parent
22686e2c39
commit
dc585bdb28
6 files changed
+85
-9
No files matched your search
@@ -2,7 +2,7 @@ package com.ruoyi.common.constant;
|
||||
|
||||
/**
|
||||
* 代码生成通用常量
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class GenConstants
|
||||
@@ -32,7 +32,7 @@ public class GenConstants
|
||||
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", "character varying" };
|
||||
|
||||
/** 数据库文本类型 */
|
||||
public static final String[] COLUMNTYPE_TEXT = { "tinytext", "text", "mediumtext", "longtext" };
|
||||
@@ -42,7 +42,7 @@ public class GenConstants
|
||||
|
||||
/** 数据库数字类型 */
|
||||
public static final String[] COLUMNTYPE_NUMBER = { "tinyint", "smallint", "mediumint", "int", "number", "integer",
|
||||
"bit", "bigint", "float", "double", "decimal" };
|
||||
"bit", "bigint", "float", "double", "decimal", "numeric" };
|
||||
|
||||
/** 页面不需要编辑字段 */
|
||||
public static final String[] COLUMNNAME_NOT_EDIT = { "id", "create_by", "create_time", "del_flag" };
|
||||
|
||||
+63
-3
@@ -4,13 +4,20 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.velocity.Template;
|
||||
@@ -46,6 +53,9 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(GenTableServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Autowired
|
||||
private GenTableMapper genTableMapper;
|
||||
|
||||
@@ -180,7 +190,7 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
if (row > 0)
|
||||
{
|
||||
// 保存列信息
|
||||
List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
|
||||
List<GenTableColumn> genTableColumns = selectDbTableColumnsByName(tableName);
|
||||
for (GenTableColumn column : genTableColumns)
|
||||
{
|
||||
GenUtils.initColumnField(column, table);
|
||||
@@ -299,7 +309,7 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
List<GenTableColumn> tableColumns = table.getColumns();
|
||||
Map<String, GenTableColumn> tableColumnMap = tableColumns.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, Function.identity()));
|
||||
|
||||
List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
|
||||
List<GenTableColumn> dbTableColumns = selectDbTableColumnsByName(tableName);
|
||||
if (StringUtils.isEmpty(dbTableColumns))
|
||||
{
|
||||
throw new ServiceException("同步数据失败,原表结构不存在");
|
||||
@@ -528,4 +538,54 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
}
|
||||
return genPath + File.separator + VelocityUtils.getFileName(template, table);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询表列信息并补充自增列元数据
|
||||
*/
|
||||
private List<GenTableColumn> selectDbTableColumnsByName(String tableName)
|
||||
{
|
||||
List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
|
||||
fillColumnIncrementByJdbc(tableName, dbTableColumns);
|
||||
return dbTableColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 JDBC DatabaseMetaData 补充自增/Identity列元数据,兼顾跨数据库方言
|
||||
*/
|
||||
private void fillColumnIncrementByJdbc(String tableName, List<GenTableColumn> columns)
|
||||
{
|
||||
if (StringUtils.isEmpty(columns))
|
||||
{
|
||||
return;
|
||||
}
|
||||
try (Connection conn = dataSource.getConnection())
|
||||
{
|
||||
DatabaseMetaData meta = conn.getMetaData();
|
||||
String catalog = conn.getCatalog();
|
||||
String schema = conn.getSchema();
|
||||
Set<String> autoCols = new HashSet<>();
|
||||
try (ResultSet rs = meta.getColumns(catalog, schema, tableName, null))
|
||||
{
|
||||
while (rs.next())
|
||||
{
|
||||
if ("YES".equalsIgnoreCase(rs.getString("IS_AUTOINCREMENT")))
|
||||
{
|
||||
autoCols.add(rs.getString("COLUMN_NAME"));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (GenTableColumn column : columns)
|
||||
{
|
||||
if (autoCols.contains(column.getColumnName()))
|
||||
{
|
||||
column.setIsIncrement("1");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
log.error("通过 JDBC DatabaseMetaData 读取自增列失败,已中止 Generator 元数据初始化", e);
|
||||
throw new ServiceException("读取数据库自增列元数据失败,请检查数据库连接与元数据权限");
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -40,7 +40,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
</select>
|
||||
|
||||
<select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult">
|
||||
select column_name, (case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else '0' end) as is_required, (case when column_key = 'PRI' then '1' else '0' end) as is_pk, ordinal_position as sort, column_comment, (case when extra = 'auto_increment' then '1' else '0' end) as is_increment, column_type
|
||||
select column_name,
|
||||
(case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else '0' end) as is_required,
|
||||
(case when column_key = 'PRI' then '1' else '0' end) as is_pk,
|
||||
ordinal_position as sort,
|
||||
column_comment,
|
||||
(case when extra = 'auto_increment' then '1' else '0' end) as is_increment,
|
||||
column_type
|
||||
from information_schema.columns where table_schema = (select current_schema()) and table_name = (#{tableName})
|
||||
order by ordinal_position
|
||||
</select>
|
||||
|
||||
@@ -79,7 +79,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
<select id="selectDbTableList" parameterType="GenTable" resultMap="GenTableResult">
|
||||
select table_name, table_comment, create_time, update_time from information_schema.tables
|
||||
where table_schema = (select current_schema())
|
||||
AND table_name NOT LIKE 'qrtz\_%' AND table_name NOT LIKE 'gen\_%'
|
||||
AND table_name NOT LIKE 'qrtz\_%' AND table_name NOT LIKE 'gen\_%' AND table_name NOT LIKE 'bak\_v1\_%'
|
||||
AND table_name NOT IN (select table_name from gen_table)
|
||||
<if test="tableName != null and tableName != ''">
|
||||
AND lower(table_name) like lower(concat('%', #{tableName}, '%'))
|
||||
@@ -98,7 +98,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
|
||||
<select id="selectDbTableListByNames" resultMap="GenTableResult">
|
||||
select table_name, table_comment, create_time, update_time from information_schema.tables
|
||||
where table_name NOT LIKE 'qrtz\_%' and table_name NOT LIKE 'gen\_%' and table_schema = (select current_schema())
|
||||
where table_name NOT LIKE 'qrtz\_%' and table_name NOT LIKE 'gen\_%' and table_name NOT LIKE 'bak\_v1\_%' and table_schema = (select current_schema())
|
||||
and table_name in
|
||||
<foreach collection="array" item="name" open="(" separator="," close=")">
|
||||
#{name}
|
||||
|
||||
@@ -40,8 +40,13 @@ public class ${ClassName} extends ${Entity}
|
||||
#if($parentheseIndex != -1)
|
||||
@Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")
|
||||
#elseif($column.javaType == 'Date')
|
||||
#if($column.columnType == 'date')
|
||||
@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
@Excel(name = "${comment}", width = 30, dateFormat = "yyyy-MM-dd")
|
||||
#else
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@Excel(name = "${comment}", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
|
||||
#end
|
||||
#else
|
||||
@Excel(name = "${comment}")
|
||||
#end
|
||||
|
||||
@@ -31,8 +31,13 @@ public class ${subClassName} extends BaseEntity
|
||||
#if($parentheseIndex != -1)
|
||||
@Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")
|
||||
#elseif($column.javaType == 'Date')
|
||||
#if($column.columnType == 'date')
|
||||
@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
@Excel(name = "${comment}", width = 30, dateFormat = "yyyy-MM-dd")
|
||||
#else
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@Excel(name = "${comment}", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
|
||||
#end
|
||||
#else
|
||||
@Excel(name = "${comment}")
|
||||
#end
|
||||
|
||||
Reference in new issue
Block a user