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 @@ *
  • cache_flushInterval
  • *
  • cache_readWrite
  • *
  • cache_size
  • + *
  • cache_implementation
  • + *
  • cache_eviction
  • *
  • cache_skip
  • * * - *

    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 calculateAttribute(IntrospectedTable introspectedTable, CacheProperty cacheProperty) { - return getPropertyValue(introspectedTable, cacheProperty.getPropertyName()) + private Optional 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 getPropertyValue(IntrospectedTable introspectedTable, String propertyName) { + private Optional getPropertyValueForAttribute(IntrospectedTable introspectedTable, + CacheProperty cacheProperty, + String classAccessor) { + Optional value = getRawPropertyValue(introspectedTable, cacheProperty.getPropertyName()); + + if (cacheProperty.isClassName()) { + value = value.map(FullyQualifiedJavaType::new) + .map(FullyQualifiedJavaType::getShortName) + .map(s -> s + classAccessor); + } + + return value; + } + + private Optional getRawPropertyValue(IntrospectedTable introspectedTable, String propertyName) { String value = introspectedTable.getTableConfigurationProperty(propertyName); if (value == null) { value = properties.getProperty(propertyName); diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/dynamic/sql/DynamicSqlSupportClassGenerator.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/dynamic/sql/DynamicSqlSupportClassGenerator.java index 4a1c687feb..e32f80ae4a 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/dynamic/sql/DynamicSqlSupportClassGenerator.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/runtime/dynamic/sql/DynamicSqlSupportClassGenerator.java @@ -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; } 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 fe9381933a..97e08fb41f 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 @@ -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$ diff --git a/core/mybatis-generator-core/src/site/xhtml/reference/plugins.xhtml b/core/mybatis-generator-core/src/site/xhtml/reference/plugins.xhtml index 1025675dd6..0b4076f066 100644 --- a/core/mybatis-generator-core/src/site/xhtml/reference/plugins.xhtml +++ b/core/mybatis-generator-core/src/site/xhtml/reference/plugins.xhtml @@ -44,14 +44,19 @@ here.

    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 @@ + + - + + + + + diff --git a/core/mybatis-generator-maven-plugin/pom.xml b/core/mybatis-generator-maven-plugin/pom.xml index 293445fb94..6f3662d871 100644 --- a/core/mybatis-generator-maven-plugin/pom.xml +++ b/core/mybatis-generator-maven-plugin/pom.xml @@ -1,7 +1,7 @@