Skip to content

Commit 6e9e4c2

Browse files
authored
Merge pull request #1056 from kazuki43zoo/default-resultset-type
Add defaultResultSetType as global configuration
2 parents 0fa5395 + 7765a86 commit 6e9e4c2

File tree

13 files changed

+228
-2
lines changed

13 files changed

+228
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ void parseStatement(Method method) {
306306
Integer fetchSize = null;
307307
Integer timeout = null;
308308
StatementType statementType = StatementType.PREPARED;
309-
ResultSetType resultSetType = null;
309+
ResultSetType resultSetType = configuration.getDefaultResultSetType();
310310
SqlCommandType sqlCommandType = getSqlCommandType(method);
311311
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
312312
boolean flushCache = !isSelect;
@@ -342,7 +342,9 @@ void parseStatement(Method method) {
342342
fetchSize = options.fetchSize() > -1 || options.fetchSize() == Integer.MIN_VALUE ? options.fetchSize() : null; //issue #348
343343
timeout = options.timeout() > -1 ? options.timeout() : null;
344344
statementType = options.statementType();
345-
resultSetType = options.resultSetType();
345+
if (options.resultSetType() != ResultSetType.DEFAULT) {
346+
resultSetType = options.resultSetType();
347+
}
346348
}
347349

348350
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
@@ -253,6 +253,7 @@ private void settingsElement(Properties props) {
253253
configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
254254
configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
255255
configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));
256+
configuration.setDefaultResultSetType(resolveResultSetType(props.getProperty("defaultResultSetType")));
256257
configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
257258
configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
258259
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
@@ -103,6 +103,9 @@ public void parseStatementNode() {
103103
String resultMap = context.getStringAttribute("resultMap");
104104
String resultSetType = context.getStringAttribute("resultSetType");
105105
ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);
106+
if (resultSetTypeEnum == null) {
107+
resultSetTypeEnum = configuration.getDefaultResultSetType();
108+
}
106109
String keyProperty = context.getStringAttribute("keyProperty");
107110
String keyColumn = context.getStringAttribute("keyColumn");
108111
String resultSets = context.getStringAttribute("resultSets");

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.apache.ibatis.mapping.MappedStatement;
7272
import org.apache.ibatis.mapping.ParameterMap;
7373
import org.apache.ibatis.mapping.ResultMap;
74+
import org.apache.ibatis.mapping.ResultSetType;
7475
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
7576
import org.apache.ibatis.parsing.XNode;
7677
import org.apache.ibatis.plugin.Interceptor;
@@ -121,6 +122,7 @@ public class Configuration {
121122
protected Set<String> lazyLoadTriggerMethods = new HashSet<>(Arrays.asList("equals", "clone", "hashCode", "toString"));
122123
protected Integer defaultStatementTimeout;
123124
protected Integer defaultFetchSize;
125+
protected ResultSetType defaultResultSetType;
124126
protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
125127
protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL;
126128
protected AutoMappingUnknownColumnBehavior autoMappingUnknownColumnBehavior = AutoMappingUnknownColumnBehavior.NONE;
@@ -431,6 +433,20 @@ public void setDefaultFetchSize(Integer defaultFetchSize) {
431433
this.defaultFetchSize = defaultFetchSize;
432434
}
433435

436+
/**
437+
* @since 3.5.2
438+
*/
439+
public ResultSetType getDefaultResultSetType() {
440+
return defaultResultSetType;
441+
}
442+
443+
/**
444+
* @since 3.5.2
445+
*/
446+
public void setDefaultResultSetType(ResultSetType defaultResultSetType) {
447+
this.defaultResultSetType = defaultResultSetType;
448+
}
449+
434450
public boolean isUseColumnLabel() {
435451
return useColumnLabel;
436452
}

src/site/es/xdoc/configuration.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
298298
Sin valor (null)
299299
</td>
300300
</tr>
301+
<tr>
302+
<td>
303+
defaultResultSetType
304+
</td>
305+
<td>
306+
Specifies a scroll strategy when omit it per statement settings. (Since: 3.5.2)
307+
</td>
308+
<td>
309+
FORWARD_ONLY | SCROLL_SENSITIVE | SCROLL_INSENSITIVE | DEFAULT(same behavior with 'Not Set')
310+
</td>
311+
<td>
312+
Not Set (null)
313+
</td>
314+
</tr>
301315
<tr>
302316
<td>
303317
safeRowBoundsEnabled

src/site/ja/xdoc/configuration.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
327327
なし (null)
328328
</td>
329329
</tr>
330+
<tr>
331+
<td>
332+
defaultResultSetType
333+
</td>
334+
<td>
335+
ステートメント毎の設定を省略した場合のスクロール方法を指定します。 (導入されたバージョン: 3.5.2)
336+
</td>
337+
<td>
338+
FORWARD_ONLY | SCROLL_SENSITIVE | SCROLL_INSENSITIVE | DEFAULT(指定しない時と同じ動作)
339+
</td>
340+
<td>
341+
なし (null)
342+
</td>
343+
</tr>
330344
<tr>
331345
<td>
332346
safeRowBoundsEnabled

src/site/ko/xdoc/configuration.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
307307
셋팅되지 않음(null)
308308
</td>
309309
</tr>
310+
<tr>
311+
<td>
312+
defaultResultSetType
313+
</td>
314+
<td>
315+
Specifies a scroll strategy when omit it per statement settings. (Since: 3.5.2)
316+
</td>
317+
<td>
318+
FORWARD_ONLY | SCROLL_SENSITIVE | SCROLL_INSENSITIVE | DEFAULT(same behavior with 'Not Set')
319+
</td>
320+
<td>
321+
Not Set (null)
322+
</td>
323+
</tr>
310324
<tr>
311325
<td>
312326
safeRowBoundsEnabled

src/site/xdoc/configuration.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,20 @@ SqlSessionFactory factory =
383383
Not Set (null)
384384
</td>
385385
</tr>
386+
<tr>
387+
<td>
388+
defaultResultSetType
389+
</td>
390+
<td>
391+
Specifies a scroll strategy when omit it per statement settings. (Since: 3.5.2)
392+
</td>
393+
<td>
394+
FORWARD_ONLY | SCROLL_SENSITIVE | SCROLL_INSENSITIVE | DEFAULT(same behavior with 'Not Set')
395+
</td>
396+
<td>
397+
Not Set (null)
398+
</td>
399+
</tr>
386400
<tr>
387401
<td>
388402
safeRowBoundsEnabled

src/site/zh/xdoc/configuration.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
316316
未设置 (null)
317317
</td>
318318
</tr>
319+
<tr>
320+
<td>
321+
defaultResultSetType
322+
</td>
323+
<td>
324+
Specifies a scroll strategy when omit it per statement settings. (Since: 3.5.2)
325+
</td>
326+
<td>
327+
FORWARD_ONLY | SCROLL_SENSITIVE | SCROLL_INSENSITIVE | DEFAULT(same behavior with 'Not Set')
328+
</td>
329+
<td>
330+
Not Set (null)
331+
</td>
332+
</tr>
319333
<tr>
320334
<td>
321335
safeRowBoundsEnabled
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* Copyright 2009-2019 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.executor.keygen.Jdbc3KeyGenerator;
23+
import org.apache.ibatis.mapping.MappedStatement;
24+
import org.apache.ibatis.mapping.ResultSetType;
25+
import org.apache.ibatis.mapping.StatementType;
26+
import org.apache.ibatis.session.Configuration;
27+
import org.junit.jupiter.api.Test;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
class AnnotationMapperBuilderTest {
32+
33+
@Test
34+
void withOptions() {
35+
Configuration configuration = new Configuration();
36+
MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
37+
builder.parse();
38+
39+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptions");
40+
assertThat(mappedStatement.getFetchSize()).isEqualTo(200);
41+
assertThat(mappedStatement.getTimeout()).isEqualTo(10);
42+
assertThat(mappedStatement.getStatementType()).isEqualTo(StatementType.STATEMENT);
43+
assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
44+
assertThat(mappedStatement.isFlushCacheRequired()).isTrue();
45+
assertThat(mappedStatement.isUseCache()).isFalse();
46+
assertThat(mappedStatement.getResultSets()).containsExactly("resultSets");
47+
48+
mappedStatement = configuration.getMappedStatement("insertWithOptions");
49+
assertThat(mappedStatement.getKeyGenerator()).isInstanceOf(Jdbc3KeyGenerator.class);
50+
assertThat(mappedStatement.getKeyColumns()).containsExactly("key_column");
51+
assertThat(mappedStatement.getKeyProperties()).containsExactly("keyProperty");
52+
}
53+
54+
@Test
55+
void withOptionsAndWithoutOptionsAttributesWhenSpecifyDefaultValue() {
56+
Configuration configuration = new Configuration();
57+
configuration.setDefaultResultSetType(ResultSetType.SCROLL_INSENSITIVE);
58+
MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
59+
builder.parse();
60+
61+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptionsAndWithoutOptionsAttributes");
62+
assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
63+
}
64+
65+
66+
@Test
67+
void withOptionsAndWithoutOptionsAttributesWhenNotSpecifyDefaultValue() {
68+
Configuration configuration = new Configuration();
69+
MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
70+
builder.parse();
71+
72+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptionsAndWithoutOptionsAttributes");
73+
assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.DEFAULT);
74+
}
75+
76+
@Test
77+
void withoutOptionsWhenSpecifyDefaultValue() {
78+
Configuration configuration = new Configuration();
79+
configuration.setDefaultResultSetType(ResultSetType.SCROLL_INSENSITIVE);
80+
MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
81+
builder.parse();
82+
83+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithoutOptions");
84+
assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
85+
}
86+
87+
@Test
88+
void withoutOptionsWhenNotSpecifyDefaultValue() {
89+
Configuration configuration = new Configuration();
90+
MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
91+
builder.parse();
92+
93+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithoutOptions");
94+
assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.DEFAULT);
95+
}
96+
97+
interface Mapper {
98+
99+
@Insert("insert into test (name) values(#{name})")
100+
@Options(useGeneratedKeys = true, keyColumn = "key_column", keyProperty = "keyProperty")
101+
void insertWithOptions(String name);
102+
103+
@Select("select * from test")
104+
@Options(fetchSize = 200, timeout = 10, statementType = StatementType.STATEMENT, resultSetType = ResultSetType.SCROLL_INSENSITIVE, flushCache = Options.FlushCachePolicy.TRUE, useCache = false, resultSets = "resultSets")
105+
String selectWithOptions(Integer id);
106+
107+
@Select("select * from test")
108+
@Options
109+
String selectWithOptionsAndWithoutOptionsAttributes(Integer id);
110+
111+
@Select("select * from test")
112+
String selectWithoutOptions(Integer id);
113+
114+
}
115+
116+
}

0 commit comments

Comments
 (0)