Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public KotlinType getInnerClass() {
return innerClass;
}

public KotlinType getOuterObject() {
return outerObject;
}

public String getTablePropertyImport() {
return getSupportObjectImport()
+ "." //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -182,30 +171,31 @@ public List<KotlinFile> 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<KotlinFile> answer = new ArrayList<>();
if (context.getPlugins().dynamicSqlSupportGenerated(supportFile, supportClassGenerator.getInnerClass(),
if (context.getPlugins().dynamicSqlSupportGenerated(supportFile,
supportClassGenerator.getOuterObject(),
supportClassGenerator.getInnerClass(),
introspectedTable)) {
answer.add(supportFile);
}
Expand All @@ -214,15 +204,10 @@ public List<KotlinFile> 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)
Expand All @@ -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);
}
}
Expand All @@ -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)
Expand All @@ -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);
}
}
Expand All @@ -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)
Expand All @@ -293,23 +276,22 @@ 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)
.withTableFieldName(supportClassGenerator.getTablePropertyName())
.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$
Expand Down Expand Up @@ -366,16 +348,15 @@ 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)
.withTableFieldName(supportClassGenerator.getTablePropertyName())
.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$
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
}
Expand Down
16 changes: 13 additions & 3 deletions core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>Java</b> users
and should simply require updating the MyBatis Dynamic SQL version to 1.3.0 or later.</p>

<p>Differences for <b>Kotlin</b> users are more extensive. The generated Dynamic SQL Support class in the Kotlin runtime
<p>Differences for <b>Kotlin</b> 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.</p>

<p>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).
</p>

<p>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:</p>
Expand Down Expand Up @@ -72,9 +82,9 @@ required:</p>

<ul>
<li>The declared <code>SqlTable</code> 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)</li>
<li>Reference to any <code>SqlColumn</code> should reference the top level value rather than a nested value</li>
<li>References to any <code>SqlColumn</code> should reference the top level value rather than a nested value</li>
</ul>

<p>For example, previously code might have looked like this:</p>
Expand Down