0
# Customization
1
2
The MyBatis Spring Boot Starter provides several extension points for advanced customization of MyBatis configuration beyond what's available through properties. These customization interfaces allow programmatic control over the auto-configuration process.
3
4
## Capabilities
5
6
### ConfigurationCustomizer
7
8
Functional interface for customizing the MyBatis Configuration object during auto-configuration.
9
10
```java { .api }
11
/**
12
* Callback interface for customizing MyBatis Configuration objects
13
* generated during auto-configuration
14
*/
15
@FunctionalInterface
16
public interface ConfigurationCustomizer {
17
/**
18
* Customize the given MyBatis Configuration object
19
* @param configuration the Configuration object to customize
20
*/
21
void customize(Configuration configuration);
22
}
23
```
24
25
**Usage Examples:**
26
27
```java
28
@Configuration
29
public class MyBatisCustomization {
30
31
@Bean
32
public ConfigurationCustomizer mybatisConfigurationCustomizer() {
33
return configuration -> {
34
// Add custom type handlers
35
configuration.getTypeHandlerRegistry().register(LocalDateTime.class, LocalDateTimeTypeHandler.class);
36
37
// Configure custom object factory
38
configuration.setObjectFactory(new CustomObjectFactory());
39
40
// Add interceptors/plugins
41
configuration.addInterceptor(new CustomInterceptor());
42
43
// Configure custom result handler
44
configuration.getResultMaps().forEach(resultMap -> {
45
// Custom result map modifications
46
});
47
48
// Enable specific MyBatis features
49
configuration.setCallSettersOnNulls(true);
50
configuration.setMapUnderscoreToCamelCase(true);
51
};
52
}
53
54
// You can define multiple customizers - they will all be applied
55
@Bean
56
public ConfigurationCustomizer loggingCustomizer() {
57
return configuration -> {
58
configuration.setLogImpl(StdOutImpl.class);
59
};
60
}
61
}
62
```
63
64
### SqlSessionFactoryBeanCustomizer
65
66
Functional interface for customizing the SqlSessionFactoryBean before the SqlSessionFactory is created.
67
68
```java { .api }
69
/**
70
* Callback interface for customizing SqlSessionFactoryBean objects
71
* generated during auto-configuration
72
*/
73
@FunctionalInterface
74
public interface SqlSessionFactoryBeanCustomizer {
75
/**
76
* Customize the given SqlSessionFactoryBean object
77
* @param factoryBean the SqlSessionFactoryBean to customize
78
*/
79
void customize(SqlSessionFactoryBean factoryBean);
80
}
81
```
82
83
**Usage Examples:**
84
85
```java
86
@Configuration
87
public class SqlSessionFactoryCustomization {
88
89
@Bean
90
public SqlSessionFactoryBeanCustomizer sqlSessionFactoryBeanCustomizer() {
91
return factoryBean -> {
92
// Set custom VFS implementation
93
factoryBean.setVfs(MyCustomVFS.class);
94
95
// Add custom plugins/interceptors
96
factoryBean.setPlugins(new Interceptor[]{
97
new PaginationInterceptor(),
98
new AuditInterceptor()
99
});
100
101
// Set custom database ID provider
102
factoryBean.setDatabaseIdProvider(new CustomDatabaseIdProvider());
103
104
// Configure custom cache
105
factoryBean.setCache(new CustomCache());
106
107
// Set custom object wrapper factory
108
factoryBean.setObjectWrapperFactory(new CustomObjectWrapperFactory());
109
};
110
}
111
112
@Bean
113
public SqlSessionFactoryBeanCustomizer typeHandlerCustomizer(
114
@Autowired List<TypeHandler<?>> typeHandlers) {
115
return factoryBean -> {
116
if (!typeHandlers.isEmpty()) {
117
factoryBean.setTypeHandlers(typeHandlers.toArray(new TypeHandler[0]));
118
}
119
};
120
}
121
}
122
```
123
124
### Custom Spring Boot VFS
125
126
The starter provides a custom VFS implementation optimized for Spring Boot environments.
127
128
```java { .api }
129
/**
130
* Spring Boot compatible VFS implementation for MyBatis
131
*/
132
public class SpringBootVFS extends VFS {
133
/** Default constructor */
134
public SpringBootVFS();
135
136
/** Always returns true - this VFS is always valid in Spring Boot */
137
@Override
138
public boolean isValid();
139
140
/**
141
* Set the charset for decoding URL strings
142
* @param charset the charset for URL decoding
143
*/
144
public static void setUrlDecodingCharset(Charset charset);
145
146
/**
147
* Set the supplier for providing ClassLoader to be used
148
* @param supplier the ClassLoader supplier
149
*/
150
public static void setClassLoaderSupplier(Supplier<ClassLoader> supplier);
151
152
/** Lists resources at the given URL and path */
153
@Override
154
protected List<String> list(URL url, String path) throws IOException;
155
}
156
```
157
158
**Usage Examples:**
159
160
```java
161
@Configuration
162
public class VFSCustomization {
163
164
@PostConstruct
165
public void configureVFS() {
166
// Customize URL decoding charset
167
SpringBootVFS.setUrlDecodingCharset(StandardCharsets.UTF_8);
168
169
// Customize ClassLoader supplier
170
SpringBootVFS.setClassLoaderSupplier(() -> Thread.currentThread().getContextClassLoader());
171
}
172
}
173
```
174
175
## Advanced Customization Patterns
176
177
### Combining Multiple Customizers
178
179
You can define multiple customizers that will all be applied in the order they are discovered:
180
181
```java
182
@Configuration
183
public class ComprehensiveMyBatisCustomization {
184
185
@Bean
186
@Order(1)
187
public ConfigurationCustomizer coreConfigurationCustomizer() {
188
return configuration -> {
189
// Core settings
190
configuration.setMapUnderscoreToCamelCase(true);
191
configuration.setCallSettersOnNulls(true);
192
};
193
}
194
195
@Bean
196
@Order(2)
197
public ConfigurationCustomizer typeHandlerCustomizer() {
198
return configuration -> {
199
// Type handlers
200
configuration.getTypeHandlerRegistry().register(LocalDateTime.class, LocalDateTimeTypeHandler.class);
201
configuration.getTypeHandlerRegistry().register(Instant.class, InstantTypeHandler.class);
202
};
203
}
204
205
@Bean
206
@Order(3)
207
public ConfigurationCustomizer interceptorCustomizer() {
208
return configuration -> {
209
// Interceptors
210
configuration.addInterceptor(new PerformanceInterceptor());
211
configuration.addInterceptor(new LoggingInterceptor());
212
};
213
}
214
}
215
```
216
217
### Conditional Customization
218
219
Apply customizations based on environment or configuration:
220
221
```java
222
@Configuration
223
public class ConditionalMyBatisCustomization {
224
225
@Bean
226
@ConditionalOnProperty(name = "mybatis.performance.monitoring.enabled", havingValue = "true")
227
public ConfigurationCustomizer performanceMonitoringCustomizer() {
228
return configuration -> {
229
configuration.addInterceptor(new PerformanceMonitoringInterceptor());
230
};
231
}
232
233
@Bean
234
@ConditionalOnProperty(name = "mybatis.sql.logging.enabled", havingValue = "true")
235
public ConfigurationCustomizer sqlLoggingCustomizer() {
236
return configuration -> {
237
configuration.setLogImpl(StdOutImpl.class);
238
};
239
}
240
241
@Bean
242
@Profile("dev")
243
public ConfigurationCustomizer developmentCustomizer() {
244
return configuration -> {
245
configuration.addInterceptor(new SqlPrintInterceptor());
246
configuration.setCacheEnabled(false);
247
};
248
}
249
}
250
```
251
252
### Integration with External Libraries
253
254
Customize MyBatis to work with other libraries:
255
256
```java
257
@Configuration
258
public class IntegrationCustomization {
259
260
@Bean
261
public ConfigurationCustomizer jacksonCustomizer(ObjectMapper objectMapper) {
262
return configuration -> {
263
// Configure JSON type handler using Jackson
264
configuration.getTypeHandlerRegistry().register(JsonTypeHandler.class);
265
};
266
}
267
268
@Bean
269
public SqlSessionFactoryBeanCustomizer springSecurityCustomizer() {
270
return factoryBean -> {
271
// Add security-aware interceptor
272
factoryBean.setPlugins(new Interceptor[]{
273
new SecurityContextInterceptor()
274
});
275
};
276
}
277
}
278
```