From 2d60efe747cc41932020c7420c4af25f5eb460d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 8 Aug 2023 09:51:01 +0800 Subject: [PATCH] feat: add JdbcTypeMapper to customize bean property type --- .../codegen/dialect/JdbcDialect.java | 3 ++- .../codegen/dialect/JdbcTypeMapping.java | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java index 3d7cd984..112ed488 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java @@ -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)); diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcTypeMapping.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcTypeMapping.java index 3f8f3e50..18590c67 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcTypeMapping.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcTypeMapping.java @@ -29,6 +29,7 @@ public class JdbcTypeMapping { } private static final Map 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); + } + }