Skip to content

Commit c7ca5de

Browse files
authored
Merge pull request #951 from jeffgbutler/new-plugin-method
Add ability to provide a fine grained filter for tables in a context
2 parents f78bb0a + a4b0f84 commit c7ca5de

File tree

10 files changed

+132
-68
lines changed

10 files changed

+132
-68
lines changed

core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/CompositePlugin.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1264,4 +1264,15 @@ public boolean clientUpdateByPrimaryKeyMethodGenerated(KotlinFunction kotlinFunc
12641264

12651265
return true;
12661266
}
1267+
1268+
@Override
1269+
public boolean shouldGenerate(IntrospectedTable introspectedTable) {
1270+
for (Plugin plugin : plugins) {
1271+
if (!plugin.shouldGenerate(introspectedTable)) {
1272+
return false;
1273+
}
1274+
}
1275+
1276+
return true;
1277+
}
12671278
}

core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/Plugin.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -2034,4 +2034,14 @@ default boolean clientUpdateByPrimaryKeyMethodGenerated(KotlinFunction kotlinFun
20342034
IntrospectedTable introspectedTable) {
20352035
return true;
20362036
}
2037+
2038+
/**
2039+
* If false, the table will be skipped in code generation.
2040+
*
2041+
* @param introspectedTable the current table
2042+
* @return true if code should be generated for this table, else false
2043+
*/
2044+
default boolean shouldGenerate(IntrospectedTable introspectedTable) {
2045+
return true;
2046+
}
20372047
}

core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -459,6 +459,11 @@ public void generateFiles(ProgressCallback callback,
459459

460460
for (IntrospectedTable introspectedTable : introspectedTables) {
461461
callback.checkCancel();
462+
463+
if (!pluginAggregator.shouldGenerate(introspectedTable)) {
464+
continue;
465+
}
466+
462467
generatedJavaFiles.addAll(introspectedTable
463468
.getGeneratedJavaFiles());
464469
generatedXmlFiles.addAll(introspectedTable
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2006-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.generator.plugins;
17+
18+
import org.mybatis.generator.api.IntrospectedTable;
19+
import org.mybatis.generator.api.PluginAdapter;
20+
21+
import java.util.List;
22+
23+
/**
24+
* This plugin will cause any table of type "VIEW" in a context to be ignored.
25+
*/
26+
public class IgnoreViewsPlugin extends PluginAdapter {
27+
@Override
28+
public boolean validate(List<String> warnings) {
29+
return true;
30+
}
31+
32+
@Override
33+
public boolean shouldGenerate(IntrospectedTable introspectedTable) {
34+
return !"VIEW".equalsIgnoreCase(introspectedTable.getTableType()); //$NON-NLS-1$
35+
}
36+
}

core/mybatis-generator-core/src/site/xhtml/reference/plugins.xhtml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2006-2022 the original author or authors.
4+
Copyright 2006-2023 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -119,6 +119,11 @@ The plugin will create the additional Methods:
119119
<p>Using this plugin, you can configure the property values fluently with chained method calls. Example: <code>new MyDomain().withFoo("Test").withBar(4711);</code></p>
120120

121121

122+
<h2>org.mybatis.generator.plugins.IgnoreViewsPlugin</h2>
123+
<p>This plugin will filter out any table of type "VIEW" from a code generation run. This can be useful if you use
124+
a wildcard to select many tables and views, but don't want to generate code for the views.
125+
</p>
126+
122127
<h2>org.mybatis.generator.plugins.MapperAnnotationPlugin</h2>
123128
<p>This plugin has no impact and is not needed when the target runtime in use is based on MyBatis Dynamic SQL.</p>
124129
<p>This plugin adds the <code>@Mapper</code> annotation to generated mapper interfaces. This

core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
Logging should now work as expected in Maven - Maven ships with the SLF4J simple logger in the classpath, so
4141
configuring logging in Maven will always use the SLF simple logger.
4242
</li>
43+
<li>Added a new plugin method "shouldGenerate" that can be used to provide fine-grained filtering of tables in a
44+
context. Also added a new "IgnoreViewsPlugin" that filters out table type VIEW
45+
</li>
4346
</ul>
4447

4548

core/mybatis-generator-core/src/test/resources/scripts/generatorConfig.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2006-2022 the original author or authors.
4+
Copyright 2006-2023 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -906,4 +906,17 @@
906906
<columnOverride column="class" property="dbClass" />
907907
</table>
908908
</context>
909+
910+
<context id="nothing-generated" targetRuntime="MyBatis3Simple">
911+
<plugin type="org.mybatis.generator.plugins.IgnoreViewsPlugin" />
912+
913+
<jdbcConnection driverClass="org.hsqldb.jdbcDriver" connectionURL="${database.url}" userId="sa" />
914+
915+
<javaModelGenerator targetPackage="mbg.test.mb3.generated.simpleannotated.nothing" targetProject="MAVEN"/>
916+
917+
<javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="mbg.test.mb3.generated.simpleannotated.nothing" targetProject="MAVEN"/>
918+
919+
<table tableName="NameView" />
920+
<table tableName="PKFields" />
921+
</context>
909922
</generatorConfiguration>

core/mybatis-generator-systests-mybatis3/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2006-2022 the original author or authors.
4+
Copyright 2006-2023 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -110,6 +110,10 @@
110110
<artifactId>hsqldb</artifactId>
111111
<scope>test</scope>
112112
</dependency>
113+
<dependency>
114+
<groupId>org.assertj</groupId>
115+
<artifactId>assertj-core</artifactId>
116+
</dependency>
113117
</dependencies>
114118
<description>Tests for the MyBatis3 code generator.</description>
115119
<profiles>

core/mybatis-generator-systests-mybatis3/src/main/resources/generatorConfig.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2006-2022 the original author or authors.
4+
Copyright 2006-2023 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -906,4 +906,17 @@
906906
<columnOverride column="class" property="dbClass" />
907907
</table>
908908
</context>
909+
910+
<context id="nothing-generated" targetRuntime="MyBatis3Simple">
911+
<plugin type="org.mybatis.generator.plugins.IgnoreViewsPlugin" />
912+
913+
<jdbcConnection driverClass="org.hsqldb.jdbcDriver" connectionURL="${database.url}" userId="sa" />
914+
915+
<javaModelGenerator targetPackage="mbg.test.mb3.generated.simpleannotated.nothing" targetProject="MAVEN"/>
916+
917+
<javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="mbg.test.mb3.generated.simpleannotated.nothing" targetProject="MAVEN"/>
918+
919+
<table tableName="NameView" />
920+
<table tableName="PKFields" />
921+
</context>
909922
</generatorConfiguration>

0 commit comments

Comments
 (0)