优化:不依赖表前缀判断是否为视图

This commit is contained in:
iqudoo
2026-04-13 02:06:41 +08:00
parent 885f823203
commit 0bea5abfb6
6 changed files with 35 additions and 59 deletions

View File

@@ -5,10 +5,9 @@
本插件为 MyBatis Generator 提供了以下增强功能: 本插件为 MyBatis Generator 提供了以下增强功能:
1. **TapeMybatisGeneratorPlugin** - 扩展 MyBatis Mapper添加 `selectPrimaryKeyByExample` 方法,支持分页查询主键列表 1. **TapeMybatisGeneratorPlugin** - 扩展 MyBatis Mapper添加 `selectPrimaryKeyByExample`, `batchInsert` 方法,拓展查询分页支持
2. **TapeRepositoryGeneratorPlugin** - 为非视图表自动生成 Repository 接口和实现类,提供完整的 CRUD 和软删除功能 2. **TapeRepositoryGeneratorPlugin** - 为非视图表自动生成 Repository 接口和实现类,提供完整的 CRUD 和软删除功能
3. **TapeRepoviewGeneratorPlugin** - 为视图表自动生成 RepoView 接口和实现类,提供查询功能 3. **TapeRepoviewGeneratorPlugin** - 为视图表自动生成 RepoView 接口和实现类,提供查询功能
4. **分页支持** - 为 Example 类自动添加分页相关字段和方法offset、rows、usePage、limit 等)
## 插件说明 ## 插件说明
@@ -131,7 +130,6 @@
```xml ```xml
<context id="Mysql" targetRuntime="MyBatis3"> <context id="Mysql" targetRuntime="MyBatis3">
<!-- 配置属性 --> <!-- 配置属性 -->
<property name="viewKeyWords" value="VIEW_,V_"/>
<property name="targetProject" value="src/main/java"/> <property name="targetProject" value="src/main/java"/>
<property name="modelPackage" value="com.iqudoo.platform.application.database.model"/> <property name="modelPackage" value="com.iqudoo.platform.application.database.model"/>
<property name="mapperPackage" value="com.iqudoo.platform.application.database.mapper"/> <property name="mapperPackage" value="com.iqudoo.platform.application.database.mapper"/>
@@ -161,7 +159,6 @@
| 参数名 | 说明 | 默认值 | 必需 | | 参数名 | 说明 | 默认值 | 必需 |
|----------------------------|-------------------------------|-------------------------------------------------------|------| |----------------------------|-------------------------------|-------------------------------------------------------|------|
| `viewKeyWords` | 视图表关键字(逗号分隔,不区分大小写) | `VIEW_,V_` | 否 |
| `targetProject` | 生成代码的目标项目路径 | `src/main/java` | 否 | | `targetProject` | 生成代码的目标项目路径 | `src/main/java` | 否 |
| `modelPackage` | Model 类的包路径 | `com.iqudoo.platform.application.database.model` | 是 | | `modelPackage` | Model 类的包路径 | `com.iqudoo.platform.application.database.model` | 是 |
| `mapperPackage` | Mapper 接口的包路径 | `com.iqudoo.platform.application.database.mapper` | 是 | | `mapperPackage` | Mapper 接口的包路径 | `com.iqudoo.platform.application.database.mapper` | 是 |
@@ -177,11 +174,6 @@
| `startPageNum` | 分页开始页码 | `1` | 否 | | `startPageNum` | 分页开始页码 | `1` | 否 |
| `maxPageSize` | 最大每页数量 | `100` | 否 | | `maxPageSize` | 最大每页数量 | `100` | 否 |
**视图表识别规则**
- 表名包含 `viewKeyWords` 中任一关键字的表将被识别为视图表,大小写不敏感
- 视图表会生成 RepoView不会生成 Repository
- 非视图表会生成 Repository不会生成 RepoView
## 数据库表结构要求 ## 数据库表结构要求
### 标准表结构模板 ### 标准表结构模板

View File

