逆向工程插件优化

This commit is contained in:
张家训
2026-06-23 20:42:06 +08:00
parent 443a5e9cd9
commit ee800011c6
2 changed files with 81 additions and 19 deletions
@@ -3,6 +3,13 @@ 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 org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.XmlElement;
import java.lang.reflect.Field;
import java.util.ArrayList;
@@ -10,15 +17,20 @@ import java.util.List;
import java.util.Properties;
/**
* 自定义插件:抑制 *Key 类的生成,将主键字段直接合并到 PO 类中。
* 自定义插件:抑制 *Key 类的生成,将主键字段合并到 PO 类中。
* <p>
* MBG 默认对含主键的表生成两个类:
* <ul>
* <li>XXXPO extends XXXPOKey(业务字段)</li>
* <li>XXXPOKey(仅主键字段)</li>
* </ul>
* 本插件通过将 primaryKeyColumns 合并到 baseColumns 中并清空 PK 列表,
* 使 MBG 只生成一个包含所有字段的 PO 类。
* 本插件实现:
* <ol>
* <li>将 primaryKeyColumns 合并到 baseColumns(PO 类含所有字段)</li>
* <li>抑制 *Key 类文件生成(返回 false)</li>
* <li>不清空 primaryKeyColumns(保持 MBG 对主键感知 → 生成 selectByPrimaryKey)</li>
* <li>重写 selectByPrimaryKey 的参数类型:*Key → *PO</li>
* </ol>
*/
public class SuppressKeyPlugin extends PluginAdapter {
@@ -35,18 +47,14 @@ public class SuppressKeyPlugin extends PluginAdapter {
return true;
}
/**
* 将主键字段合并到基础字段中,阻止 MBG 生成独立的 *Key 类。
*/
// ==================== 初始化:合并主键列到 PO ====================
@Override
public void initialized(IntrospectedTable introspectedTable) {
if (!suppressKey) return;
try {
// 通过反射获取 primaryKeyColumns 和 baseColumns
Field pkField = getField(introspectedTable.getClass(), "primaryKeyColumns");
Field baseField = getField(introspectedTable.getClass(), "baseColumns");
if (pkField == null || baseField == null) return;
pkField.setAccessible(true);
@@ -59,30 +67,82 @@ public class SuppressKeyPlugin extends PluginAdapter {
if (pkColumns == null || pkColumns.isEmpty()) return;
// 将主键列全部移到 baseColumns 开头
// PK 列合并到 baseColumns 开头(去重)
List<IntrospectedColumn> merged = new ArrayList<>(pkColumns);
if (baseColumns != null) {
merged.addAll(baseColumns);
for (IntrospectedColumn bc : baseColumns) {
boolean isPk = false;
for (IntrospectedColumn pk : pkColumns) {
if (bc.getActualColumnName().equals(pk.getActualColumnName())) {
isPk = true;
break;
}
}
if (!isPk) merged.add(bc);
}
}
baseField.set(introspectedTable, merged);
pkField.set(introspectedTable, new ArrayList<>());
} catch (Exception e) {
// 反射失败时静默忽略,回退到默认行为
// ★ 不清空 primaryKeyColumns
} catch (Exception ignored) {
}
}
// ==================== 抑制 *Key 类文件 ====================
@Override
public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
return !suppressKey;
}
// ==================== 重写 selectByPrimaryKey 参数 ====================
/**
* MBG 构造的 selectByPrimaryKey 参数是 XxxPOKey key。
* 由于 Parameter 不可变,清空参数列表后重建为 XxxPO record。
*/
@Override
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
if (!suppressKey) return true;
FullyQualifiedJavaType recordType = new FullyQualifiedJavaType(
introspectedTable.getBaseRecordType());
method.getParameters().clear();
method.getParameters().add(new Parameter(recordType, "record"));
interfaze.addImportedType(recordType);
return true;
}
/**
* MBG 构造的 XML parameterType 是 XxxPOKey,替换为 XxxPO。
* 由于 Attribute 不可变,用 List.set() 替换旧条目。
*/
@Override
public boolean sqlMapSelectByPrimaryKeyElementGenerated(XmlElement element,
IntrospectedTable introspectedTable) {
if (!suppressKey) return true;
String baseRecordType = introspectedTable.getBaseRecordType();
List<Attribute> attrs = element.getAttributes();
for (int i = 0; i < attrs.size(); i++) {
if ("parameterType".equals(attrs.get(i).getName())) {
attrs.set(i, new Attribute("parameterType", baseRecordType));
}
}
return true;
}
// =================================================================
private static Field getField(Class<?> clazz, String name) {
Field field;
try {
field = clazz.getDeclaredField(name);
return clazz.getDeclaredField(name);
} catch (NoSuchFieldException e) {
// 可能字段在父类中
if (clazz.getSuperclass() != null) {
return getField(clazz.getSuperclass(), name);
}
return null;
}
return field;
}
}
+2
View File
@@ -11,6 +11,8 @@
</plugin>
<!-- 只保留Selective版本的方法 -->
<plugin type="com.example.workutils.biz.mybatisgenerator.service.generator.SelectiveOnlyPlugin" />
<!-- TINYINT 标识字段 → Boolean(如 *_flag, shadow, is_* 等) -->
<plugin type="com.example.workutils.biz.mybatisgenerator.service.generator.TinyintToBooleanPlugin" />
<!-- 抑制 *Key 类生成,主键字段合并到 PO 类 -->
<plugin type="com.example.workutils.biz.mybatisgenerator.service.generator.SuppressKeyPlugin" />
<!-- 使用Lombok @Data注解,不生成getter/setter -->