Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support configuration properties for mybatis-thymeleaf #333

Merged
merged 1 commit into from
May 3, 2019
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 @@ -20,10 +20,10 @@
import org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriver;
import org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriverConfig;
import org.mybatis.scripting.velocity.Driver;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -37,9 +37,11 @@
@ConditionalOnClass(LanguageDriver.class)
public class MybatisLanguageDriverAutoConfiguration {

private static final String CONFIGURATION_PROPERTY_PREFIX = "mybatis.scripting-language-driver";

@Configuration
@ConditionalOnClass(FreeMarkerLanguageDriver.class)
static class FreeMarkerConfiguration {
public static class FreeMarkerConfiguration {
@Bean
@ConditionalOnMissingBean
FreeMarkerLanguageDriver freeMarkerLanguageDriver() {
Expand All @@ -49,7 +51,7 @@ FreeMarkerLanguageDriver freeMarkerLanguageDriver() {

@Configuration
@ConditionalOnClass(Driver.class)
static class VelocityConfiguration {
public static class VelocityConfiguration {
@Bean
@ConditionalOnMissingBean
Driver velocityLanguageDriver() {
Expand All @@ -59,11 +61,18 @@ Driver velocityLanguageDriver() {

@Configuration
@ConditionalOnClass(ThymeleafLanguageDriver.class)
static class ThymeleafConfiguration {
public static class ThymeleafConfiguration {
@Bean
@ConditionalOnMissingBean
ThymeleafLanguageDriver thymeleafLanguageDriver(ThymeleafLanguageDriverConfig config) {
return new ThymeleafLanguageDriver(config);
}

@Bean
@ConditionalOnMissingBean
ThymeleafLanguageDriver thymeleafLanguageDriver(ObjectProvider<ThymeleafLanguageDriverConfig> configProvider) {
return new ThymeleafLanguageDriver(configProvider.getIfAvailable(ThymeleafLanguageDriverConfig::newInstance));
@ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".thymeleaf")
public ThymeleafLanguageDriverConfig thymeleafLanguageDriverConfig() {
return ThymeleafLanguageDriverConfig.newInstance();
}
}

Expand Down
12 changes: 10 additions & 2 deletions mybatis-spring-boot-autoconfigure/src/site/xdoc/index.xml.vm
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,21 @@ public class CityDao {
</tr>
<tr>
<td>
<code>configuration</code>
<code>configuration.*</code>
</td>
<td>
A MyBatis <code>Configuration</code> bean. About available properties see the <a href="http://www.mybatis.org/mybatis-3/configuration.html#settings">MyBatis reference page</a>.
A MyBatis <code>Configuration</code> bean. About available nested properties see the <a href="http://www.mybatis.org/mybatis-3/configuration.html#settings">MyBatis reference page</a>.
<span class="label important">NOTE</span> This property cannot be used at the same time with the <code>config-location</code>.
</td>
</tr>
<tr>
<td>
<code>scripting-language-driver.thymeleaf.*</code>
</td>
<td>
A MyBatis Thymeleaf <code>Configuration</code> bean. About available nested properties see the <a href="http://www.mybatis.org/thymeleaf-scripting/user-guide.html#_configuration_properties">MyBatis Thymeleaf reference page</a>.
</td>
</tr>
</tbody>
</table>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.mybatis.spring.boot.autoconfigure;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import org.apache.ibatis.mapping.BoundSql;
Expand All @@ -25,13 +27,15 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mybatis.scripting.freemarker.FreeMarkerLanguageDriver;
import org.mybatis.scripting.thymeleaf.TemplateEngineCustomizer;
import org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriver;
import org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriverConfig;
import org.mybatis.scripting.velocity.Driver;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.thymeleaf.TemplateEngine;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -66,6 +70,17 @@ void testDefaultConfiguration() {
assertThat(languageDriverBeans.get("freeMarkerLanguageDriver")).isInstanceOf(FreeMarkerLanguageDriver.class);
assertThat(languageDriverBeans.get("velocityLanguageDriver")).isInstanceOf(Driver.class);
assertThat(languageDriverBeans.get("thymeleafLanguageDriver")).isInstanceOf(ThymeleafLanguageDriver.class);
ThymeleafLanguageDriverConfig config = this.context.getBean(ThymeleafLanguageDriverConfig.class);
assertThat(config.isUse2way()).isEqualTo(true);
assertThat(config.getDialect().getPrefix()).isEqualTo("mb");
assertThat(config.getDialect().getLikeAdditionalEscapeTargetChars()).isNull();
assertThat(config.getDialect().getLikeEscapeChar()).isEqualTo('\\');
assertThat(config.getDialect().getLikeEscapeClauseFormat()).isEqualTo("ESCAPE '%s'");
assertThat(config.getTemplateFile().getBaseDir()).isEqualTo("");
assertThat(config.getTemplateFile().getCacheTtl()).isNull();
assertThat(config.getTemplateFile().getEncoding()).isEqualTo(StandardCharsets.UTF_8);
assertThat(config.getTemplateFile().getPatterns()).hasSize(1).contains("*.sql");
assertThat(config.getCustomizer()).isNull();
}

@Test
Expand All @@ -89,6 +104,54 @@ void testCustomThymeleafConfig() {
assertThat(boundSql.getParameterObject()).isEqualTo(10);
assertThat(boundSql.getParameterMappings().get(0).getProperty()).isEqualTo("id");
assertThat(boundSql.getParameterMappings().get(0).getJavaType()).isEqualTo(Integer.class);
ThymeleafLanguageDriverConfig config = this.context.getBean(ThymeleafLanguageDriverConfig.class);
assertThat(config.isUse2way()).isEqualTo(true);
assertThat(config.getDialect().getPrefix()).isEqualTo("m");
assertThat(config.getDialect().getLikeAdditionalEscapeTargetChars()).isNull();
assertThat(config.getDialect().getLikeEscapeChar()).isEqualTo('\\');
assertThat(config.getDialect().getLikeEscapeClauseFormat()).isEqualTo("ESCAPE '%s'");
assertThat(config.getTemplateFile().getBaseDir()).isEqualTo("");
assertThat(config.getTemplateFile().getCacheTtl()).isNull();
assertThat(config.getTemplateFile().getEncoding()).isEqualTo(StandardCharsets.UTF_8);
assertThat(config.getTemplateFile().getPatterns()).hasSize(1).contains("*.sql");
assertThat(config.getCustomizer()).isNull();
}

@Test
void testCustomThymeleafConfigUsingConfigurationProperty() {
TestPropertyValues.of("mybatis.scripting-language-driver.thymeleaf.use2way=false",
"mybatis.scripting-language-driver.thymeleaf.dialect.like-additional-escape-target-chars=*,?",
"mybatis.scripting-language-driver.thymeleaf.dialect.like-escape-char=~",
"mybatis.scripting-language-driver.thymeleaf.dialect.like-escape-clause-format=escape '%s'",
"mybatis.scripting-language-driver.thymeleaf.dialect.prefix=mybatis",
"mybatis.scripting-language-driver.thymeleaf.template-file.base-dir=sqls",
"mybatis.scripting-language-driver.thymeleaf.template-file.cache-enabled=false",
"mybatis.scripting-language-driver.thymeleaf.template-file.cache-ttl=1234",
"mybatis.scripting-language-driver.thymeleaf.template-file.encoding=Windows-31J",
"mybatis.scripting-language-driver.thymeleaf.template-file.patterns=*.sql,*.sqlf",
"mybatis.scripting-language-driver.thymeleaf.customizer=org.mybatis.spring.boot.autoconfigure.MybatisLanguageDriverAutoConfigurationTest$MyTemplateEngineCustomizer")
.applyTo(this.context);
this.context.register(MyAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class);
this.context.refresh();
ThymeleafLanguageDriver driver = this.context.getBean(ThymeleafLanguageDriver.class);
SqlSource sqlSource = driver.createSqlSource(new Configuration(),
"SELECT * FROM users WHERE id = [# mybatis:p='id' /]", Integer.class);
BoundSql boundSql = sqlSource.getBoundSql(10);
assertThat(boundSql.getSql()).isEqualTo("SELECT * FROM users WHERE id = ?");
assertThat(boundSql.getParameterObject()).isEqualTo(10);
assertThat(boundSql.getParameterMappings().get(0).getProperty()).isEqualTo("id");
assertThat(boundSql.getParameterMappings().get(0).getJavaType()).isEqualTo(Integer.class);
ThymeleafLanguageDriverConfig config = this.context.getBean(ThymeleafLanguageDriverConfig.class);
assertThat(config.isUse2way()).isEqualTo(false);
assertThat(config.getDialect().getPrefix()).isEqualTo("mybatis");
assertThat(config.getDialect().getLikeAdditionalEscapeTargetChars()).hasSize(2).contains('*', '?');
assertThat(config.getDialect().getLikeEscapeChar()).isEqualTo('~');
assertThat(config.getDialect().getLikeEscapeClauseFormat()).isEqualTo("escape '%s'");
assertThat(config.getTemplateFile().getBaseDir()).isEqualTo("sqls");
assertThat(config.getTemplateFile().getCacheTtl()).isEqualTo(1234);
assertThat(config.getTemplateFile().getEncoding()).isEqualTo(Charset.forName("Windows-31J"));
assertThat(config.getTemplateFile().getPatterns()).hasSize(2).contains("*.sql", "*.sqlf");
assertThat(config.getCustomizer()).isEqualTo(MyTemplateEngineCustomizer.class);
}

@Test
Expand Down Expand Up @@ -129,4 +192,10 @@ ThymeleafLanguageDriverConfig thymeleafLanguageDriverConfig() {
}
}

public static class MyTemplateEngineCustomizer implements TemplateEngineCustomizer {
@Override
public void customize(TemplateEngine defaultTemplateEngine) {
}
}

}