0
# Configuration Properties
1
2
The MyBatis Spring Boot Starter provides comprehensive configuration through Spring Boot's properties system, allowing you to configure all aspects of MyBatis behavior using application.properties or application.yml files.
3
4
## Capabilities
5
6
### MybatisProperties
7
8
Main configuration properties class that binds all `mybatis.*` properties to MyBatis settings.
9
10
```java { .api }
11
import org.apache.ibatis.scripting.LanguageDriver;
12
import org.apache.ibatis.session.ExecutorType;
13
import org.springframework.boot.context.properties.ConfigurationProperties;
14
import org.springframework.core.io.Resource;
15
import java.util.Properties;
16
17
/**
18
* Configuration properties for MyBatis integration with Spring Boot
19
*/
20
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
21
public class MybatisProperties {
22
public static final String MYBATIS_PREFIX = "mybatis";
23
24
/** Location of MyBatis xml config file */
25
private String configLocation;
26
27
/** Locations of MyBatis mapper files */
28
private String[] mapperLocations;
29
30
/** Packages to search type aliases (Package delimiters are ",; \t\n") */
31
private String typeAliasesPackage;
32
33
/** Super class for filtering type alias */
34
private Class<?> typeAliasesSuperType;
35
36
/** Packages to search for type handlers (Package delimiters are ",; \t\n") */
37
private String typeHandlersPackage;
38
39
/** Whether to perform presence check of MyBatis xml config file */
40
private boolean checkConfigLocation;
41
42
/** Execution mode for SqlSessionTemplate */
43
private ExecutorType executorType;
44
45
/** Default scripting language driver class */
46
private Class<? extends LanguageDriver> defaultScriptingLanguageDriver;
47
48
/** Externalized properties for MyBatis configuration */
49
private Properties configurationProperties;
50
51
/** Configuration object for customize default settings */
52
private CoreConfiguration configuration;
53
54
// Getters and setters for all properties
55
public String getConfigLocation();
56
public void setConfigLocation(String configLocation);
57
public String[] getMapperLocations();
58
public void setMapperLocations(String[] mapperLocations);
59
public String getTypeAliasesPackage();
60
public void setTypeAliasesPackage(String typeAliasesPackage);
61
public Class<?> getTypeAliasesSuperType();
62
public void setTypeAliasesSuperType(Class<?> typeAliasesSuperType);
63
public String getTypeHandlersPackage();
64
public void setTypeHandlersPackage(String typeHandlersPackage);
65
public boolean isCheckConfigLocation();
66
public void setCheckConfigLocation(boolean checkConfigLocation);
67
public ExecutorType getExecutorType();
68
public void setExecutorType(ExecutorType executorType);
69
public Class<? extends LanguageDriver> getDefaultScriptingLanguageDriver();
70
public void setDefaultScriptingLanguageDriver(Class<? extends LanguageDriver> defaultScriptingLanguageDriver);
71
public Properties getConfigurationProperties();
72
public void setConfigurationProperties(Properties configurationProperties);
73
public CoreConfiguration getConfiguration();
74
public void setConfiguration(CoreConfiguration configuration);
75
76
/** Resolves mapper location patterns to actual Resource objects */
77
public Resource[] resolveMapperLocations();
78
}
79
```
80
81
**Usage Examples:**
82
83
```properties
84
# Basic MyBatis configuration
85
mybatis.config-location=classpath:mybatis-config.xml
86
mybatis.mapper-locations=classpath:mappers/*.xml
87
mybatis.type-aliases-package=com.example.model
88
mybatis.type-handlers-package=com.example.handlers
89
mybatis.check-config-location=true
90
91
# Executor configuration
92
mybatis.executor-type=simple
93
94
# Custom configuration properties
95
mybatis.configuration-properties.key1=value1
96
mybatis.configuration-properties.key2=value2
97
```
98
99
### CoreConfiguration
100
101
Nested configuration class for MyBatis core module settings, providing access to all internal MyBatis configuration options.
102
103
```java { .api }
104
import org.apache.ibatis.io.VFS;
105
import org.apache.ibatis.logging.Log;
106
import org.apache.ibatis.mapping.ResultSetType;
107
import org.apache.ibatis.session.AutoMappingBehavior;
108
import org.apache.ibatis.session.AutoMappingUnknownColumnBehavior;
109
import org.apache.ibatis.session.ExecutorType;
110
import org.apache.ibatis.session.LocalCacheScope;
111
import org.apache.ibatis.type.JdbcType;
112
import org.apache.ibatis.type.TypeHandler;
113
import java.util.Properties;
114
import java.util.Set;
115
116
/**
117
* Configuration properties for MyBatis core module
118
*/
119
public static class CoreConfiguration {
120
/** Allows using RowBounds on nested statements */
121
private Boolean safeRowBoundsEnabled;
122
123
/** Allows using ResultHandler on nested statements */
124
private Boolean safeResultHandlerEnabled;
125
126
/** Enables automatic mapping from database column names to camel case */
127
private Boolean mapUnderscoreToCamelCase;
128
129
/** When enabled, any method call will load all lazy properties */
130
private Boolean aggressiveLazyLoading;
131
132
/** Allows multiple ResultSets from a single statement */
133
private Boolean multipleResultSetsEnabled;
134
135
/** Allows JDBC support for generated keys */
136
private Boolean useGeneratedKeys;
137
138
/** Uses column label instead of column name */
139
private Boolean useColumnLabel;
140
141
/** Globally enables or disables any caches */
142
private Boolean cacheEnabled;
143
144
/** Specifies if setters will be called when retrieved value is null */
145
private Boolean callSettersOnNulls;
146
147
/** Allow referencing statement parameters by their actual names */
148
private Boolean useActualParamName;
149
150
/** Returns empty instance instead of null when all columns are NULL */
151
private Boolean returnInstanceForEmptyRow;
152
153
/** Removes extra whitespace characters from SQL */
154
private Boolean shrinkWhitespacesInSql;
155
156
/** Default value of 'nullable' attribute on 'foreach' tag */
157
private Boolean nullableOnForEach;
158
159
/** Uses argument name for constructor auto-mapping */
160
private Boolean argNameBasedConstructorAutoMapping;
161
162
/** Globally enables or disables lazy loading */
163
private Boolean lazyLoadingEnabled;
164
165
/** Number of seconds driver will wait for response */
166
private Integer defaultStatementTimeout;
167
168
/** Driver hint for fetching size */
169
private Integer defaultFetchSize;
170
171
/** MyBatis local cache scope (SESSION or STATEMENT) */
172
private LocalCacheScope localCacheScope;
173
174
/** JDBC type for null values */
175
private JdbcType jdbcTypeForNull;
176
177
/** Scroll strategy when omit per statement settings */
178
private ResultSetType defaultResultSetType;
179
180
/** Configures the default executor (SIMPLE, REUSE, BATCH) */
181
private ExecutorType defaultExecutorType;
182
183
/** How MyBatis should automatically map columns to fields */
184
private AutoMappingBehavior autoMappingBehavior;
185
186
/** Behavior when detects unknown column of automatic mapping target */
187
private AutoMappingUnknownColumnBehavior autoMappingUnknownColumnBehavior;
188
189
/** Prefix string for logger names */
190
private String logPrefix;
191
192
/** Methods that trigger lazy load */
193
private Set<String> lazyLoadTriggerMethods;
194
195
/** Logging implementation */
196
private Class<? extends Log> logImpl;
197
198
/** VFS implementations */
199
private Class<? extends VFS> vfsImpl;
200
201
/** Default SQL provider class */
202
private Class<?> defaultSqlProviderType;
203
204
/** Default TypeHandler for Enum */
205
private Class<? extends TypeHandler> defaultEnumTypeHandler;
206
207
/** Class that provides Configuration instance */
208
private Class<?> configurationFactory;
209
210
/** Configuration variables */
211
private Properties variables;
212
213
/** Database identify value for switching query */
214
private String databaseId;
215
216
// All getter and setter methods...
217
public Boolean getSafeRowBoundsEnabled();
218
public void setSafeRowBoundsEnabled(Boolean safeRowBoundsEnabled);
219
public Boolean getSafeResultHandlerEnabled();
220
public void setSafeResultHandlerEnabled(Boolean safeResultHandlerEnabled);
221
// ... all other getters and setters
222
223
/** Applies this configuration to a MyBatis Configuration object */
224
public void applyTo(Configuration target);
225
}
226
```
227
228
**Usage Examples:**
229
230
```properties
231
# Core MyBatis configuration through Spring Boot properties
232
mybatis.configuration.map-underscore-to-camel-case=true
233
mybatis.configuration.cache-enabled=true
234
mybatis.configuration.lazy-loading-enabled=true
235
mybatis.configuration.aggressive-lazy-loading=false
236
mybatis.configuration.use-generated-keys=true
237
mybatis.configuration.default-executor-type=simple
238
mybatis.configuration.default-statement-timeout=30
239
mybatis.configuration.default-fetch-size=100
240
mybatis.configuration.safe-row-bounds-enabled=false
241
mybatis.configuration.safe-result-handler-enabled=true
242
mybatis.configuration.use-column-label=true
243
mybatis.configuration.use-actual-param-name=true
244
mybatis.configuration.return-instance-for-empty-row=false
245
mybatis.configuration.shrink-whitespaces-in-sql=false
246
mybatis.configuration.nullable-on-for-each=false
247
mybatis.configuration.arg-name-based-constructor-auto-mapping=false
248
mybatis.configuration.auto-mapping-behavior=partial
249
mybatis.configuration.auto-mapping-unknown-column-behavior=none
250
mybatis.configuration.local-cache-scope=session
251
mybatis.configuration.jdbc-type-for-null=other
252
mybatis.configuration.log-prefix=mybatis.
253
mybatis.configuration.variables.key1=value1
254
mybatis.configuration.database-id=mysql
255
```
256
257
## Additional Configuration Properties
258
259
### Starter-Specific Properties
260
261
Additional properties specific to the Spring Boot integration:
262
263
```properties
264
# Enable lazy initialization for mapper beans
265
mybatis.lazy-initialization=false
266
267
# Default scope for auto-configured mapper beans
268
mybatis.mapper-default-scope=
269
270
# Whether to inject SqlSession on mapper scan (for spring-native compatibility)
271
mybatis.inject-sql-session-on-mapper-scan=true
272
```
273
274
### Scripting Language Driver Properties
275
276
Configuration for additional scripting language drivers:
277
278
```properties
279
# FreeMarker scripting language driver
280
mybatis.scripting-language-driver.freemarker.base-package-to-scan=
281
mybatis.scripting-language-driver.freemarker.freemarker-settings.locale=en_US
282
283
# Velocity scripting language driver
284
mybatis.scripting-language-driver.velocity.velocity-settings.resource.loader=class
285
286
# Thymeleaf scripting language driver
287
mybatis.scripting-language-driver.thymeleaf.use-3x-syntax=true
288
mybatis.scripting-language-driver.thymeleaf.dialect.prefix=mb
289
```