-
Notifications
You must be signed in to change notification settings - Fork 5
Usage on framework
In this page, we explain ways that integrate with an application framework.
-
The mybatis-spring-boot-starter 2.1 will supports spring-boot 2.1, 2.2, 2.3 and 2.4.
-
The mybatis-spring-boot-starter 2.2 will supports spring-boot 2.2, 2.5+.
Since the mybatis-spring-boot-starter 2.1+, it support the auto-configure for language driver. Therefore you can enable the this plugin on your application simply by adding this artifact under classpath. Furthermore if the LanguageDriver
bean’s count is one, the auto-configure set to default scripting language driver.
You can customize a configuration using mybatis-thymeleaf.properties
. Furthermore since the mybatis-spring-boot-starter 2.1+, it support the configuration properties that prefixed mybatis.scripting-language-driver.thymeleaf
for customizing this plugin feature as follow:
mybatis.scripting-language-driver.thymeleaf.use2way = false
mybatis.scripting-language-driver.thymeleaf.template-file.cache-ttl = 3600000
mybatis:
scripting-language-driver:
thymeleaf:
use2way: false
template-file:
cache-ttl: 3600000
For more information on configurable properties, please refer to the reference documentation.
Also, you can fully customize a template engine by adding the ThymeleafLanguageDriver
instance to the DI container using @Bean
method.
@Bean
ThymeleafLanguageDriver thymeleafLanguageDriver() {
TemplateEngine templateEngine = new TemplateEngine(); // (1)
templateEngine.addDialect(new MyBatisDialect());
templateEngine.setEngineContextFactory(new MyBatisIntegratingEngineContextFactory(
templateEngine.getEngineContextFactory()));
// ...
return new ThymeleafLanguageDriver(templateEngine); // (2)
}
-
Create an instance of class that implements
org.thymeleaf.ITemplateEngine
-
Create and return an instance of
ThymeleafLanguageDriver
that associate with user-defined template engine instance
The mybatis-spring-boot-starter 2.0 supports spring-boot 2.0 and 2.1.
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!!)
For more information on configurable properties, please refer to the reference documentation.
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(
templateEngine.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!!)