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