Skip to content

Commit 31a6a81

Browse files
committed
Add defaultResultSetType as global configuration
1 parent 6f9105e commit 31a6a81

File tree

10 files changed

+174
-4
lines changed

10 files changed

+174
-4
lines changed

src/main/java/org/apache/ibatis/annotations/Options.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 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.
@@ -48,7 +48,7 @@ public enum FlushCachePolicy {
4848

4949
FlushCachePolicy flushCache() default FlushCachePolicy.DEFAULT;
5050

51-
ResultSetType resultSetType() default ResultSetType.FORWARD_ONLY;
51+
ResultSetType resultSetType() default ResultSetType.DEFAULT;
5252

5353
StatementType statementType() default StatementType.PREPARED;
5454

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ void parseStatement(Method method) {
293293
Integer fetchSize = null;
294294
Integer timeout = null;
295295
StatementType statementType = StatementType.PREPARED;
296-
ResultSetType resultSetType = ResultSetType.FORWARD_ONLY;
296+
ResultSetType resultSetType = configuration.getDefaultResultSetType();
297297
SqlCommandType sqlCommandType = getSqlCommandType(method);
298298
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
299299
boolean flushCache = !isSelect;
@@ -329,7 +329,9 @@ void parseStatement(Method method) {
329329
fetchSize = options.fetchSize() > -1 || options.fetchSize() == Integer.MIN_VALUE ? options.fetchSize() : null; //issue #348
330330
timeout = options.timeout() > -1 ? options.timeout() : null;
331331
statementType = options.statementType();
332-
resultSetType = options.resultSetType();
332+
if (options.resultSetType() != ResultSetType.DEFAULT) {
333+
resultSetType = options.resultSetType();
334+
}
333335
}
334336

335337
String resultMapId = null;

src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ private void settingsElement(Properties props) throws Exception {
248248
configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
249249
configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
250250
configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));
251+
configuration.setDefaultResultSetType(resolveResultSetType(props.getProperty("defaultResultSetType")));
251252
configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
252253
configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
253254
configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));

src/main/java/org/apache/ibatis/builder/xml/XMLStatementBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public void parseStatementNode() {
7575
String resultSetType = context.getStringAttribute("resultSetType");
7676
StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType", StatementType.PREPARED.toString()));
7777
ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);
78+
if (resultSetTypeEnum == null) {
79+
resultSetTypeEnum = configuration.getDefaultResultSetType();
80+
}
7881

7982
String nodeName = context.getNode().getNodeName();
8083
SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH));

