-
Notifications
You must be signed in to change notification settings - Fork 6
Usage on framework
In this page, we explain ways that integrate with an application framework.
If you are using the mybatis-spring-boot-starter(Spring Boot), you can configure using configuration properties(properties or yaml file) as follow:
mybatis.configuration.default-scripting-language=org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriver
# Workaround for https://github.com/spring-projects/spring-boot/issues/16079
# Adding if need (If you use the Spring Boot 2.1.4+, this configuration not need)
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
mybatis:
configuration:
default-scripting-language: org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriver
# Workaround for https://github.com/spring-projects/spring-boot/issues/16079
# Adding if need (If you use the Spring Boot 2.1.4+, this configuration not need)
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
You can customize a configuration using mybatis-thymeleaf.properties
.
Furthermore, you can customize using the Spring Boot configuration properties instead of mybatis-thymeleaf.properties
as follow:
mybatis.configuration-properties.use-2way=false # (1)
mybatis.configuration-properties.template-file.cache-ttl=3600000
mybatis:
configuration-properties:
use-2way: false # (1)
template-file.cache-ttl: 3600000
@Bean
ConfigurationCustomizer mybatisConfigurationCustomizer(MyBatisProperties properties) {
return configuration -> {
configuration.getLanguageRegistry().register(new ThymeleafLanguageDriver(
ThymeleafLanguageDriverConfig.newInstance(properties.getConfigurationProperties()))); // (2)
configuration.setDefaultScriptingLanguage(ThymeleafLanguageDriver.class); // (3)
};
}
-
Specify the configuration property of
mybatis.configuration.configuration-properties.{property key}
format -
Create a
ThymeleafLanguageDriverConfig
instance usingProperties
object that holds values ofmybatis.configuration.configuration-properties.{property key}
and register aThymeleafLanguageDriver
correspond with it. -
Set the
ThymeleafLanguageDriver
class as default scripting language driver at after registering aThymeleafLanguageDriver
instance instead of Spring Boot configuration properties (Very Important!!)
Also, you can fully customize a template engine using the ConfigurationCustomizer
.
@Bean
ConfigurationCustomizer mybatisConfigurationCustomizer() {
return configuration -> {
TemplateEngine templateEngine = new TemplateEngine(); // (1)
templateEngine.addDialect(new MyBatisDialect());
templateEngine.setEngineContextFactory(new MyBatisIntegratingEngineContextFactory(
targetTemplateEngine.getEngineContextFactory()));
// ...
configuration.getLanguageRegistry().register(new ThymeleafLanguageDriver(templateEngine)); // (2)
configuration.setDefaultScriptingLanguage(ThymeleafLanguageDriver.class); // (3)
};
}
-
Create an instance of class that implements
org.thymeleaf.ITemplateEngine
-
Register an instance of
ThymeleafLanguageDriver
that associate with user-defined template engine instance -
Set the
ThymeleafLanguageDriver
class as default scripting language driver at after registering aThymeleafLanguageDriver
instance instead of Spring Boot configuration properties (Very Important!!)