Skip to content

Commit 60b7a4b

Browse files
authored
Merge pull request mybatis#344 from kazuki43zoo/polish
Polish
2 parents c834135 + 66cca75 commit 60b7a4b

8 files changed

+46
-69
lines changed

src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java

+21-33
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import java.io.IOException;
2525
import java.sql.SQLException;
26+
import java.util.Optional;
2627
import java.util.Properties;
28+
import java.util.stream.Stream;
2729

2830
import javax.sql.DataSource;
2931

@@ -434,61 +436,51 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
434436
} else {
435437
LOGGER.debug(() -> "Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
436438
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);
444440
}
445441

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);
453445

454446
if (hasLength(this.typeAliasesPackage)) {
455447
String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
456448
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
457-
for (String packageToScan : typeAliasPackageArray) {
449+
Stream.of(typeAliasPackageArray).forEach(packageToScan -> {
458450
targetConfiguration.getTypeAliasRegistry().registerAliases(packageToScan,
459-
typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
451+
typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
460452
LOGGER.debug(() -> "Scanned package: '" + packageToScan + "' for aliases");
461-
}
453+
});
462454
}
463455

464456
if (!isEmpty(this.typeAliases)) {
465-
for (Class<?> typeAlias : this.typeAliases) {
457+
Stream.of(this.typeAliases).forEach(typeAlias -> {
466458
targetConfiguration.getTypeAliasRegistry().registerAlias(typeAlias);
467459
LOGGER.debug(() -> "Registered type alias: '" + typeAlias + "'");
468-
}
460+
});
469461
}
470462

471463
if (!isEmpty(this.plugins)) {
472-
for (Interceptor plugin : this.plugins) {
464+
Stream.of(this.plugins).forEach(plugin -> {
473465
targetConfiguration.addInterceptor(plugin);
474466
LOGGER.debug(() -> "Registered plugin: '" + plugin + "'");
475-
}
467+
});
476468
}
477469

478470
if (hasLength(this.typeHandlersPackage)) {
479471
String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
480472
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
481-
for (String packageToScan : typeHandlersPackageArray) {
473+
Stream.of(typeHandlersPackageArray).forEach(packageToScan -> {
482474
targetConfiguration.getTypeHandlerRegistry().register(packageToScan);
483475
LOGGER.debug(() -> "Scanned package: '" + packageToScan + "' for type handlers");
484-
}
476+
});
485477
}
486478

487479
if (!isEmpty(this.typeHandlers)) {
488-
for (TypeHandler<?> typeHandler : this.typeHandlers) {
480+
Stream.of(this.typeHandlers).forEach(typeHandler -> {
489481
targetConfiguration.getTypeHandlerRegistry().register(typeHandler);
490482
LOGGER.debug(() -> "Registered type handler: '" + typeHandler + "'");
491-
}
483+
});
492484
}
493485

494486
if (this.databaseIdProvider != null) {//fix #64 set databaseId before parse mapper xmls
@@ -499,9 +491,7 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
499491
}
500492
}
501493

502-
if (this.cache != null) {
503-
targetConfiguration.addCache(this.cache);
504-
}
494+
Optional.ofNullable(this.cache).ifPresent(targetConfiguration::addCache);
505495

506496
if (xmlConfigBuilder != null) {
507497
try {
@@ -514,11 +504,9 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
514504
}
515505
}
516506

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));
522510

