diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java index 3a980e3cf4..7fb793ca4c 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java @@ -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. diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/plugins/CacheNamespacePlugin.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/plugins/CacheNamespacePlugin.java index a661ce24bf..8d43eb3706 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/plugins/CacheNamespacePlugin.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/plugins/CacheNamespacePlugin.java @@ -37,12 +37,17 @@ *
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 + *
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.
*
@@ -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() {
@@ -74,6 +83,10 @@ public String getPropertyName() {
public String getAttributeName() {
return attributeName;
}
+
+ public boolean isClassName() {
+ return isClassName;
+ }
}
@Override
@@ -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;
@@ -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$
@@ -122,12 +152,28 @@ private String calculateAnnotation(IntrospectedTable introspectedTable) {
}
}
- private Optional
This plugin adds a @CacheNamespace annotation to generated mapper interface (Kotlin or Java). This plugin is for MyBatis3 targeted runtimes only.
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.
Any property can be overridden by specifying the property on a <table> element.
diff --git a/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml b/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml index 785cf7d33e..c74dd2098c 100644 --- a/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml +++ b/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml @@ -121,6 +121,8 @@