优化:不依赖表前缀判断是否为视图
This commit is contained in:
10
README.md
10
README.md
@@ -5,10 +5,9 @@
|
||||
|
||||
本插件为 MyBatis Generator 提供了以下增强功能:
|
||||
|
||||
1. **TapeMybatisGeneratorPlugin** - 扩展 MyBatis Mapper,添加 `selectPrimaryKeyByExample` 方法,支持分页查询主键列表
|
||||
1. **TapeMybatisGeneratorPlugin** - 扩展 MyBatis Mapper,添加 `selectPrimaryKeyByExample`, `batchInsert` 方法,拓展查询分页支持
|
||||
2. **TapeRepositoryGeneratorPlugin** - 为非视图表自动生成 Repository 接口和实现类,提供完整的 CRUD 和软删除功能
|
||||
3. **TapeRepoviewGeneratorPlugin** - 为视图表自动生成 RepoView 接口和实现类,提供查询功能
|
||||
4. **分页支持** - 为 Example 类自动添加分页相关字段和方法(offset、rows、usePage、limit 等)
|
||||
|
||||
## 插件说明
|
||||
|
||||
@@ -131,7 +130,6 @@
|
||||
```xml
|
||||
<context id="Mysql" targetRuntime="MyBatis3">
|
||||
<!-- 配置属性 -->
|
||||
<property name="viewKeyWords" value="VIEW_,V_"/>
|
||||
<property name="targetProject" value="src/main/java"/>
|
||||
<property name="modelPackage" value="com.iqudoo.platform.application.database.model"/>
|
||||
<property name="mapperPackage" value="com.iqudoo.platform.application.database.mapper"/>
|
||||
@@ -161,7 +159,6 @@
|
||||
|
||||
| 参数名 | 说明 | 默认值 | 必需 |
|
||||
|----------------------------|-------------------------------|-------------------------------------------------------|------|
|
||||
| `viewKeyWords` | 视图表关键字(逗号分隔,不区分大小写) | `VIEW_,V_` | 否 |
|
||||
| `targetProject` | 生成代码的目标项目路径 | `src/main/java` | 否 |
|
||||
| `modelPackage` | Model 类的包路径 | `com.iqudoo.platform.application.database.model` | 是 |
|
||||
| `mapperPackage` | Mapper 接口的包路径 | `com.iqudoo.platform.application.database.mapper` | 是 |
|
||||
@@ -177,11 +174,6 @@
|
||||
| `startPageNum` | 分页开始页码 | `1` | 否 |
|
||||
| `maxPageSize` | 最大每页数量 | `100` | 否 |
|
||||
|
||||
**视图表识别规则**:
|
||||
- 表名包含 `viewKeyWords` 中任一关键字的表将被识别为视图表,大小写不敏感
|
||||
- 视图表会生成 RepoView,不会生成 Repository
|
||||
- 非视图表会生成 Repository,不会生成 RepoView
|
||||
|
||||
## 数据库表结构要求
|
||||
|
||||
### 标准表结构模板
|
||||
|
||||
Binary file not shown.
@@ -170,6 +170,21 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||
);
|
||||
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(
|
||||
"usePage",
|
||||
JavaVisibility.PUBLIC,
|
||||
@@ -402,13 +417,13 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||
// ============================================ selectPrimaryKeyByExample ==========================================
|
||||
|
||||
@Override
|
||||
public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
|
||||
public boolean clientGenerated(Interface interfaceObj, IntrospectedTable introspectedTable) {
|
||||
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) {
|
||||
return super.clientGenerated(interfaze, introspectedTable);
|
||||
return super.clientGenerated(interfaceObj, introspectedTable);
|
||||
}
|
||||
// 获取主键列类型
|
||||
FullyQualifiedJavaType primaryType = introspectedTable.getPrimaryKeyColumns().get(0)
|
||||
@@ -424,9 +439,9 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(returnType);
|
||||
method.addParameter(new Parameter(exampleType, "example"));
|
||||
interfaze.addMethod(method);
|
||||
interfaceObj.addMethod(method);
|
||||
|
||||
return super.clientGenerated(interfaze, introspectedTable);
|
||||
return super.clientGenerated(interfaceObj, introspectedTable);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -27,7 +27,6 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
private String modelPackage = "com.iqudoo.platform.application.database.model";
|
||||
private String mapperPackage = "com.iqudoo.platform.application.database.mapper";
|
||||
private String targetProject = "src/main/java";
|
||||
private String viewKeyWords = "VIEW_,V_";
|
||||
|
||||
// 1.4.1版本专用:Java格式化器
|
||||
private JavaFormatter javaFormatter;
|
||||
@@ -59,10 +58,6 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
modelPackage = stringConfig("modelPackage", modelPackage);
|
||||
mapperPackage = stringConfig("mapperPackage", mapperPackage);
|
||||
targetProject = stringConfig("targetProject", targetProject);
|
||||
viewKeyWords = stringConfig("viewKeyWords", viewKeyWords);
|
||||
if (StringUtility.stringHasValue(viewKeyWords)) {
|
||||
viewKeyWords = viewKeyWords.toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
private String stringConfig(String key, String defaultValue) {
|
||||
@@ -87,8 +82,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
List<GeneratedJavaFile> generatedJavaFiles = new ArrayList<>();
|
||||
|
||||
// 视图表过滤逻辑
|
||||
String tableName = introspectedTable.getFullyQualifiedTable().getIntrospectedTableName().toUpperCase();
|
||||
if (isViewTable(tableName)) {
|
||||
if (ElementTools.isViewTable(introspectedTable)) {
|
||||
return generatedJavaFiles;
|
||||
}
|
||||
|
||||
@@ -135,18 +129,6 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||
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接口(手动添加所有方法,无继承,匹配指定格式)
|
||||
*/
|
||||
|
||||
@@ -27,7 +27,6 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
private String modelPackage = "com.iqudoo.platform.application.database.model";
|
||||
private String mapperPackage = "com.iqudoo.platform.application.database.mapper";
|
||||
private String targetProject = "src/main/java";
|
||||
private String viewKeyWords = "VIEW_,V_";
|
||||
|
||||
// 1.4.1版本专用格式化器
|
||||
private JavaFormatter javaFormatter;
|
||||
@@ -56,10 +55,6 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
modelPackage = stringConfig("modelPackage", modelPackage);
|
||||
mapperPackage = stringConfig("mapperPackage", mapperPackage);
|
||||
targetProject = stringConfig("targetProject", targetProject);
|
||||
viewKeyWords = stringConfig("viewKeyWords", viewKeyWords);
|
||||
if (StringUtility.stringHasValue(viewKeyWords)) {
|
||||
viewKeyWords = viewKeyWords.toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
private String stringConfig(String key, String defaultValue) {
|
||||
@@ -82,13 +77,10 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
@Override
|
||||
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
|
||||
List<GeneratedJavaFile> generatedJavaFiles = new ArrayList<>();
|
||||
|
||||
// 仅处理视图表,非视图表直接返回
|
||||
String tableName = introspectedTable.getFullyQualifiedTable().getIntrospectedTableName().toUpperCase();
|
||||
if (!isViewTable(tableName)) {
|
||||
if (!ElementTools.isViewTable(introspectedTable)) {
|
||||
return generatedJavaFiles;
|
||||
}
|
||||
|
||||
// 1. 获取视图表元数据
|
||||
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
|
||||
String exampleClassName = domainObjectName + "Example";
|
||||
@@ -132,22 +124,6 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||
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接口(移除继承,手动添加指定方法)
|
||||
*/
|
||||
|
||||
@@ -9,6 +9,17 @@ import static org.mybatis.generator.internal.util.messages.Messages.getString;
|
||||
@SuppressWarnings("unused")
|
||||
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列数据
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user