Skip to content

Commit b126624

Browse files
committed
Merge pull request mybatis#377 from kazuki43zoo/issues/376_support-defaultFetchSize
mybatis#376: Support the defaultFetchSize in config.xml
2 parents 6bb0071 + e84263b commit b126624

File tree

7 files changed

+138
-5
lines changed

7 files changed

+138
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ private void settingsElement(XNode context) throws Exception {
208208
configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
209209
configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
210210
configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
211+
configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));
211212
configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
212213
configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
213214
configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));

src/main/java/org/apache/ibatis/executor/statement/BaseStatementHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ protected void setFetchSize(Statement stmt) throws SQLException {
114114
Integer fetchSize = mappedStatement.getFetchSize();
115115
if (fetchSize != null) {
116116
stmt.setFetchSize(fetchSize);
117+
return;
118+
}
119+
Integer defaultFetchSize = configuration.getDefaultFetchSize();
120+
if (defaultFetchSize != null) {
121+
stmt.setFetchSize(defaultFetchSize);
117122
}
118123
}
119124

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public class Configuration {
110110
protected JdbcType jdbcTypeForNull = JdbcType.OTHER;
111111
protected Set<String> lazyLoadTriggerMethods = new HashSet<String>(Arrays.asList(new String[] { "equals", "clone", "hashCode", "toString" }));
112112
protected Integer defaultStatementTimeout;
113+
protected Integer defaultFetchSize;
113114
protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
114115
protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL;
115116

@@ -362,6 +363,14 @@ public void setDefaultStatementTimeout(Integer defaultStatementTimeout) {
362363
this.defaultStatementTimeout = defaultStatementTimeout;
363364
}
364365

366+
public Integer getDefaultFetchSize() {
367+
return defaultFetchSize;
368+
}
369+
370+
public void setDefaultFetchSize(Integer defaultFetchSize) {
371+
this.defaultFetchSize = defaultFetchSize;
372+
}
373+
365374
public boolean isUseColumnLabel() {
366375
return useColumnLabel;
367376
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2015 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<!DOCTYPE configuration
21+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
22+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
23+
24+
<configuration>
25+
26+
<settings>
27+
<setting name="autoMappingBehavior" value="NONE"/>
28+
<setting name="cacheEnabled" value="false"/>
29+
<setting name="proxyFactory" value="CGLIB"/>
30+
<setting name="lazyLoadingEnabled" value="true"/>
31+
<setting name="aggressiveLazyLoading" value="false"/>
32+
<setting name="multipleResultSetsEnabled" value="false"/>
33+
<setting name="useColumnLabel" value="false"/>
34+
<setting name="useGeneratedKeys" value="true"/>
35+
<setting name="defaultExecutorType" value="BATCH"/>
36+
<setting name="defaultStatementTimeout" value="10"/>
37+
<setting name="defaultFetchSize" value="100"/>
38+
<setting name="mapUnderscoreToCamelCase" value="true"/>
39+
<setting name="safeRowBoundsEnabled" value="true"/>
40+
<setting name="localCacheScope" value="STATEMENT"/>
41+
<setting name="jdbcTypeForNull" value="NULL"/>
42+
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString,xxx"/>
43+
<setting name="safeResultHandlerEnabled" value="false"/>
44+
<setting name="defaultScriptingLanguage" value="org.apache.ibatis.scripting.defaults.RawLanguageDriver"/>
45+
<setting name="callSettersOnNulls" value="true"/>
46+
<setting name="logPrefix" value="mybatis_"/>
47+
<setting name="logImpl" value="SLF4J"/>
48+
<setting name="configurationFactory" value="java.lang.String"/>
49+
</settings>
50+
51+
</configuration>

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

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,37 @@
1515
*/
1616
package org.apache.ibatis.builder;
1717

18-
import static org.junit.Assert.assertArrayEquals;
19-
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertTrue;
21-
2218
import java.io.InputStream;
2319
import java.io.StringReader;
2420
import java.sql.CallableStatement;
2521
import java.sql.PreparedStatement;
2622
import java.sql.ResultSet;
2723
import java.sql.SQLException;
24+
import java.util.Arrays;
25+
import java.util.HashSet;
26+
import java.util.Set;
2827

2928
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
29+
import org.apache.ibatis.executor.loader.cglib.CglibProxyFactory;
30+
import org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory;
3031
import org.apache.ibatis.io.Resources;
32+
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
33+
import org.apache.ibatis.scripting.defaults.RawLanguageDriver;
34+
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
35+
import org.apache.ibatis.session.AutoMappingBehavior;
3136
import org.apache.ibatis.session.Configuration;
37+
import org.apache.ibatis.session.ExecutorType;
38+
import org.apache.ibatis.session.LocalCacheScope;
3239
import org.apache.ibatis.type.BaseTypeHandler;
3340
import org.apache.ibatis.type.JdbcType;
3441
import org.apache.ibatis.type.TypeHandler;
3542
import org.apache.ibatis.type.TypeHandlerRegistry;
3643
import org.junit.Test;
3744

45+
import static org.hamcrest.core.Is.*;
46+
import static org.hamcrest.core.IsInstanceOf.*;
47+
import static org.junit.Assert.*;
48+
3849
public class XmlConfigBuilderTest {
3950

4051
@Test
@@ -44,6 +55,28 @@ public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception {
4455
XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
4556
Configuration config = builder.parse();
4657
assertNotNull(config);
58+
assertThat(config.getAutoMappingBehavior(), is(AutoMappingBehavior.PARTIAL));
59+
assertThat(config.isCacheEnabled(), is(true));
60+
assertThat(config.getProxyFactory(), is(instanceOf(JavassistProxyFactory.class)));
61+
assertThat(config.isLazyLoadingEnabled(), is(false));
62+
assertThat(config.isAggressiveLazyLoading(), is(true));
63+
assertThat(config.isMultipleResultSetsEnabled(), is(true));
64+
assertThat(config.isUseColumnLabel(), is(true));
65+
assertThat(config.isUseGeneratedKeys(), is(false));
66+
assertThat(config.getDefaultExecutorType(), is(ExecutorType.SIMPLE));
67+
assertNull(config.getDefaultStatementTimeout());
68+
assertNull(config.getDefaultFetchSize());
69+
assertThat(config.isMapUnderscoreToCamelCase(), is(false));
70+
assertThat(config.isSafeRowBoundsEnabled(), is(false));
71+
assertThat(config.getLocalCacheScope(), is(LocalCacheScope.SESSION));
72+
assertThat(config.getJdbcTypeForNull(), is(JdbcType.OTHER));
73+
assertThat(config.getLazyLoadTriggerMethods(), is((Set<String>) new HashSet<String>(Arrays.asList("equals", "clone", "hashCode", "toString"))));
74+
assertThat(config.isSafeResultHandlerEnabled(), is(true));
75+
assertThat(config.getDefaultScriptingLanuageInstance(), is(instanceOf(XMLLanguageDriver.class)));
76+
assertThat(config.isCallSettersOnNulls(), is(false));
77+
assertNull(config.getLogPrefix());
78+
assertNull(config.getLogImpl());
79+
assertNull(config.getConfigurationFactory());
4780
}
4881

4982
enum MyEnum {
@@ -102,4 +135,37 @@ public void registerJavaTypeInitializingTypeHandler() {
102135
assertTrue(typeHandler instanceof EnumOrderTypeHandler);
103136
assertArrayEquals(MyEnum.values(), ((EnumOrderTypeHandler) typeHandler).constants);
104137
}
138+
139+
@Test
140+
public void shouldSuccessfullyLoadXMLConfigFile() throws Exception {
141+
String resource = "org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml";
142+
InputStream inputStream = Resources.getResourceAsStream(resource);
143+
XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
144+
Configuration config = builder.parse();
145+
146+
assertThat(config.getAutoMappingBehavior(), is(AutoMappingBehavior.NONE));
147+
assertThat(config.isCacheEnabled(), is(false));
148+
assertThat(config.getProxyFactory(), is(instanceOf(CglibProxyFactory.class)));
149+
assertThat(config.isLazyLoadingEnabled(), is(true));
150+
assertThat(config.isAggressiveLazyLoading(), is(false));
151+
assertThat(config.isMultipleResultSetsEnabled(), is(false));
152+
assertThat(config.isUseColumnLabel(), is(false));
153+
assertThat(config.isUseGeneratedKeys(), is(true));
154+
assertThat(config.getDefaultExecutorType(), is(ExecutorType.BATCH));
155+
assertThat(config.getDefaultStatementTimeout(), is(10));
156+
assertThat(config.getDefaultFetchSize(), is(100));
157+
assertThat(config.isMapUnderscoreToCamelCase(), is(true));
158+
assertThat(config.isSafeRowBoundsEnabled(), is(true));
159+
assertThat(config.getLocalCacheScope(), is(LocalCacheScope.STATEMENT));
160+
assertThat(config.getJdbcTypeForNull(), is(JdbcType.NULL));
161+
assertThat(config.getLazyLoadTriggerMethods(), is((Set<String>) new HashSet<String>(Arrays.asList("equals", "clone", "hashCode", "toString", "xxx"))));
162+
assertThat(config.isSafeResultHandlerEnabled(), is(false));
163+
assertThat(config.getDefaultScriptingLanuageInstance(), is(instanceOf(RawLanguageDriver.class)));
164+
assertThat(config.isCallSettersOnNulls(), is(true));
165+
assertThat(config.getLogPrefix(), is("mybatis_"));
166+
assertThat(config.getLogImpl().getName(), is(Slf4jImpl.class.getName()));
167+
assertThat(config.getConfigurationFactory().getName(), is(String.class.getName()));
168+
169+
}
170+
105171
}

src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public BaseExecutorTest() {
5757
config.setMultipleResultSetsEnabled(true);
5858
config.setUseColumnLabel(true);
5959
config.setDefaultStatementTimeout(5000);
60+
config.setDefaultFetchSize(100);
6061
}
6162

6263
@Test

src/test/java/org/apache/ibatis/executor/ExecutorTestHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public static MappedStatement prepareSelectAllAuthorsAutoMappedStatement(final C
197197
}
198198
}).build());
199199
}
200-
}).build();
200+
}).fetchSize(1000).build();
201201
}
202202

203203
public static MappedStatement prepareSelectOneAuthorMappedStatementWithConstructorResults(final Configuration config) {

0 commit comments

Comments
 (0)