refactor: refactor JdbcTypeMapper in "codegen" module

This commit is contained in:
Michael Yang 2024-02-26 11:24:18 +08:00
parent 203f4d5236
commit 0ff7384581
2 changed files with 13 additions and 9 deletions

View File

@ -25,6 +25,7 @@ import java.util.Map;
/** /**
* 默认方言抽象类 * 默认方言抽象类
*
* @author michael * @author michael
*/ */
public abstract class AbstractJdbcDialect implements IDialect { public abstract class AbstractJdbcDialect implements IDialect {
@ -46,15 +47,15 @@ public abstract class AbstractJdbcDialect implements IDialect {
column.setRawType(columnMetaData.getColumnTypeName(i)); column.setRawType(columnMetaData.getColumnTypeName(i));
column.setRawLength(columnMetaData.getColumnDisplaySize(i)); column.setRawLength(columnMetaData.getColumnDisplaySize(i));
String jdbcType = columnMetaData.getColumnClassName(i);
column.setPropertyType(JdbcTypeMapping.getType(jdbcType, column.getRawLength()));
column.setAutoIncrement(columnMetaData.isAutoIncrement(i)); column.setAutoIncrement(columnMetaData.isAutoIncrement(i));
column.setNullable(columnMetaData.isNullable(i)); column.setNullable(columnMetaData.isNullable(i));
//注释 //注释
column.setComment(columnRemarks.get(column.getName())); column.setComment(columnRemarks.get(column.getName()));
String jdbcType = columnMetaData.getColumnClassName(i);
column.setPropertyType(JdbcTypeMapping.getType(jdbcType, table, column));
table.addColumn(column); table.addColumn(column);
} }
} }
@ -104,6 +105,4 @@ public abstract class AbstractJdbcDialect implements IDialect {
protected abstract String forBuildColumnsSql(String schema, String tableName); protected abstract String forBuildColumnsSql(String schema, String tableName);
} }

View File

@ -15,6 +15,8 @@
*/ */
package com.mybatisflex.codegen.dialect; package com.mybatisflex.codegen.dialect;
import com.mybatisflex.codegen.entity.Column;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import java.util.HashMap; import java.util.HashMap;
@ -22,6 +24,7 @@ import java.util.Map;
/** /**
* 字段类型映射 * 字段类型映射
*
* @author michael * @author michael
*/ */
public class JdbcTypeMapping { public class JdbcTypeMapping {
@ -67,17 +70,19 @@ public class JdbcTypeMapping {
registerMapping("java.time.LocalDate", "java.util.Date"); registerMapping("java.time.LocalDate", "java.util.Date");
} }
static String getType(String jdbcType, int length) { static String getType(String jdbcType, Table table, Column column) {
if (mapper != null) { if (mapper != null) {
return mapper.getType(jdbcType, length); String type = mapper.getType(jdbcType, table, column);
if (StringUtil.isNotBlank(type)) {
return type;
}
} }
String registered = mapping.get(jdbcType); String registered = mapping.get(jdbcType);
return StringUtil.isNotBlank(registered) ? registered : jdbcType; return StringUtil.isNotBlank(registered) ? registered : jdbcType;
} }
public interface JdbcTypeMapper { public interface JdbcTypeMapper {
String getType(String jdbcType, int length); String getType(String jdbcType, Table table, Column column);
} }
} }