feat: add JdbcTypeMapper to customize bean property type

This commit is contained in:
开源海哥 2023-08-08 09:51:01 +08:00
parent 5fa138dee0
commit 2d60efe747
2 changed files with 20 additions and 2 deletions

View File

@ -25,6 +25,7 @@ import java.util.Map;
/**
* 默认方言抽象类
*
* @author michael
*/
public abstract class JdbcDialect implements IDialect {
@ -47,7 +48,7 @@ public abstract class JdbcDialect implements IDialect {
column.setRawLength(columnMetaData.getColumnDisplaySize(i));
String jdbcType = columnMetaData.getColumnClassName(i);
column.setPropertyType(JdbcTypeMapping.getType(jdbcType));
column.setPropertyType(JdbcTypeMapping.getType(jdbcType, column.getRawLength()));
column.setAutoIncrement(columnMetaData.isAutoIncrement(i));

View File

@ -29,6 +29,7 @@ public class JdbcTypeMapping {
}
private static final Map<String, String> mapping = new HashMap<>();
private static JdbcTypeMapper mapper;
static {
registerMapping("[B", "byte[]");
@ -46,6 +47,14 @@ public class JdbcTypeMapping {
return mapping;
}
public static JdbcTypeMapper getMapper() {
return mapper;
}
public static void setMapper(JdbcTypeMapper mapper) {
JdbcTypeMapping.mapper = mapper;
}
/**
* 当只使用 date 类型来映射数据库的所有 "时间" 类型时调用此方法
*/
@ -56,9 +65,17 @@ public class JdbcTypeMapping {
registerMapping("java.time.LocalDate", "java.util.Date");
}
static String getType(String jdbcType) {
static String getType(String jdbcType, int length) {
if (mapper != null) {
return mapper.getType(jdbcType, length);
}
String registered = mapping.get(jdbcType);
return StringUtil.isNotBlank(registered) ? registered : jdbcType;
}
public interface JdbcTypeMapper {
String getType(String jdbcType, int length);
}
}