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 @@ -1201,9 +1201,9 @@ public boolean mapperExtensionsGenerated(KotlinFile extensionsFile, Introspected
}

@Override
public boolean mapperGenerated(KotlinFile mapperFile, IntrospectedTable introspectedTable) {
public boolean mapperGenerated(KotlinFile mapperFile, KotlinType mapper, IntrospectedTable introspectedTable) {
for (Plugin plugin : plugins) {
if (!plugin.mapperGenerated(mapperFile, introspectedTable)) {
if (!plugin.mapperGenerated(mapperFile, mapper, introspectedTable)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ default boolean mapperExtensionsGenerated(KotlinFile extensionsFile, Introspecte
return true;
}

default boolean mapperGenerated(KotlinFile mapperFile, IntrospectedTable introspectedTable) {
default boolean mapperGenerated(KotlinFile mapperFile, KotlinType mapper, IntrospectedTable introspectedTable) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.generator.plugins;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.kotlin.KotlinFile;
import org.mybatis.generator.api.dom.kotlin.KotlinType;
import org.mybatis.generator.internal.util.StringUtility;

/**
* This plugin adds a CacheNamespace annotation to generated Java or Kotlin mapper interfaces.
* The plugin accepts the following properties (all are optional):
*
* <ul>
* <li>cache_blocking</li>
* <li>cache_flushInterval</li>
* <li>cache_readWrite</li>
* <li>cache_size</li>
* <li>cache_skip</li>
* </ul>
*
* <p>All properties (except cache_skip) correspond to properties of the MyBatis CacheNamespace annotation and
* are passed "as is" to the corresponding properties of the generated
* annotation. All properties can be specified at the table level, or on the
* plugin element. The property on the table element will override any
* property on the plugin element.
*
* <p>If the "cache_skip" property is set to "true" - either on the plugin or on a specific table,
* the annotation will not be applied to the generated interface.
*
* @author Jeff Butler
*/
public class CacheNamespacePlugin extends PluginAdapter {

public enum CacheProperty {
BLOCKING("cache_blocking", "blocking"), //$NON-NLS-1$ //$NON-NLS-2$
FLUSH_INTERVAL("cache_flushInterval", "flushInterval"), //$NON-NLS-1$ //$NON-NLS-2$
READ_WRITE("cache_readWrite", "readWrite"), //$NON-NLS-1$ //$NON-NLS-2$
SIZE("cache_size", "size"); //$NON-NLS-1$ //$NON-NLS-2$

private final String propertyName;
private final String attributeName;

CacheProperty(String propertyName, String attributeName) {
this.propertyName = propertyName;
this.attributeName = attributeName;
}

public String getPropertyName() {
return propertyName;
}

public String getAttributeName() {
return attributeName;
}
}

@Override
public boolean validate(List<String> arg0) {
return true;
}

@Override
public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
if (!skip(introspectedTable)) {
interfaze.addImportedType(
new FullyQualifiedJavaType("org.apache.ibatis.annotations.CacheNamespace")); //$NON-NLS-1$
interfaze.addAnnotation(calculateAnnotation(introspectedTable));
}

return true;
}

@Override
public boolean mapperGenerated(KotlinFile mapperFile, KotlinType mapper, IntrospectedTable introspectedTable) {
if (!skip(introspectedTable)) {
mapperFile.addImport("org.apache.ibatis.annotations.CacheNamespace"); //$NON-NLS-1$
mapper.addAnnotation(calculateAnnotation(introspectedTable));
}

return true;
}

private boolean skip(IntrospectedTable introspectedTable) {
return getPropertyValue(introspectedTable, "cache_skip") //$NON-NLS-1$
.map("true"::equalsIgnoreCase) //$NON-NLS-1$
.orElse(false);
}

private String calculateAnnotation(IntrospectedTable introspectedTable) {
String attributes = Arrays.stream(CacheProperty.values())
.map(cp -> calculateAttribute(introspectedTable, cp))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.joining(", ")); //$NON-NLS-1$

if (StringUtility.stringHasValue(attributes)) {
return "@CacheNamespace(" + attributes + ")"; //$NON-NLS-1$ //$NON-NLS-2$
} else {
return "@CacheNamespace"; //$NON-NLS-1$
}
}

private Optional<String> calculateAttribute(IntrospectedTable introspectedTable, CacheProperty cacheProperty) {
return getPropertyValue(introspectedTable, cacheProperty.getPropertyName())
.map(v -> String.format("%s = %s", cacheProperty.getAttributeName(), v)); //$NON-NLS-1$
}

private Optional<String> getPropertyValue(IntrospectedTable introspectedTable, String propertyName) {
String value = introspectedTable.getTableConfigurationProperty(propertyName);
if (value == null) {
value = properties.getProperty(propertyName);
}

if (StringUtility.stringHasValue(value)) {
return Optional.of(value);
} else {
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public List<KotlinFile> getKotlinFiles() {
answer.add(supportFile);
}

if (context.getPlugins().mapperGenerated(mapperFile, introspectedTable)) {
if (context.getPlugins().mapperGenerated(mapperFile, mapper, introspectedTable)) {
answer.add(mapperFile);
}

Expand Down
21 changes: 18 additions & 3 deletions core/mybatis-generator-core/src/site/xhtml/reference/plugins.xhtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2006-2020 the original author or authors.
Copyright 2006-2021 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,10 +40,25 @@ online
<a target="_blank" href="https://github.com/mybatis/generator/tree/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/plugins">
here</a>.</p>

<h2>org.mybatis.generator.plugins.CacheNamespacePlugin</h2>
<p>This plugin adds a @CacheNamespace annotation to generated mapper interface (Kotlin or Java). This
plugin is for MyBatis3 targeted runtimes only.</p>
<p>This plugin accepts the following properties. All are optional and,
if specified, the values (except "cache_skip") will be passed directly to the corresponding property
on the generated @CacheNamespace annotation. If "cache_skip" is set to "true" on the plugin configuration,
or for any table, the annotation will not be generated.</p>
<ul>
<li>cache_blocking</li>
<li>cache_flushInterval</li>
<li>cache_readWrite</li>
<li>cache_size</li>
<li>cache_skip</li>
</ul>
<p>Any property can be overridden by specifying the property on a &lt;table&gt; element.</p>

<h2>org.mybatis.generator.plugins.CachePlugin</h2>
<p>This plugin has no impact when the target runtime in use does not generate XML.</p>
<p>This plugin adds a &lt;cache&gt; element to generated SQL maps. This
plugin is for MyBatis3 targeted runtimes only.</p>
<p>This plugin adds a &lt;cache&gt; element to generated SQL maps.</p>
<p>This plugin accepts the following properties. All are optional and,
if specified, the values will be passed directly to the corresponding property
on the generated &lt;cache&gt; element.</p>
Expand Down
1 change: 1 addition & 0 deletions core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ required:</p>
<li>Added capabilities to the SerializablePlugin for Kotlin</li>
<li>Added the ability to specify a package for MyBatis Dynamic SQL Support Classes</li>
<li>Added the ability to specify a MyBatis Dynamic SQL Support class name on the table configuration</li>
<li>Added a plugin to generate @CacheNamespace annotations</li>
</ul>

<h3>Removed Items</h3>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2006-2020 the original author or authors.
Copyright 2006-2021 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -119,10 +119,9 @@
<context id="kotlin-miscellaneousTests_Annotated" targetRuntime="MyBatis3Kotlin">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />

<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<plugin type="org.mybatis.generator.plugins.CacheNamespacePlugin" >
<property name="cache_readWrite" value="true"/>
</plugin>

<jdbcConnection driverClass="org.hsqldb.jdbcDriver"
connectionURL="jdbc:hsqldb:mem:aname"
Expand All @@ -149,6 +148,7 @@
<columnOverride column="wierd$Field" delimitedColumnName="true"/>
</table>
<table tableName="RegexRename">
<property name="cache_skip" value="true"/>
<generatedKey column="CUST_ID" sqlStatement="call next value for TestSequence" />
<columnRenamingRule searchString="^CUST" />
</table>
Expand All @@ -167,6 +167,7 @@

<context id="kotlin-modelonly" targetRuntime="MyBatis3Kotlin">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

<jdbcConnection driverClass="org.hsqldb.jdbcDriver"
connectionURL="jdbc:hsqldb:mem:aname"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<context id="miscellaneousTests_Annotated" targetRuntime="MyBatis3DynamicSql">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
<plugin type="org.mybatis.generator.plugins.CacheNamespacePlugin" />

<commentGenerator>
<property name="suppressAllComments" value="true"/>
Expand Down Expand Up @@ -151,6 +152,7 @@
<columnOverride column="wierd$Field" delimitedColumnName="true"/>
</table>
<table tableName="RegexRename">
<property name="cache_skip" value="true"/>
<generatedKey column="CUST_ID" sqlStatement="call next value for TestSequence" />
<columnRenamingRule searchString="^CUST" />
</table>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2006-2020 the original author or authors.
Copyright 2006-2021 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -119,6 +119,9 @@
<context id="kotlin-miscellaneousTests_Annotated" targetRuntime="MyBatis3Kotlin">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
<plugin type="org.mybatis.generator.plugins.CacheNamespacePlugin" >
<property name="cache_readWrite" value="true"/>
</plugin>

<jdbcConnection driverClass="org.hsqldb.jdbcDriver"
connectionURL="jdbc:hsqldb:mem:aname"
Expand All @@ -145,6 +148,7 @@
<columnOverride column="wierd$Field" delimitedColumnName="true"/>
</table>
<table tableName="RegexRename">
<property name="cache_skip" value="true"/>
<generatedKey column="CUST_ID" sqlStatement="call next value for TestSequence" />
<columnRenamingRule searchString="^CUST" />
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<context id="miscellaneousTests_Annotated" targetRuntime="MyBatis3DynamicSql">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
<plugin type="org.mybatis.generator.plugins.CacheNamespacePlugin" />

<commentGenerator>
<property name="suppressAllComments" value="true"/>
Expand Down Expand Up @@ -151,6 +152,7 @@
<columnOverride column="wierd$Field" delimitedColumnName="true"/>
</table>
<table tableName="RegexRename">
<property name="cache_skip" value="true"/>
<generatedKey column="CUST_ID" sqlStatement="call next value for TestSequence" />
<columnRenamingRule searchString="^CUST" />
</table>
Expand Down