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
@@ -1,5 +1,5 @@
/*
* 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
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@
* <li>cache_flushInterval</li>
* <li>cache_readWrite</li>
* <li>cache_size</li>
* <li>cache_implementation</li>
* <li>cache_eviction</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
* <p>All properties (except cache_skip) correspond to properties of the MyBatis CacheNamespace annotation.
* Most properties are passed "as is" to the corresponding properties of the generated
* annotation. The properties "cache_implementation" and "cache_eviction" must be fully qualified class names.
* If specified, the values
* will be added to the import list of the mapper file, and the short names will be used in 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.
*
Expand All @@ -54,17 +59,21 @@
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$
BLOCKING("cache_blocking", "blocking", false), //$NON-NLS-1$ //$NON-NLS-2$
FLUSH_INTERVAL("cache_flushInterval", "flushInterval", false), //$NON-NLS-1$ //$NON-NLS-2$
READ_WRITE("cache_readWrite", "readWrite", false), //$NON-NLS-1$ //$NON-NLS-2$
SIZE("cache_size", "size", false), //$NON-NLS-1$ //$NON-NLS-2$
IMPLEMENTATION("cache_implementation", "implementation", true), //$NON-NLS-1$ //$NON-NLS-2$
EVICTION("cache_eviction", "eviction", true); //$NON-NLS-1$ //$NON-NLS-2$

private final String propertyName;
private final String attributeName;
private final boolean isClassName;

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

public String getPropertyName() {
Expand All @@ -74,6 +83,10 @@ public String getPropertyName() {
public String getAttributeName() {
return attributeName;
}

public boolean isClassName() {
return isClassName;
}
}

@Override
Expand All @@ -86,7 +99,16 @@ public boolean clientGenerated(Interface interfaze, IntrospectedTable introspect
if (!skip(introspectedTable)) {
interfaze.addImportedType(
new FullyQualifiedJavaType("org.apache.ibatis.annotations.CacheNamespace")); //$NON-NLS-1$
interfaze.addAnnotation(calculateAnnotation(introspectedTable));

Arrays.stream(CacheProperty.values())
.filter(CacheProperty::isClassName)
.map(cp -> getRawPropertyValue(introspectedTable, cp.getPropertyName()))
.filter(Optional::isPresent)
.map(Optional::get)
.map(FullyQualifiedJavaType::new)
.forEach(interfaze::addImportedType);

interfaze.addAnnotation(calculateAnnotation(introspectedTable, ".class")); //$NON-NLS-1$
}

return true;
Expand All @@ -96,21 +118,29 @@ public boolean clientGenerated(Interface interfaze, IntrospectedTable introspect
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));

Arrays.stream(CacheProperty.values())
.filter(CacheProperty::isClassName)
.map(cp -> getRawPropertyValue(introspectedTable, cp.getPropertyName()))
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(mapperFile::addImport);

mapper.addAnnotation(calculateAnnotation(introspectedTable, "::class")); //$NON-NLS-1$
}

return true;
}

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

private String calculateAnnotation(IntrospectedTable introspectedTable) {
private String calculateAnnotation(IntrospectedTable introspectedTable, String classAccessor) {
String attributes = Arrays.stream(CacheProperty.values())
.map(cp -> calculateAttribute(introspectedTable, cp))
.map(cp -> calculateAttribute(introspectedTable, cp, classAccessor))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.joining(", ")); //$NON-NLS-1$
Expand All @@ -122,12 +152,28 @@ private String calculateAnnotation(IntrospectedTable introspectedTable) {
}
}

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

private Optional<String> getPropertyValue(IntrospectedTable introspectedTable, String propertyName) {
private Optional<String> getPropertyValueForAttribute(IntrospectedTable introspectedTable,
CacheProperty cacheProperty,
String classAccessor) {
Optional<String> value = getRawPropertyValue(introspectedTable, cacheProperty.getPropertyName());

if (cacheProperty.isClassName()) {
value = value.map(FullyQualifiedJavaType::new)
.map(FullyQualifiedJavaType::getShortName)
.map(s -> s + classAccessor);
}

return value;
}

private Optional<String> getRawPropertyValue(IntrospectedTable introspectedTable, String propertyName) {
String value = introspectedTable.getTableConfigurationProperty(propertyName);
if (value == null) {
value = properties.getProperty(propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ private TopLevelClass buildBasicClass() {
topLevelClass.setVisibility(JavaVisibility.PUBLIC);
topLevelClass.setFinal(true);
topLevelClass.addImportedType(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.SqlColumn")); //$NON-NLS-1$
topLevelClass.addImportedType(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.AliasableSqlTable")); //$NON-NLS-1$
topLevelClass.addImportedType(
new FullyQualifiedJavaType("org.mybatis.dynamic.sql.AliasableSqlTable")); //$NON-NLS-1$
topLevelClass.addImportedType(new FullyQualifiedJavaType("java.sql.JDBCType")); //$NON-NLS-1$
return topLevelClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private KotlinType buildInnerClass() {
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();

return KotlinType.newClass(domainObjectName)
.withSuperType("AliasableSqlTable<" + domainObjectName +">(\"" //$NON-NLS-1$ //$NON-NLS-2$
.withSuperType("AliasableSqlTable<" + domainObjectName + ">(\"" //$NON-NLS-1$ //$NON-NLS-2$
+ escapeStringForKotlin(introspectedTable.getFullyQualifiedTableNameAtRuntime())
+ "\", ::" + domainObjectName //$NON-NLS-1$
+ ")") //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ here</a>.</p>
<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,
if specified, most values (except "cache_skip") will be passed directly to the corresponding property
on the generated @CacheNamespace annotation. The cache_eviction and cache_implementation properties
must be fully qualified class names. The class will be added to the import list in the mapper, and the
short name will be used on the generated 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_eviction (must be a fully qualified class name)</li>
<li>cache_implementation (must be a fully qualified class name)</li>
<li>cache_skip</li>
</ul>
<p>Any property can be overridden by specifying the property on a &lt;table&gt; element.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
<plugin type="org.mybatis.generator.plugins.CacheNamespacePlugin" >
<property name="cache_readWrite" value="true"/>
<property name="cache_implementation" value="org.apache.ibatis.cache.impl.PerpetualCache"/>
<property name="cache_eviction" value="org.apache.ibatis.cache.decorators.LruCache"/>
</plugin>

<jdbcConnection driverClass="org.hsqldb.jdbcDriver"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@
<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" />
<plugin type="org.mybatis.generator.plugins.CacheNamespacePlugin" >
<property name="cache_readWrite" value="true"/>
<property name="cache_implementation" value="org.apache.ibatis.cache.impl.PerpetualCache"/>
<property name="cache_eviction" value="org.apache.ibatis.cache.decorators.LruCache"/>
</plugin>

<commentGenerator>
<property name="suppressAllComments" value="true"/>
Expand Down
3 changes: 2 additions & 1 deletion core/mybatis-generator-maven-plugin/pom.xml
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 @@ -69,6 +69,7 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
<plugin type="org.mybatis.generator.plugins.CacheNamespacePlugin" >
<property name="cache_readWrite" value="true"/>
<property name="cache_implementation" value="org.apache.ibatis.cache.impl.PerpetualCache"/>
<property name="cache_eviction" value="org.apache.ibatis.cache.decorators.LruCache"/>
</plugin>

<jdbcConnection driverClass="org.hsqldb.jdbcDriver"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@
<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" />
<plugin type="org.mybatis.generator.plugins.CacheNamespacePlugin" >
<property name="cache_readWrite" value="true"/>
<property name="cache_implementation" value="org.apache.ibatis.cache.impl.PerpetualCache"/>
<property name="cache_eviction" value="org.apache.ibatis.cache.decorators.LruCache"/>
</plugin>

<commentGenerator>
<property name="suppressAllComments" value="true"/>
Expand Down