1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.spring.boot.autoconfigure;
17
18 import java.io.IOException;
19 import java.util.Optional;
20 import java.util.Properties;
21 import java.util.Set;
22 import java.util.stream.Stream;
23
24 import org.apache.ibatis.io.VFS;
25 import org.apache.ibatis.logging.Log;
26 import org.apache.ibatis.mapping.ResultSetType;
27 import org.apache.ibatis.scripting.LanguageDriver;
28 import org.apache.ibatis.session.AutoMappingBehavior;
29 import org.apache.ibatis.session.AutoMappingUnknownColumnBehavior;
30 import org.apache.ibatis.session.Configuration;
31 import org.apache.ibatis.session.ExecutorType;
32 import org.apache.ibatis.session.LocalCacheScope;
33 import org.apache.ibatis.type.JdbcType;
34 import org.apache.ibatis.type.TypeHandler;
35 import org.springframework.boot.context.properties.ConfigurationProperties;
36 import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
37 import org.springframework.boot.context.properties.PropertyMapper;
38 import org.springframework.core.io.Resource;
39 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
40 import org.springframework.core.io.support.ResourcePatternResolver;
41
42
43
44
45
46
47
48 @ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
49 public class MybatisProperties {
50
51 public static final String MYBATIS_PREFIX = "mybatis";
52
53 private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
54
55
56
57
58 private String configLocation;
59
60
61
62
63 private String[] mapperLocations;
64
65
66
67
68 private String typeAliasesPackage;
69
70
71
72
73
74 private Class<?> typeAliasesSuperType;
75
76
77
78
79 private String typeHandlersPackage;
80
81
82
83
84 private boolean checkConfigLocation = false;
85
86
87
88
89 private ExecutorType executorType;
90
91
92
93
94 private Class<? extends LanguageDriver> defaultScriptingLanguageDriver;
95
96
97
98
99 private Properties configurationProperties;
100
101
102
103
104
105 private CoreConfiguration configuration;
106
107
108
109
110 public String getConfigLocation() {
111 return this.configLocation;
112 }
113
114
115
116
117 public void setConfigLocation(String configLocation) {
118 this.configLocation = configLocation;
119 }
120
121 public String[] getMapperLocations() {
122 return this.mapperLocations;
123 }
124
125 public void setMapperLocations(String[] mapperLocations) {
126 this.mapperLocations = mapperLocations;
127 }
128
129 public String getTypeHandlersPackage() {
130 return this.typeHandlersPackage;
131 }
132
133 public void setTypeHandlersPackage(String typeHandlersPackage) {
134 this.typeHandlersPackage = typeHandlersPackage;
135 }
136
137 public String getTypeAliasesPackage() {
138 return this.typeAliasesPackage;
139 }
140
141 public void setTypeAliasesPackage(String typeAliasesPackage) {
142 this.typeAliasesPackage = typeAliasesPackage;
143 }
144
145
146
147
148 public Class<?> getTypeAliasesSuperType() {
149 return typeAliasesSuperType;
150 }
151
152
153
154
155 public void setTypeAliasesSuperType(Class<?> typeAliasesSuperType) {
156 this.typeAliasesSuperType = typeAliasesSuperType;
157 }
158
159 public boolean isCheckConfigLocation() {
160 return this.checkConfigLocation;
161 }
162
163 public void setCheckConfigLocation(boolean checkConfigLocation) {
164 this.checkConfigLocation = checkConfigLocation;
165 }
166
167 public ExecutorType getExecutorType() {
168 return this.executorType;
169 }
170
171 public void setExecutorType(ExecutorType executorType) {
172 this.executorType = executorType;
173 }
174
175
176
177
178 public Class<? extends LanguageDriver> getDefaultScriptingLanguageDriver() {
179 return defaultScriptingLanguageDriver;
180 }
181
182
183
184
185 public void setDefaultScriptingLanguageDriver(Class<? extends LanguageDriver> defaultScriptingLanguageDriver) {
186 this.defaultScriptingLanguageDriver = defaultScriptingLanguageDriver;
187 }
188
189
190
191
192 public Properties getConfigurationProperties() {
193 return configurationProperties;
194 }
195
196
197
198
199 public void setConfigurationProperties(Properties configurationProperties) {
200 this.configurationProperties = configurationProperties;
201 }
202
203 public CoreConfiguration getConfiguration() {
204 return configuration;
205 }
206
207 public void setConfiguration(CoreConfiguration configuration) {
208 this.configuration = configuration;
209 }
210
211 public Resource[] resolveMapperLocations() {
212 return Stream.of(Optional.ofNullable(this.mapperLocations).orElse(new String[0]))
213 .flatMap(location -> Stream.of(getResources(location))).toArray(Resource[]::new);
214 }
215
216 private Resource[] getResources(String location) {
217 try {
218 return resourceResolver.getResources(location);
219 } catch (IOException e) {
220 return new Resource[0];
221 }
222 }
223
224
225
226
227
228
229 public static class CoreConfiguration {
230
231
232
233
234 private Boolean safeRowBoundsEnabled;
235
236
237
238
239 private Boolean safeResultHandlerEnabled;
240
241
242
243
244
245 private Boolean mapUnderscoreToCamelCase;
246
247
248
249
250
251 private Boolean aggressiveLazyLoading;
252
253
254
255
256
257 private Boolean multipleResultSetsEnabled;
258
259
260
261
262
263 private Boolean useGeneratedKeys;
264
265
266
267
268
269 private Boolean useColumnLabel;
270
271
272
273
274 private Boolean cacheEnabled;
275
276
277
278
279
280
281 private Boolean callSettersOnNulls;
282
283
284
285
286
287 private Boolean useActualParamName;
288
289
290
291
292
293
294 private Boolean returnInstanceForEmptyRow;
295
296
297
298
299
300 private Boolean shrinkWhitespacesInSql;
301
302
303
304
305 private Boolean nullableOnForEach;
306
307
308
309
310
311 private Boolean argNameBasedConstructorAutoMapping;
312
313
314
315
316
317 private Boolean lazyLoadingEnabled;
318
319
320
321
322 private Integer defaultStatementTimeout;
323
324
325
326
327
328 private Integer defaultFetchSize;
329
330
331
332
333
334
335
336 private LocalCacheScope localCacheScope;
337
338
339
340
341
342
343 private JdbcType jdbcTypeForNull;
344
345
346
347
348 private ResultSetType defaultResultSetType;
349
350
351
352
353
354 private ExecutorType defaultExecutorType;
355
356
357
358
359
360
361 private AutoMappingBehavior autoMappingBehavior;
362
363
364
365
366
367 private AutoMappingUnknownColumnBehavior autoMappingUnknownColumnBehavior;
368
369
370
371
372 private String logPrefix;
373
374
375
376
377 private Set<String> lazyLoadTriggerMethods;
378
379
380
381
382
383 private Class<? extends Log> logImpl;
384
385
386
387
388 private Class<? extends VFS> vfsImpl;
389
390
391
392
393
394 private Class<?> defaultSqlProviderType;
395
396
397
398
399 Class<? extends TypeHandler> defaultEnumTypeHandler;
400
401
402
403
404
405
406 private Class<?> configurationFactory;
407
408
409
410
411 private Properties variables;
412
413
414
415
416 private String databaseId;
417
418 public Boolean getSafeRowBoundsEnabled() {
419 return safeRowBoundsEnabled;
420 }
421
422 public void setSafeRowBoundsEnabled(Boolean safeRowBoundsEnabled) {
423 this.safeRowBoundsEnabled = safeRowBoundsEnabled;
424 }
425
426 public Boolean getSafeResultHandlerEnabled() {
427 return safeResultHandlerEnabled;
428 }
429
430 public void setSafeResultHandlerEnabled(Boolean safeResultHandlerEnabled) {
431 this.safeResultHandlerEnabled = safeResultHandlerEnabled;
432 }
433
434 public Boolean getMapUnderscoreToCamelCase() {
435 return mapUnderscoreToCamelCase;
436 }
437
438 public void setMapUnderscoreToCamelCase(Boolean mapUnderscoreToCamelCase) {
439 this.mapUnderscoreToCamelCase = mapUnderscoreToCamelCase;
440 }
441
442 public Boolean getAggressiveLazyLoading() {
443 return aggressiveLazyLoading;
444 }
445
446 public void setAggressiveLazyLoading(Boolean aggressiveLazyLoading) {
447 this.aggressiveLazyLoading = aggressiveLazyLoading;
448 }
449
450 @DeprecatedConfigurationProperty(since = "3.0.4", reason = "The option is not used at MyBatis core module. It will be removed in the future. See https://github.com/mybatis/mybatis-3/pull/3238")
451 public Boolean getMultipleResultSetsEnabled() {
452 return multipleResultSetsEnabled;
453 }
454
455 public void setMultipleResultSetsEnabled(Boolean multipleResultSetsEnabled) {
456 this.multipleResultSetsEnabled = multipleResultSetsEnabled;
457 }
458
459 public Boolean getUseGeneratedKeys() {
460 return useGeneratedKeys;
461 }
462
463 public void setUseGeneratedKeys(Boolean useGeneratedKeys) {
464 this.useGeneratedKeys = useGeneratedKeys;
465 }
466
467 public Boolean getUseColumnLabel() {
468 return useColumnLabel;
469 }
470
471 public void setUseColumnLabel(Boolean useColumnLabel) {
472 this.useColumnLabel = useColumnLabel;
473 }
474
475 public Boolean getCacheEnabled() {
476 return cacheEnabled;
477 }
478
479 public void setCacheEnabled(Boolean cacheEnabled) {
480 this.cacheEnabled = cacheEnabled;
481 }
482
483 public Boolean getCallSettersOnNulls() {
484 return callSettersOnNulls;
485 }
486
487 public void setCallSettersOnNulls(Boolean callSettersOnNulls) {
488 this.callSettersOnNulls = callSettersOnNulls;
489 }
490
491 public Boolean getUseActualParamName() {
492 return useActualParamName;
493 }
494
495 public void setUseActualParamName(Boolean useActualParamName) {
496 this.useActualParamName = useActualParamName;
497 }
498
499 public Boolean getReturnInstanceForEmptyRow() {
500 return returnInstanceForEmptyRow;
501 }
502
503 public void setReturnInstanceForEmptyRow(Boolean returnInstanceForEmptyRow) {
504 this.returnInstanceForEmptyRow = returnInstanceForEmptyRow;
505 }
506
507 public Boolean getShrinkWhitespacesInSql() {
508 return shrinkWhitespacesInSql;
509 }
510
511 public void setShrinkWhitespacesInSql(Boolean shrinkWhitespacesInSql) {
512 this.shrinkWhitespacesInSql = shrinkWhitespacesInSql;
513 }
514
515 public Boolean getNullableOnForEach() {
516 return nullableOnForEach;
517 }
518
519 public void setNullableOnForEach(Boolean nullableOnForEach) {
520 this.nullableOnForEach = nullableOnForEach;
521 }
522
523 public Boolean getArgNameBasedConstructorAutoMapping() {
524 return argNameBasedConstructorAutoMapping;
525 }
526
527 public void setArgNameBasedConstructorAutoMapping(Boolean argNameBasedConstructorAutoMapping) {
528 this.argNameBasedConstructorAutoMapping = argNameBasedConstructorAutoMapping;
529 }
530
531 public String getLogPrefix() {
532 return logPrefix;
533 }
534
535 public void setLogPrefix(String logPrefix) {
536 this.logPrefix = logPrefix;
537 }
538
539 public Class<? extends Log> getLogImpl() {
540 return logImpl;
541 }
542
543 public void setLogImpl(Class<? extends Log> logImpl) {
544 this.logImpl = logImpl;
545 }
546
547 public Class<? extends VFS> getVfsImpl() {
548 return vfsImpl;
549 }
550
551 public void setVfsImpl(Class<? extends VFS> vfsImpl) {
552 this.vfsImpl = vfsImpl;
553 }
554
555 public Class<?> getDefaultSqlProviderType() {
556 return defaultSqlProviderType;
557 }
558
559 public void setDefaultSqlProviderType(Class<?> defaultSqlProviderType) {
560 this.defaultSqlProviderType = defaultSqlProviderType;
561 }
562
563 public LocalCacheScope getLocalCacheScope() {
564 return localCacheScope;
565 }
566
567 public void setLocalCacheScope(LocalCacheScope localCacheScope) {
568 this.localCacheScope = localCacheScope;
569 }
570
571 public JdbcType getJdbcTypeForNull() {
572 return jdbcTypeForNull;
573 }
574
575 public void setJdbcTypeForNull(JdbcType jdbcTypeForNull) {
576 this.jdbcTypeForNull = jdbcTypeForNull;
577 }
578
579 public Set<String> getLazyLoadTriggerMethods() {
580 return lazyLoadTriggerMethods;
581 }
582
583 public void setLazyLoadTriggerMethods(Set<String> lazyLoadTriggerMethods) {
584 this.lazyLoadTriggerMethods = lazyLoadTriggerMethods;
585 }
586
587 public Integer getDefaultStatementTimeout() {
588 return defaultStatementTimeout;
589 }
590
591 public void setDefaultStatementTimeout(Integer defaultStatementTimeout) {
592 this.defaultStatementTimeout = defaultStatementTimeout;
593 }
594
595 public Integer getDefaultFetchSize() {
596 return defaultFetchSize;
597 }
598
599 public void setDefaultFetchSize(Integer defaultFetchSize) {
600 this.defaultFetchSize = defaultFetchSize;
601 }
602
603 public ResultSetType getDefaultResultSetType() {
604 return defaultResultSetType;
605 }
606
607 public void setDefaultResultSetType(ResultSetType defaultResultSetType) {
608 this.defaultResultSetType = defaultResultSetType;
609 }
610
611 public ExecutorType getDefaultExecutorType() {
612 return defaultExecutorType;
613 }
614
615 public void setDefaultExecutorType(ExecutorType defaultExecutorType) {
616 this.defaultExecutorType = defaultExecutorType;
617 }
618
619 public AutoMappingBehavior getAutoMappingBehavior() {
620 return autoMappingBehavior;
621 }
622
623 public void setAutoMappingBehavior(AutoMappingBehavior autoMappingBehavior) {
624 this.autoMappingBehavior = autoMappingBehavior;
625 }
626
627 public AutoMappingUnknownColumnBehavior getAutoMappingUnknownColumnBehavior() {
628 return autoMappingUnknownColumnBehavior;
629 }
630
631 public void setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior autoMappingUnknownColumnBehavior) {
632 this.autoMappingUnknownColumnBehavior = autoMappingUnknownColumnBehavior;
633 }
634
635 public Properties getVariables() {
636 return variables;
637 }
638
639 public void setVariables(Properties variables) {
640 this.variables = variables;
641 }
642
643 public Boolean getLazyLoadingEnabled() {
644 return lazyLoadingEnabled;
645 }
646
647 public void setLazyLoadingEnabled(Boolean lazyLoadingEnabled) {
648 this.lazyLoadingEnabled = lazyLoadingEnabled;
649 }
650
651 public Class<?> getConfigurationFactory() {
652 return configurationFactory;
653 }
654
655 public void setConfigurationFactory(Class<?> configurationFactory) {
656 this.configurationFactory = configurationFactory;
657 }
658
659 public Class<? extends TypeHandler> getDefaultEnumTypeHandler() {
660 return defaultEnumTypeHandler;
661 }
662
663 public void setDefaultEnumTypeHandler(Class<? extends TypeHandler> defaultEnumTypeHandler) {
664 this.defaultEnumTypeHandler = defaultEnumTypeHandler;
665 }
666
667 public String getDatabaseId() {
668 return databaseId;
669 }
670
671 public void setDatabaseId(String databaseId) {
672 this.databaseId = databaseId;
673 }
674
675 public void applyTo(Configuration target) {
676 PropertyMapper mapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
677 mapper.from(getSafeRowBoundsEnabled()).to(target::setSafeRowBoundsEnabled);
678 mapper.from(getSafeResultHandlerEnabled()).to(target::setSafeResultHandlerEnabled);
679 mapper.from(getMapUnderscoreToCamelCase()).to(target::setMapUnderscoreToCamelCase);
680 mapper.from(getAggressiveLazyLoading()).to(target::setAggressiveLazyLoading);
681 mapper.from(getMultipleResultSetsEnabled()).to(target::setMultipleResultSetsEnabled);
682 mapper.from(getUseGeneratedKeys()).to(target::setUseGeneratedKeys);
683 mapper.from(getUseColumnLabel()).to(target::setUseColumnLabel);
684 mapper.from(getCacheEnabled()).to(target::setCacheEnabled);
685 mapper.from(getCallSettersOnNulls()).to(target::setCallSettersOnNulls);
686 mapper.from(getUseActualParamName()).to(target::setUseActualParamName);
687 mapper.from(getReturnInstanceForEmptyRow()).to(target::setReturnInstanceForEmptyRow);
688 mapper.from(getShrinkWhitespacesInSql()).to(target::setShrinkWhitespacesInSql);
689 mapper.from(getNullableOnForEach()).to(target::setNullableOnForEach);
690 mapper.from(getArgNameBasedConstructorAutoMapping()).to(target::setArgNameBasedConstructorAutoMapping);
691 mapper.from(getLazyLoadingEnabled()).to(target::setLazyLoadingEnabled);
692 mapper.from(getLogPrefix()).to(target::setLogPrefix);
693 mapper.from(getLazyLoadTriggerMethods()).to(target::setLazyLoadTriggerMethods);
694 mapper.from(getDefaultStatementTimeout()).to(target::setDefaultStatementTimeout);
695 mapper.from(getDefaultFetchSize()).to(target::setDefaultFetchSize);
696 mapper.from(getLocalCacheScope()).to(target::setLocalCacheScope);
697 mapper.from(getJdbcTypeForNull()).to(target::setJdbcTypeForNull);
698 mapper.from(getDefaultResultSetType()).to(target::setDefaultResultSetType);
699 mapper.from(getDefaultExecutorType()).to(target::setDefaultExecutorType);
700 mapper.from(getAutoMappingBehavior()).to(target::setAutoMappingBehavior);
701 mapper.from(getAutoMappingUnknownColumnBehavior()).to(target::setAutoMappingUnknownColumnBehavior);
702 mapper.from(getVariables()).to(target::setVariables);
703 mapper.from(getLogImpl()).to(target::setLogImpl);
704 mapper.from(getVfsImpl()).to(target::setVfsImpl);
705 mapper.from(getDefaultSqlProviderType()).to(target::setDefaultSqlProviderType);
706 mapper.from(getConfigurationFactory()).to(target::setConfigurationFactory);
707 mapper.from(getDefaultEnumTypeHandler()).to(target::setDefaultEnumTypeHandler);
708 mapper.from(getDatabaseId()).to(target::setDatabaseId);
709 }
710
711 }
712
713 }