From 18489c90075bf6798a517fa43f810c405699afbe Mon Sep 17 00:00:00 2001
From: Jeff Butler
Date: Mon, 29 Nov 2021 09:27:30 -0500
Subject: [PATCH] Remove separate extensions file for Kotlin
---
.../generator/api/CompositePlugin.java | 18 +---
.../org/mybatis/generator/api/Plugin.java | 32 +++++++-
...KotlinDynamicSqlSupportClassGenerator.java | 4 +
.../KotlinMapperAndExtensionsGenerator.java | 82 +++++++------------
.../src/site/xhtml/whatsNew.xhtml | 16 +++-
5 files changed, 82 insertions(+), 70 deletions(-)
diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/CompositePlugin.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/CompositePlugin.java
index 76a5a665f5..b2ef971f88 100644
--- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/CompositePlugin.java
+++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/CompositePlugin.java
@@ -1178,21 +1178,11 @@ public boolean dynamicSqlSupportGenerated(TopLevelClass supportClass, Introspect
}
@Override
- public boolean dynamicSqlSupportGenerated(KotlinFile kotlinFile, KotlinType supportClass,
- IntrospectedTable introspectedTable) {
+ public boolean dynamicSqlSupportGenerated(KotlinFile kotlinFile, KotlinType outerSupportObject,
+ KotlinType innerSupportClass, IntrospectedTable introspectedTable) {
for (Plugin plugin : plugins) {
- if (!plugin.dynamicSqlSupportGenerated(kotlinFile, supportClass, introspectedTable)) {
- return false;
- }
- }
-
- return true;
- }
-
- @Override
- public boolean mapperExtensionsGenerated(KotlinFile extensionsFile, IntrospectedTable introspectedTable) {
- for (Plugin plugin : plugins) {
- if (!plugin.mapperExtensionsGenerated(extensionsFile, introspectedTable)) {
+ if (!plugin.dynamicSqlSupportGenerated(kotlinFile, outerSupportObject, innerSupportClass,
+ introspectedTable)) {
return false;
}
}
diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/Plugin.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/Plugin.java
index 5789563832..1997861507 100644
--- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/Plugin.java
+++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/Plugin.java
@@ -1969,11 +1969,39 @@ default boolean dynamicSqlSupportGenerated(TopLevelClass supportClass, Introspec
return true;
}
- default boolean dynamicSqlSupportGenerated(KotlinFile kotlinFile, KotlinType supportClass,
- IntrospectedTable introspectedTable) {
+ /**
+ * This method is called when the MyBatis Dynamic SQL support object has been generated. The format of the class
+ * is an outer object with an inner class. The inner class contains the table and column definitions. The outer
+ * (singleton) object contains a reference to an instance of the inner class, and shortcut properties that
+ * reference the columns of that instance.
+ *
+ * @param kotlinFile the generated Kotlin file containing the outer object and inner class
+ * @param outerSupportObject a reference to the outer object in the file
+ * @param innerSupportClass a reference to the inner class
+ * @param introspectedTable the class containing information about the table as
+ * introspected from the database
+ * @return true if the generated file should be kept
+ */
+ default boolean dynamicSqlSupportGenerated(KotlinFile kotlinFile, KotlinType outerSupportObject,
+ KotlinType innerSupportClass, IntrospectedTable introspectedTable) {
return true;
}
+ /**
+ * This method is no longer called.
+ *
+ * @deprecated this method is no longer called
+ *
+ * @param extensionsFile
+ * the partially generated file
+ * @param introspectedTable
+ * The class containing information about the table as introspected from the database
+ * @return true if the file should be generated, false if the generated
+ * file should be ignored. In the case of multiple plugins, the
+ * first plugin returning false will disable the calling of further
+ * plugins.
+ */
+ @Deprecated
default boolean mapperExtensionsGenerated(KotlinFile extensionsFile, IntrospectedTable introspectedTable) {
return true;
}
diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/KotlinDynamicSqlSupportClassGenerator.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/KotlinDynamicSqlSupportClassGenerator.java
index 58b9ea363b..f875efaf95 100644
--- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/KotlinDynamicSqlSupportClassGenerator.java
+++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/KotlinDynamicSqlSupportClassGenerator.java
@@ -84,6 +84,10 @@ public KotlinType getInnerClass() {
return innerClass;
}
+ public KotlinType getOuterObject() {
+ return outerObject;
+ }
+
public String getTablePropertyImport() {
return getSupportObjectImport()
+ "." //$NON-NLS-1$
diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/KotlinMapperAndExtensionsGenerator.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/KotlinMapperAndExtensionsGenerator.java
index c8a011dc2b..e4837476b8 100644
--- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/KotlinMapperAndExtensionsGenerator.java
+++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/kotlin/KotlinMapperAndExtensionsGenerator.java
@@ -92,17 +92,6 @@ protected KotlinFile createMapperInterfaceFile() {
return kf;
}
- protected KotlinFile createMapperExtensionsFile() {
- FullyQualifiedKotlinType type = new FullyQualifiedKotlinType(
- introspectedTable.getMyBatis3JavaMapperType());
-
- KotlinFile kf = new KotlinFile(type.getShortNameWithoutTypeArguments() + "Extensions"); //$NON-NLS-1$
- kf.setPackage(type.getPackageName());
- context.getCommentGenerator().addFileComment(kf);
-
- return kf;
- }
-
protected KotlinType createMapperInterface(KotlinFile kotlinFile) {
FullyQualifiedKotlinType type = new FullyQualifiedKotlinType(
introspectedTable.getMyBatis3JavaMapperType());
@@ -182,30 +171,31 @@ public List getKotlinFiles() {
boolean reuseResultMap = addBasicSelectManyMethod(mapperFile, mapper);
addBasicSelectOneMethod(mapperFile, mapper, reuseResultMap);
- KotlinFile extensionsFile = createMapperExtensionsFile();
String mapperName = mapper.getName();
- addGeneralCountMethod(mapperFile, mapper, extensionsFile, mapperName);
- addGeneralDeleteMethod(mapperFile, mapper, extensionsFile, mapperName);
- addDeleteByPrimaryKeyMethod(extensionsFile, mapperName);
- addInsertOneMethod(mapperFile, mapper, extensionsFile, mapperName);
- addInsertMultipleMethod(mapperFile, mapper, extensionsFile, mapperName);
- addInsertMultipleVarargMethod(extensionsFile, mapperName);
- addInsertSelectiveMethod(mapperFile, mapper, extensionsFile, mapperName);
- addColumnListProperty(extensionsFile);
- addGeneralSelectMethod(extensionsFile, mapperName);
- addSelectDistinctMethod(extensionsFile, mapperName);
- addSelectByPrimaryKeyMethod(extensionsFile, mapperName);
- addGeneralUpdateMethod(mapperFile, mapper, extensionsFile, mapperName);
- addUpdateAllMethod(extensionsFile);
- addUpdateSelectiveMethod(extensionsFile);
- addUpdateByPrimaryKeyMethod(extensionsFile, mapperName);
- addUpdateByPrimaryKeySelectiveMethod(extensionsFile, mapperName);
+ addGeneralCountMethod(mapperFile, mapper, mapperName);
+ addGeneralDeleteMethod(mapperFile, mapper, mapperName);
+ addDeleteByPrimaryKeyMethod(mapperFile, mapperName);
+ addInsertOneMethod(mapperFile, mapper, mapperName);
+ addInsertMultipleMethod(mapperFile, mapper, mapperName);
+ addInsertMultipleVarargMethod(mapperFile, mapperName);
+ addInsertSelectiveMethod(mapperFile, mapper, mapperName);
+ addColumnListProperty(mapperFile);
+ addGeneralSelectMethod(mapperFile, mapperName);
+ addSelectDistinctMethod(mapperFile, mapperName);
+ addSelectByPrimaryKeyMethod(mapperFile, mapperName);
+ addGeneralUpdateMethod(mapperFile, mapper, mapperName);
+ addUpdateAllMethod(mapperFile);
+ addUpdateSelectiveMethod(mapperFile);
+ addUpdateByPrimaryKeyMethod(mapperFile, mapperName);
+ addUpdateByPrimaryKeySelectiveMethod(mapperFile, mapperName);
KotlinFile supportFile = supportClassGenerator.getKotlinFile();
List answer = new ArrayList<>();
- if (context.getPlugins().dynamicSqlSupportGenerated(supportFile, supportClassGenerator.getInnerClass(),
+ if (context.getPlugins().dynamicSqlSupportGenerated(supportFile,
+ supportClassGenerator.getOuterObject(),
+ supportClassGenerator.getInnerClass(),
introspectedTable)) {
answer.add(supportFile);
}
@@ -214,15 +204,10 @@ public List getKotlinFiles() {
answer.add(mapperFile);
}
- if (context.getPlugins().mapperExtensionsGenerated(extensionsFile, introspectedTable)) {
- answer.add(extensionsFile);
- }
-
return answer;
}
- protected void addInsertOneMethod(KotlinFile mapperFile, KotlinType mapper, KotlinFile extensionsFile,
- String mapperName) {
+ protected void addInsertOneMethod(KotlinFile mapperFile, KotlinType mapper, String mapperName) {
InsertMethodGenerator generator = new InsertMethodGenerator.Builder()
.withContext(context)
.withIntrospectedTable(introspectedTable)
@@ -232,7 +217,7 @@ protected void addInsertOneMethod(KotlinFile mapperFile, KotlinType mapper, Kotl
.withSupportObjectImport(supportClassGenerator.getSupportObjectImport())
.build();
- if (generate(extensionsFile, generator) && !hasGeneratedKeys) {
+ if (generate(mapperFile, generator) && !hasGeneratedKeys) {
addCommonInsertInterface(mapperFile, mapper);
}
}
@@ -255,8 +240,7 @@ protected void addBasicInsertMultipleMethod(KotlinFile kotlinFile, KotlinType ko
generate(kotlinFile, kotlinType, generator);
}
- protected void addInsertMultipleMethod(KotlinFile mapperFile, KotlinType mapper, KotlinFile extensionsFile,
- String mapperName) {
+ protected void addInsertMultipleMethod(KotlinFile mapperFile, KotlinType mapper, String mapperName) {
InsertMultipleMethodGenerator generator = new InsertMultipleMethodGenerator.Builder()
.withContext(context)
.withIntrospectedTable(introspectedTable)
@@ -266,7 +250,7 @@ protected void addInsertMultipleMethod(KotlinFile mapperFile, KotlinType mapper,
.withSupportObjectImport(supportClassGenerator.getSupportObjectImport())
.build();
- if (generate(extensionsFile, generator) && !hasGeneratedKeys) {
+ if (generate(mapperFile, generator) && !hasGeneratedKeys) {
addCommonInsertInterface(mapperFile, mapper);
}
}
@@ -283,8 +267,7 @@ protected void addInsertMultipleVarargMethod(KotlinFile kotlinFile, String mappe
generate(kotlinFile, generator);
}
- protected void addGeneralCountMethod(KotlinFile mapperFile, KotlinType mapper, KotlinFile extensionsFile,
- String mapperName) {
+ protected void addGeneralCountMethod(KotlinFile mapperFile, KotlinType mapper, String mapperName) {
GeneralCountMethodGenerator generator = new GeneralCountMethodGenerator.Builder()
.withContext(context)
.withIntrospectedTable(introspectedTable)
@@ -293,15 +276,14 @@ protected void addGeneralCountMethod(KotlinFile mapperFile, KotlinType mapper, K
.withMapperName(mapperName)
.build();
- if (generate(extensionsFile, generator)) {
+ if (generate(mapperFile, generator)) {
// add common interface
mapper.addSuperType("CommonCountMapper"); //$NON-NLS-1$
mapperFile.addImport("org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper"); //$NON-NLS-1$
}
}
- protected void addGeneralDeleteMethod(KotlinFile mapperFile, KotlinType mapper, KotlinFile extensionsFile,
- String mapperName) {
+ protected void addGeneralDeleteMethod(KotlinFile mapperFile, KotlinType mapper, String mapperName) {
GeneralDeleteMethodGenerator generator = new GeneralDeleteMethodGenerator.Builder()
.withContext(context)
.withIntrospectedTable(introspectedTable)
@@ -309,7 +291,7 @@ protected void addGeneralDeleteMethod(KotlinFile mapperFile, KotlinType mapper,
.withMapperName(mapperName)
.build();
- if (generate(extensionsFile, generator)) {
+ if (generate(mapperFile, generator)) {
// add common interface
mapper.addSuperType("CommonDeleteMapper"); //$NON-NLS-1$
mapperFile.addImport("org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper"); //$NON-NLS-1$
@@ -366,8 +348,7 @@ protected void addGeneralSelectOneMethod(KotlinFile kotlinFile, String mapperNam
generate(kotlinFile, generator);
}
- protected void addGeneralUpdateMethod(KotlinFile mapperFile, KotlinType mapper, KotlinFile extensionsFile,
- String mapperName) {
+ protected void addGeneralUpdateMethod(KotlinFile mapperFile, KotlinType mapper, String mapperName) {
GeneralUpdateMethodGenerator generator = new GeneralUpdateMethodGenerator.Builder()
.withContext(context)
.withIntrospectedTable(introspectedTable)
@@ -375,7 +356,7 @@ protected void addGeneralUpdateMethod(KotlinFile mapperFile, KotlinType mapper,
.withMapperName(mapperName)
.build();
- if (generate(extensionsFile, generator)) {
+ if (generate(mapperFile, generator)) {
// add common interface
mapper.addSuperType("CommonUpdateMapper"); //$NON-NLS-1$
mapperFile.addImport("org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper"); //$NON-NLS-1$
@@ -432,8 +413,7 @@ protected void addDeleteByPrimaryKeyMethod(KotlinFile kotlinFile, String mapperN
generate(kotlinFile, generator);
}
- protected void addInsertSelectiveMethod(KotlinFile mapperFile, KotlinType mapper, KotlinFile extensionsFile,
- String mapperName) {
+ protected void addInsertSelectiveMethod(KotlinFile mapperFile, KotlinType mapper, String mapperName) {
InsertSelectiveMethodGenerator generator = new InsertSelectiveMethodGenerator.Builder()
.withContext(context)
.withIntrospectedTable(introspectedTable)
@@ -443,7 +423,7 @@ protected void addInsertSelectiveMethod(KotlinFile mapperFile, KotlinType mapper
.withSupportObjectImport(supportClassGenerator.getSupportObjectImport())
.build();
- if (generate(extensionsFile, generator) && !hasGeneratedKeys) {
+ if (generate(mapperFile, generator) && !hasGeneratedKeys) {
addCommonInsertInterface(mapperFile, mapper);
}
}
diff --git a/core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml b/core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml
index e31ced1c42..8d1b951c04 100644
--- a/core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml
+++ b/core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml
@@ -37,7 +37,17 @@ See the GitHub page for milestone 1.4.1 for details other other changes in this
version 1.3.0 or later of MyBatis Dynamic SQL. This should be a relatively minor change for most Java users
and should simply require updating the MyBatis Dynamic SQL version to 1.3.0 or later.
-Differences for Kotlin users are more extensive. The generated Dynamic SQL Support class in the Kotlin runtime
+
Differences for Kotlin users are more extensive. As a result of the changes detailed below, several plugin
+methods related to the generated Kotlin files have changed or been deprecated. If a method has been deprecated,
+it is no longer in use by the generator and will be removed in the future.
+
+We have merged the generated mapper extension methods into the same file as the mapper itself - eliminating
+generated files with the word "Extensions" in their names. If you are regenerating code in an existing project,
+you should manually delete the old generated extension files (generated Kotlin files with the word "Extensions"
+in the file name).
+
+
+The generated Dynamic SQL Support class in the Kotlin runtime
has changed and now matches the recommended format published in the MyBatis Dynamic SQL documentation (it more closely
matches the style of the support class generated for Java).
This has the following benefits:
@@ -72,9 +82,9 @@ required:
- The declared
SqlTable
object is now an instance of an inner Class rather than an inner Object.
- It should be reference with the new name (typically the same name as previously, with the first letter now
+ It should be referenced with the new name (typically the same name as previously, with the first letter now
lowercase)
- - Reference to any
SqlColumn
should reference the top level value rather than a nested value
+ - References to any
SqlColumn
should reference the top level value rather than a nested value
For example, previously code might have looked like this: