From 6f78990815194b75a47acb634e6664efeebb2f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AE=B6=E8=AE=AD?= Date: Wed, 24 Jun 2026 13:41:50 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=86=E5=90=91=E5=B7=A5=E7=A8=8B=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/TinyintToBooleanPlugin.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/java/com/example/workutils/biz/mybatisgenerator/service/generator/TinyintToBooleanPlugin.java diff --git a/src/main/java/com/example/workutils/biz/mybatisgenerator/service/generator/TinyintToBooleanPlugin.java b/src/main/java/com/example/workutils/biz/mybatisgenerator/service/generator/TinyintToBooleanPlugin.java new file mode 100644 index 0000000..653a21a --- /dev/null +++ b/src/main/java/com/example/workutils/biz/mybatisgenerator/service/generator/TinyintToBooleanPlugin.java @@ -0,0 +1,57 @@ +package com.example.workutils.biz.mybatisgenerator.service.generator; + +import org.mybatis.generator.api.IntrospectedColumn; +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.PluginAdapter; +import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; + +import java.util.List; + +/** + * TINYINT → Boolean 类型映射插件。 + *

+ * 数据库中的 TINYINT(1) 字段在生成 Java 代码时默认映射为 Byte, + * 对于标识类字段(如 archived_flag, shadow, is_deleted 等)应映射为 Boolean。 + *

+ * 规则:列名以 {@code _flag} / {@code Flag} 结尾,或以 {@code is_} 开头, + * 或名为 {@code shadow} / {@code enable} / {@code status} 等常见布尔语义列名, + * 且 JDBC 类型为 TINYINT 时,Java 类型改为 java.lang.Boolean。 + */ +public class TinyintToBooleanPlugin extends PluginAdapter { + + @Override + public boolean validate(List warnings) { + return true; + } + + @Override + public void initialized(IntrospectedTable introspectedTable) { + for (IntrospectedColumn column : introspectedTable.getAllColumns()) { + if (shouldMapToBoolean(column)) { + column.setFullyQualifiedJavaType(new FullyQualifiedJavaType("java.lang.Boolean")); + } + } + } + + /** + * 判断列是否应映射为 Boolean。 + * 条件:JDBC 类型为 TINYINT 且列名匹配布尔语义模式。 + */ + private static boolean shouldMapToBoolean(IntrospectedColumn column) { + if (column.getJdbcType() != java.sql.Types.TINYINT) { + return false; + } + String name = column.getActualColumnName().toLowerCase(); + return name.endsWith("_flag") + || name.contains("_flag_") + || name.equals("shadow") + || name.equals("enable") + || name.equals("disabled") + || name.startsWith("is_") + || name.equals("deleted") + || name.endsWith("_status") + || name.equals("active") + || name.equals("valid") + || name.equals("locked"); + } +}