523511
if (!isEmpty(this.mapperLocations)) {
524512
for (Resource mapperLocation : this.mapperLocations) {

src/main/java/org/mybatis/spring/SqlSessionTemplate.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2018 the original author or authors.
2+
* Copyright 2010-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -412,7 +412,7 @@ public List<BatchResult> flushStatements() {
412412
* @see "org.springframework.beans.factory.support.DisposableBeanAdapter#CLOSE_METHOD_NAME"
413413
*/
414414
@Override
415-
public void destroy() throws Exception {
415+
public void destroy() {
416416
//This method forces spring disposer to avoid call of SqlSessionTemplate.close() which gives UnsupportedOperationException
417417
}
418418

src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2010-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22+
import java.util.Optional;
2223
import java.util.stream.Collectors;
2324

2425
import org.mybatis.spring.mapper.ClassPathMapperScanner;
@@ -77,9 +78,7 @@ void registerBeanDefinitions(AnnotationAttributes annoAttrs, BeanDefinitionRegis
7778
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
7879

7980
// this check is needed in Spring 3.1
80-
if (resourceLoader != null) {
81-
scanner.setResourceLoader(resourceLoader);
82-
}
81+
Optional.ofNullable(resourceLoader).ifPresent(scanner::setResourceLoader);
8382

8483
Class<? extends Annotation> annotationClass = annoAttrs.getClass("annotationClass");
8584
if (!Annotation.class.equals(annotationClass)) {

src/main/java/org/mybatis/spring/batch/MyBatisCursorItemReader.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2010-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@ public MyBatisCursorItemReader() {
4949
}
5050

5151
@Override
52-
protected T doRead() throws Exception {
52+
protected T doRead() {
5353
T next = null;
5454
if (cursorIterator.hasNext()) {
5555
next = cursorIterator.next();
@@ -58,7 +58,7 @@ protected T doRead() throws Exception {
5858
}
5959

6060
@Override
61-
protected void doOpen() throws Exception {
61+
protected void doOpen() {
6262
Map<String, Object> parameters = new HashMap<>();
6363
if (parameterValues != null) {
6464
parameters.putAll(parameterValues);

src/main/java/org/mybatis/spring/batch/builder/MyBatisBatchItemWriterBuilder.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2010-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@
2020
import org.mybatis.spring.batch.MyBatisBatchItemWriter;
2121
import org.springframework.core.convert.converter.Converter;
2222

23+
import java.util.Optional;
24+
2325
/**
2426
* A builder for the {@link MyBatisBatchItemWriter}.
2527
*
@@ -107,12 +109,8 @@ public MyBatisBatchItemWriter<T> build() {
107109
writer.setSqlSessionTemplate(this.sqlSessionTemplate);
108110
writer.setSqlSessionFactory(this.sqlSessionFactory);
109111
writer.setStatementId(this.statementId);
110-
if (this.assertUpdates != null) {
111-
writer.setAssertUpdates(this.assertUpdates);
112-
}
113-
if (this.itemToParameterConverter != null) {
114-
writer.setItemToParameterConverter(this.itemToParameterConverter);
115-
}
112+
Optional.ofNullable(this.assertUpdates).ifPresent(writer::setAssertUpdates);
113+
Optional.ofNullable(this.itemToParameterConverter).ifPresent(writer::setItemToParameterConverter);
116114
return writer;
117115
}
118116

src/main/java/org/mybatis/spring/batch/builder/MyBatisCursorItemReaderBuilder.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2010-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import org.mybatis.spring.batch.MyBatisCursorItemReader;
2020

2121
import java.util.Map;
22+
import java.util.Optional;
2223

2324
/**
2425
* A builder for the {@link MyBatisCursorItemReader}.
@@ -107,12 +108,8 @@ public MyBatisCursorItemReader<T> build() {
107108
reader.setSqlSessionFactory(this.sqlSessionFactory);
108109
reader.setQueryId(this.queryId);
109110
reader.setParameterValues(this.parameterValues);
110-
if (this.saveState != null) {
111-
reader.setSaveState(saveState);
112-
}
113-
if (this.maxItemCount != null) {
114-
reader.setMaxItemCount(this.maxItemCount);
115-
}
111+
Optional.ofNullable(this.saveState).ifPresent(reader::setSaveState);
112+
Optional.ofNullable(this.maxItemCount).ifPresent(reader::setMaxItemCount);
116113
return reader;
117114
}
118115

src/main/java/org/mybatis/spring/batch/builder/MyBatisPagingItemReaderBuilder.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2010-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import org.mybatis.spring.batch.MyBatisPagingItemReader;
2020

2121
import java.util.Map;
22+
import java.util.Optional;
2223

2324
/**
2425
* A builder for the {@link MyBatisPagingItemReader}.
@@ -120,15 +121,9 @@ public MyBatisPagingItemReader<T> build() {
120121
reader.setSqlSessionFactory(this.sqlSessionFactory);
121122
reader.setQueryId(this.queryId);
122123
reader.setParameterValues(this.parameterValues);
123-
if (this.pageSize != null) {
124-
reader.setPageSize(this.pageSize);
125-
}
126-
if (this.saveState != null) {
127-
reader.setSaveState(saveState);
128-
}
129-
if (this.maxItemCount != null) {
130-
reader.setMaxItemCount(this.maxItemCount);
131-
}
124+
Optional.ofNullable(this.pageSize).ifPresent(reader::setPageSize);
125+
Optional.ofNullable(this.saveState).ifPresent(reader::setSaveState);
126+
Optional.ofNullable(this.maxItemCount).ifPresent(reader::setMaxItemCount);
132127
return reader;
133128
}
134129

src/main/java/org/mybatis/spring/transaction/SpringManagedTransaction.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2010-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -117,15 +117,15 @@ public void rollback() throws SQLException {
117117
* {@inheritDoc}
118118
*/
119119
@Override
120-
public void close() throws SQLException {
120+
public void close() {
121121
DataSourceUtils.releaseConnection(this.connection, this.dataSource);
122122
}
123123

124124
/**
125125
* {@inheritDoc}
126126
*/
127127
@Override
128-
public Integer getTimeout() throws SQLException {
128+
public Integer getTimeout() {
129129
ConnectionHolder holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
130130
if (holder != null && holder.hasTimeout()) {
131131
return holder.getTimeToLiveInSeconds();

0 commit comments

Comments
 (0)