@@ -170,6 +170,21 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
); );
FormatTools.addMethodWithBestPosition(topLevelClass, setLimitByOffsetRows); FormatTools.addMethodWithBestPosition(topLevelClass, setLimitByOffsetRows);
Method setLimitByOffsetOnly = ElementTools.generateMethod(
"limitOffset",
JavaVisibility.PUBLIC,
topLevelClass.getType(),
new Parameter(integerType, "offset"),
new Parameter(integerType, "rows")
);
setLimitByOffsetOnly = ElementTools.generateMethodBody(
setLimitByOffsetOnly,
"this.offset = offset;",
"this.rows = 999999999;",
"return this;"
);
FormatTools.addMethodWithBestPosition(topLevelClass, setLimitByOffsetOnly);
Method usePage = ElementTools.generateMethod( Method usePage = ElementTools.generateMethod(
"usePage", "usePage",
JavaVisibility.PUBLIC, JavaVisibility.PUBLIC,
@@ -402,13 +417,13 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
// ============================================ selectPrimaryKeyByExample ========================================== // ============================================ selectPrimaryKeyByExample ==========================================
@Override @Override
public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) { public boolean clientGenerated(Interface interfaceObj, IntrospectedTable introspectedTable) {
if (introspectedTable.getTargetRuntime() != IntrospectedTable.TargetRuntime.MYBATIS3) { if (introspectedTable.getTargetRuntime() != IntrospectedTable.TargetRuntime.MYBATIS3) {
return super.clientGenerated(interfaze, introspectedTable); return super.clientGenerated(interfaceObj, introspectedTable);
} }
addBatchInsertClientMethod(interfaze, introspectedTable); addBatchInsertClientMethod(interfaceObj, introspectedTable);
if (introspectedTable.getPrimaryKeyColumns().size() <= 0) { if (introspectedTable.getPrimaryKeyColumns().size() <= 0) {
return super.clientGenerated(interfaze, introspectedTable); return super.clientGenerated(interfaceObj, introspectedTable);
} }
// 获取主键列类型 // 获取主键列类型
FullyQualifiedJavaType primaryType = introspectedTable.getPrimaryKeyColumns().get(0) FullyQualifiedJavaType primaryType = introspectedTable.getPrimaryKeyColumns().get(0)
@@ -424,9 +439,9 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
method.setVisibility(JavaVisibility.PUBLIC); method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(returnType); method.setReturnType(returnType);
method.addParameter(new Parameter(exampleType, "example")); method.addParameter(new Parameter(exampleType, "example"));
interfaze.addMethod(method); interfaceObj.addMethod(method);
return super.clientGenerated(interfaze, introspectedTable); return super.clientGenerated(interfaceObj, introspectedTable);
} }

View File

@@ -27,7 +27,6 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
private String modelPackage = "com.iqudoo.platform.application.database.model"; private String modelPackage = "com.iqudoo.platform.application.database.model";
private String mapperPackage = "com.iqudoo.platform.application.database.mapper"; private String mapperPackage = "com.iqudoo.platform.application.database.mapper";
private String targetProject = "src/main/java"; private String targetProject = "src/main/java";
private String viewKeyWords = "VIEW_,V_";
// 1.4.1版本专用Java格式化器 // 1.4.1版本专用Java格式化器
private JavaFormatter javaFormatter; private JavaFormatter javaFormatter;
@@ -59,10 +58,6 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
modelPackage = stringConfig("modelPackage", modelPackage); modelPackage = stringConfig("modelPackage", modelPackage);
mapperPackage = stringConfig("mapperPackage", mapperPackage); mapperPackage = stringConfig("mapperPackage", mapperPackage);
targetProject = stringConfig("targetProject", targetProject); targetProject = stringConfig("targetProject", targetProject);
viewKeyWords = stringConfig("viewKeyWords", viewKeyWords);
if (StringUtility.stringHasValue(viewKeyWords)) {
viewKeyWords = viewKeyWords.toUpperCase();
}
} }
private String stringConfig(String key, String defaultValue) { private String stringConfig(String key, String defaultValue) {
@@ -87,8 +82,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
List<GeneratedJavaFile> generatedJavaFiles = new ArrayList<>(); List<GeneratedJavaFile> generatedJavaFiles = new ArrayList<>();
// 视图表过滤逻辑 // 视图表过滤逻辑
String tableName = introspectedTable.getFullyQualifiedTable().getIntrospectedTableName().toUpperCase(); if (ElementTools.isViewTable(introspectedTable)) {
if (isViewTable(tableName)) {
return generatedJavaFiles; return generatedJavaFiles;
} }
@@ -135,18 +129,6 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
return generatedJavaFiles; return generatedJavaFiles;
} }
private boolean isViewTable(String tableName) {
if (StringUtility.stringHasValue(viewKeyWords)) {
String[] keywords = viewKeyWords.split(",");
for (String keyword : keywords) {
if (tableName.contains(keyword.trim().toUpperCase())) {
return true;
}
}
}
return false;
}
/** /**
* 核心修改生成Repository接口手动添加所有方法无继承匹配指定格式 * 核心修改生成Repository接口手动添加所有方法无继承匹配指定格式
*/ */

