逆向工程插件优化
This commit is contained in:
+57
@@ -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 类型映射插件。
|
||||
* <p>
|
||||
* 数据库中的 TINYINT(1) 字段在生成 Java 代码时默认映射为 Byte,
|
||||
* 对于标识类字段(如 archived_flag, shadow, is_deleted 等)应映射为 Boolean。
|
||||
* <p>
|
||||
* 规则:列名以 {@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<String> 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user