src/main/java/org/apache/ibatis/mapping/ResultSetType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
* @author Clinton Begin
2222
*/
2323
public enum ResultSetType {
24+
/**
25+
* Special value that indicate to use default value.
26+
* @since 3.4.5
27+
*/
28+
DEFAULT(-1),
2429
FORWARD_ONLY(ResultSet.TYPE_FORWARD_ONLY),
2530
SCROLL_INSENSITIVE(ResultSet.TYPE_SCROLL_INSENSITIVE),
2631
SCROLL_SENSITIVE(ResultSet.TYPE_SCROLL_SENSITIVE);

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.apache.ibatis.mapping.MappedStatement;
6969
import org.apache.ibatis.mapping.ParameterMap;
7070
import org.apache.ibatis.mapping.ResultMap;
71+
import org.apache.ibatis.mapping.ResultSetType;
7172
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
7273
import org.apache.ibatis.parsing.XNode;
7374
import org.apache.ibatis.plugin.Interceptor;
@@ -118,6 +119,7 @@ public class Configuration {
118119
protected Set<String> lazyLoadTriggerMethods = new HashSet<String>(Arrays.asList(new String[] { "equals", "clone", "hashCode", "toString" }));
119120
protected Integer defaultStatementTimeout;
120121
protected Integer defaultFetchSize;
122+
protected ResultSetType defaultResultSetType;
121123
protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
122124
protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL;
123125
protected AutoMappingUnknownColumnBehavior autoMappingUnknownColumnBehavior = AutoMappingUnknownColumnBehavior.NONE;
@@ -426,6 +428,20 @@ public void setDefaultFetchSize(Integer defaultFetchSize) {
426428
this.defaultFetchSize = defaultFetchSize;
427429
}
428430

431+
/**
432+
* @since 3.4.5
433+
*/
434+
public ResultSetType getDefaultResultSetType() {
435+
return defaultResultSetType;
436+
}
437+
438+
/**
439+
* @since 3.4.5
440+
*/
441+
public void setDefaultResultSetType(ResultSetType defaultResultSetType) {
442+
this.defaultResultSetType = defaultResultSetType;
443+
}
444+
429445
public boolean isUseColumnLabel() {
430446
return useColumnLabel;
431447
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* Copyright 2009-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.builder;
17+
18+
import org.apache.ibatis.annotations.Insert;
19+
import org.apache.ibatis.annotations.Options;
20+
import org.apache.ibatis.annotations.Select;
21+
import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder;
22+
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
23+
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.mapping.MappedStatement;
26+
import org.apache.ibatis.mapping.ResultSetType;
27+
import org.apache.ibatis.mapping.StatementType;
28+
import org.apache.ibatis.session.Configuration;
29+
import org.apache.ibatis.type.TypeHandler;
30+
import org.junit.Test;
31+
32+
import java.io.InputStream;
33+
import java.util.regex.Pattern;
34+
35+
import static com.googlecode.catchexception.apis.BDDCatchException.caughtException;
36+
import static com.googlecode.catchexception.apis.BDDCatchException.when;
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
import static org.assertj.core.api.BDDAssertions.then;
39+
40+
public class AnnotationMapperBuilderTest {
41+
42+
@Test
43+
public void withOptions() throws Exception {
44+
Configuration configuration = new Configuration();
45+
MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
46+
builder.parse();
47+
48+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptions");
49+
assertThat(mappedStatement.getFetchSize()).isEqualTo(200);
50+
assertThat(mappedStatement.getTimeout()).isEqualTo(10);
51+
assertThat(mappedStatement.getStatementType()).isEqualTo(StatementType.STATEMENT);
52+
assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
53+
assertThat(mappedStatement.isFlushCacheRequired()).isTrue();
54+
assertThat(mappedStatement.isUseCache()).isFalse();
55+
assertThat(mappedStatement.getResultSets()).containsExactly("resultSets");
56+
57+
mappedStatement = configuration.getMappedStatement("insertWithOptions");
58+
assertThat(mappedStatement.getKeyGenerator()).isInstanceOf(Jdbc3KeyGenerator.class);
59+
assertThat(mappedStatement.getKeyColumns()).containsExactly("key_column");
60+
assertThat(mappedStatement.getKeyProperties()).containsExactly("keyProperty");
61+
}
62+
63+
@Test
64+
public void withOptionsAndWithoutOptionsAttributesWhenSpecifyDefaultValue() throws Exception {
65+
Configuration configuration = new Configuration();
66+
configuration.setDefaultResultSetType(ResultSetType.SCROLL_INSENSITIVE);
67+
MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
68+
builder.parse();
69+
70+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptionsAndWithoutOptionsAttributes");
71+
assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
72+
}
73+
74+
75+
@Test
76+
public void withOptionsAndWithoutOptionsAttributesWhenNotSpecifyDefaultValue() throws Exception {
77+
Configuration configuration = new Configuration();
78+
MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
79+
builder.parse();
80+
81+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptionsAndWithoutOptionsAttributes");
82+
assertThat(mappedStatement.getResultSetType()).isNull();
83+
}
84+
85+
@Test
86+
public void withoutOptionsWhenSpecifyDefaultValue() throws Exception {
87+
Configuration configuration = new Configuration();
88+
configuration.setDefaultResultSetType(ResultSetType.SCROLL_INSENSITIVE);
89+
MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
90+
builder.parse();
91+
92+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithoutOptions");
93+
assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
94+
}
95+
96+
@Test
97+
public void withoutOptionsWhenNotSpecifyDefaultValue() throws Exception {
98+
Configuration configuration = new Configuration();
99+
MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
100+
builder.parse();
101+
102+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithoutOptions");
103+
assertThat(mappedStatement.getResultSetType()).isNull();
104+
}
105+
106+
public interface Mapper {
107+
108+
@Insert("insert into test (name) values(#{name})")
109+
@Options(useGeneratedKeys = true, keyColumn = "key_column", keyProperty = "keyProperty")
110+
void insertWithOptions(String name);
111+
112+
@Select("select * from test")
113+
@Options(fetchSize = 200, timeout = 10, statementType = StatementType.STATEMENT, resultSetType = ResultSetType.SCROLL_INSENSITIVE, flushCache = Options.FlushCachePolicy.TRUE, useCache = false, resultSets = "resultSets")
114+
String selectWithOptions(Integer id);
115+
116+
@Select("select * from test")
117+
@Options
118+
String selectWithOptionsAndWithoutOptionsAttributes(Integer id);
119+
120+
@Select("select * from test")
121+
String selectWithoutOptions(Integer id);
122+
123+
}
124+
125+
}

src/test/java/org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<setting name="defaultExecutorType" value="BATCH"/>
4141
<setting name="defaultStatementTimeout" value="10"/>
4242
<setting name="defaultFetchSize" value="100"/>
43+
<setting name="defaultResultSetType" value="SCROLL_INSENSITIVE"/>
4344
<setting name="mapUnderscoreToCamelCase" value="true"/>
4445
<setting name="safeRowBoundsEnabled" value="true"/>
4546
<setting name="localCacheScope" value="STATEMENT"/>

src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.ibatis.io.Resources;
4343
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
4444
import org.apache.ibatis.mapping.Environment;
45+
import org.apache.ibatis.mapping.ResultSetType;
4546
import org.apache.ibatis.scripting.defaults.RawLanguageDriver;
4647
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
4748
import org.apache.ibatis.session.AutoMappingBehavior;
@@ -88,6 +89,7 @@ public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception {
8889
assertThat(config.getDefaultExecutorType()).isEqualTo(ExecutorType.SIMPLE);
8990
assertNull(config.getDefaultStatementTimeout());
9091
assertNull(config.getDefaultFetchSize());
92+
assertNull(config.getDefaultResultSetType());
9193
assertThat(config.isMapUnderscoreToCamelCase()).isFalse();
9294
assertThat(config.isSafeRowBoundsEnabled()).isFalse();
9395
assertThat(config.getLocalCacheScope()).isEqualTo(LocalCacheScope.SESSION);
@@ -181,6 +183,7 @@ public void shouldSuccessfullyLoadXMLConfigFile() throws Exception {
181183
assertThat(config.getDefaultExecutorType()).isEqualTo(ExecutorType.BATCH);
182184
assertThat(config.getDefaultStatementTimeout()).isEqualTo(10);
183185
assertThat(config.getDefaultFetchSize()).isEqualTo(100);
186+
assertThat(config.getDefaultResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
184187
assertThat(config.isMapUnderscoreToCamelCase()).isTrue();
185188
assertThat(config.isSafeRowBoundsEnabled()).isTrue();
186189
assertThat(config.getLocalCacheScope()).isEqualTo(LocalCacheScope.STATEMENT);

src/test/java/org/apache/ibatis/builder/XmlMapperBuilderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ public void mappedStatementWithOptions() throws Exception {
6262
inputStream.close();
6363
}
6464

65+
@Test
66+
public void mappedStatementWithoutOptionsWhenSpecifyDefaultValue() throws Exception {
67+
Configuration configuration = new Configuration();
68+
configuration.setDefaultResultSetType(ResultSetType.SCROLL_INSENSITIVE);
69+
String resource = "org/apache/ibatis/builder/AuthorMapper.xml";
70+
InputStream inputStream = Resources.getResourceAsStream(resource);
71+
XMLMapperBuilder builder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
72+
builder.parse();
73+
inputStream.close();
74+
75+
MappedStatement mappedStatement = configuration.getMappedStatement("selectAuthor");
76+
assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
77+
}
78+
6579
@Test
6680
public void parseExpression() {
6781
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};

0 commit comments

Comments
 (0)