View File

@@ -27,7 +27,6 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
private String modelPackage = "com.iqudoo.platform.application.database.model"; private String modelPackage = "com.iqudoo.platform.application.database.model";
private String mapperPackage = "com.iqudoo.platform.application.database.mapper"; private String mapperPackage = "com.iqudoo.platform.application.database.mapper";
private String targetProject = "src/main/java"; private String targetProject = "src/main/java";
private String viewKeyWords = "VIEW_,V_";
// 1.4.1版本专用格式化器 // 1.4.1版本专用格式化器
private JavaFormatter javaFormatter; private JavaFormatter javaFormatter;
@@ -56,10 +55,6 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
modelPackage = stringConfig("modelPackage", modelPackage); modelPackage = stringConfig("modelPackage", modelPackage);
mapperPackage = stringConfig("mapperPackage", mapperPackage); mapperPackage = stringConfig("mapperPackage", mapperPackage);
targetProject = stringConfig("targetProject", targetProject); targetProject = stringConfig("targetProject", targetProject);
viewKeyWords = stringConfig("viewKeyWords", viewKeyWords);
if (StringUtility.stringHasValue(viewKeyWords)) {
viewKeyWords = viewKeyWords.toUpperCase();
}
} }
private String stringConfig(String key, String defaultValue) { private String stringConfig(String key, String defaultValue) {
@@ -82,13 +77,10 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
@Override @Override
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) { public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
List<GeneratedJavaFile> generatedJavaFiles = new ArrayList<>(); List<GeneratedJavaFile> generatedJavaFiles = new ArrayList<>();
// 仅处理视图表,非视图表直接返回 // 仅处理视图表,非视图表直接返回
String tableName = introspectedTable.getFullyQualifiedTable().getIntrospectedTableName().toUpperCase(); if (!ElementTools.isViewTable(introspectedTable)) {
if (!isViewTable(tableName)) {
return generatedJavaFiles; return generatedJavaFiles;
} }
// 1. 获取视图表元数据 // 1. 获取视图表元数据
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName(); String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
String exampleClassName = domainObjectName + "Example"; String exampleClassName = domainObjectName + "Example";
@@ -132,22 +124,6 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
return generatedJavaFiles; return generatedJavaFiles;
} }
/**
* 判断是否为视图表(关键字匹配 + MBG原生视图配置
*/
private boolean isViewTable(String tableName) {
// 关键字匹配
if (StringUtility.stringHasValue(viewKeyWords)) {
String[] keywords = viewKeyWords.split(",");
for (String keyword : keywords) {
if (tableName.contains(keyword.trim().toUpperCase())) {
return true;
}
}
}
return false;
}
/** /**
* 核心修改生成视图Repo接口移除继承手动添加指定方法 * 核心修改生成视图Repo接口移除继承手动添加指定方法
*/ */

View File

@@ -9,6 +9,17 @@ import static org.mybatis.generator.internal.util.messages.Messages.getString;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class ElementTools { public class ElementTools {
/**
* 判断IntrospectedTable是否代表数据库视图
*
* @param introspectedTable MBG内省表对象
* @return true=视图false=普通表/其他类型
*/
public static boolean isViewTable(IntrospectedTable introspectedTable) {
String tableType = introspectedTable.getTableType();
return "VIEW".equals(tableType);
}
/** /**
* 检测数据表是否含有BLOB列数据 * 检测数据表是否含有BLOB列数据
* *