23
23
24
24
import java .io .IOException ;
25
25
import java .sql .SQLException ;
26
+ import java .util .Optional ;
26
27
import java .util .Properties ;
28
+ import java .util .stream .Stream ;
27
29
28
30
import javax .sql .DataSource ;
29
31
@@ -434,61 +436,51 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
434
436
} else {
435
437
LOGGER .debug (() -> "Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration" );
436
438
targetConfiguration = new Configuration ();
437
- if (this .configurationProperties != null ) {
438
- targetConfiguration .setVariables (this .configurationProperties );
439
- }
440
- }
441
-
442
- if (this .objectFactory != null ) {
443
- targetConfiguration .setObjectFactory (this .objectFactory );
439
+ Optional .ofNullable (this .configurationProperties ).ifPresent (targetConfiguration ::setVariables );
444
440
}
445
441
446
- if (this .objectWrapperFactory != null ) {
447
- targetConfiguration .setObjectWrapperFactory (this .objectWrapperFactory );
448
- }
449
-
450
- if (this .vfs != null ) {
451
- targetConfiguration .setVfsImpl (this .vfs );
452
- }
442
+ Optional .ofNullable (this .objectFactory ).ifPresent (targetConfiguration ::setObjectFactory );
443
+ Optional .ofNullable (this .objectWrapperFactory ).ifPresent (targetConfiguration ::setObjectWrapperFactory );
444
+ Optional .ofNullable (this .vfs ).ifPresent (targetConfiguration ::setVfsImpl );
453
445
454
446
if (hasLength (this .typeAliasesPackage )) {
455
447
String [] typeAliasPackageArray = tokenizeToStringArray (this .typeAliasesPackage ,
456
448
ConfigurableApplicationContext .CONFIG_LOCATION_DELIMITERS );
457
- for ( String packageToScan : typeAliasPackageArray ) {
449
+ Stream . of ( typeAliasPackageArray ). forEach ( packageToScan -> {
458
450
targetConfiguration .getTypeAliasRegistry ().registerAliases (packageToScan ,
459
- typeAliasesSuperType == null ? Object .class : typeAliasesSuperType );
451
+ typeAliasesSuperType == null ? Object .class : typeAliasesSuperType );
460
452
LOGGER .debug (() -> "Scanned package: '" + packageToScan + "' for aliases" );
461
- }
453
+ });
462
454
}
463
455
464
456
if (!isEmpty (this .typeAliases )) {
465
- for ( Class <?> typeAlias : this .typeAliases ) {
457
+ Stream . of ( this .typeAliases ). forEach ( typeAlias -> {
466
458
targetConfiguration .getTypeAliasRegistry ().registerAlias (typeAlias );
467
459
LOGGER .debug (() -> "Registered type alias: '" + typeAlias + "'" );
468
- }
460
+ });
469
461
}
470
462
471
463
if (!isEmpty (this .plugins )) {
472
- for ( Interceptor plugin : this .plugins ) {
464
+ Stream . of ( this .plugins ). forEach ( plugin -> {
473
465
targetConfiguration .addInterceptor (plugin );
474
466
LOGGER .debug (() -> "Registered plugin: '" + plugin + "'" );
475
- }
467
+ });
476
468
}
477
469
478
470
if (hasLength (this .typeHandlersPackage )) {
479
471
String [] typeHandlersPackageArray = tokenizeToStringArray (this .typeHandlersPackage ,
480
472
ConfigurableApplicationContext .CONFIG_LOCATION_DELIMITERS );
481
- for ( String packageToScan : typeHandlersPackageArray ) {
473
+ Stream . of ( typeHandlersPackageArray ). forEach ( packageToScan -> {
482
474
targetConfiguration .getTypeHandlerRegistry ().register (packageToScan );
483
475
LOGGER .debug (() -> "Scanned package: '" + packageToScan + "' for type handlers" );
484
- }
476
+ });
485
477
}
486
478
487
479
if (!isEmpty (this .typeHandlers )) {
488
- for ( TypeHandler <?> typeHandler : this .typeHandlers ) {
480
+ Stream . of ( this .typeHandlers ). forEach ( typeHandler -> {
489
481
targetConfiguration .getTypeHandlerRegistry ().register (typeHandler );
490
482
LOGGER .debug (() -> "Registered type handler: '" + typeHandler + "'" );
491
- }
483
+ });
492
484
}
493
485
494
486
if (this .databaseIdProvider != null ) {//fix #64 set databaseId before parse mapper xmls
@@ -499,9 +491,7 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
499
491
}
500
492
}
501
493
502
- if (this .cache != null ) {
503
- targetConfiguration .addCache (this .cache );
504
- }
494
+ Optional .ofNullable (this .cache ).ifPresent (targetConfiguration ::addCache );
505
495
506
496
if (xmlConfigBuilder != null ) {
507
497
try {
@@ -514,11 +504,9 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
514
504
}
515
505
}
516
506
517
- if (this .transactionFactory == null ) {
518
- this .transactionFactory = new SpringManagedTransactionFactory ();
519
- }
520
-
521
- targetConfiguration .setEnvironment (new Environment (this .environment , this .transactionFactory , this .dataSource ));
507
+ targetConfiguration .setEnvironment (new Environment (this .environment ,
508
+ this .transactionFactory == null ? new SpringManagedTransactionFactory () : this .transactionFactory ,
509
+ this .dataSource ));
522
510
523
511
if (!isEmpty (this .mapperLocations )) {
524
512
for (Resource mapperLocation : this .mapperLocations ) {
0 commit comments