diff --git a/src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java b/src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java index 1b90dd9a088..ecd74555936 100644 --- a/src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java +++ b/src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java @@ -206,6 +206,7 @@ private void settingsElement(XNode context) throws Exception { configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false)); configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE"))); configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null)); + configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null)); configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false)); configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false)); configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION"))); diff --git a/src/main/java/org/apache/ibatis/executor/statement/BaseStatementHandler.java b/src/main/java/org/apache/ibatis/executor/statement/BaseStatementHandler.java index 31629c4efb1..65e75d7ab88 100644 --- a/src/main/java/org/apache/ibatis/executor/statement/BaseStatementHandler.java +++ b/src/main/java/org/apache/ibatis/executor/statement/BaseStatementHandler.java @@ -111,6 +111,11 @@ protected void setFetchSize(Statement stmt) throws SQLException { Integer fetchSize = mappedStatement.getFetchSize(); if (fetchSize != null) { stmt.setFetchSize(fetchSize); + return; + } + Integer defaultFetchSize = configuration.getDefaultFetchSize(); + if (defaultFetchSize != null) { + stmt.setFetchSize(defaultFetchSize); } } diff --git a/src/main/java/org/apache/ibatis/session/Configuration.java b/src/main/java/org/apache/ibatis/session/Configuration.java index fdf87f964e1..e74800f28d6 100644 --- a/src/main/java/org/apache/ibatis/session/Configuration.java +++ b/src/main/java/org/apache/ibatis/session/Configuration.java @@ -109,6 +109,7 @@ public class Configuration { protected JdbcType jdbcTypeForNull = JdbcType.OTHER; protected Set lazyLoadTriggerMethods = new HashSet(Arrays.asList(new String[] { "equals", "clone", "hashCode", "toString" })); protected Integer defaultStatementTimeout; + protected Integer defaultFetchSize; protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE; protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL; @@ -362,6 +363,14 @@ public void setDefaultStatementTimeout(Integer defaultStatementTimeout) { this.defaultStatementTimeout = defaultStatementTimeout; } + public Integer getDefaultFetchSize() { + return defaultFetchSize; + } + + public void setDefaultFetchSize(Integer defaultFetchSize) { + this.defaultFetchSize = defaultFetchSize; + } + public boolean isUseColumnLabel() { return useColumnLabel; } diff --git a/src/test/java/org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml b/src/test/java/org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml new file mode 100644 index 00000000000..400c7009621 --- /dev/null +++ b/src/test/java/org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java b/src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java index f188c1c4cc5..9e07f9ce81f 100644 --- a/src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java +++ b/src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java @@ -15,26 +15,37 @@ */ package org.apache.ibatis.builder; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.io.InputStream; import java.io.StringReader; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.apache.ibatis.builder.xml.XMLConfigBuilder; +import org.apache.ibatis.executor.loader.cglib.CglibProxyFactory; +import org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory; import org.apache.ibatis.io.Resources; +import org.apache.ibatis.logging.slf4j.Slf4jImpl; +import org.apache.ibatis.scripting.defaults.RawLanguageDriver; +import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver; +import org.apache.ibatis.session.AutoMappingBehavior; import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.LocalCacheScope; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandlerRegistry; import org.junit.Test; +import static org.hamcrest.core.Is.*; +import static org.hamcrest.core.IsInstanceOf.*; +import static org.junit.Assert.*; + public class XmlConfigBuilderTest { @Test @@ -44,6 +55,28 @@ public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception { XMLConfigBuilder builder = new XMLConfigBuilder(inputStream); Configuration config = builder.parse(); assertNotNull(config); + assertThat(config.getAutoMappingBehavior(), is(AutoMappingBehavior.PARTIAL)); + assertThat(config.isCacheEnabled(), is(true)); + assertThat(config.getProxyFactory(), is(instanceOf(CglibProxyFactory.class))); + assertThat(config.isLazyLoadingEnabled(), is(false)); + assertThat(config.isAggressiveLazyLoading(), is(true)); + assertThat(config.isMultipleResultSetsEnabled(), is(true)); + assertThat(config.isUseColumnLabel(), is(true)); + assertThat(config.isUseGeneratedKeys(), is(false)); + assertThat(config.getDefaultExecutorType(), is(ExecutorType.SIMPLE)); + assertNull(config.getDefaultStatementTimeout()); + assertNull(config.getDefaultFetchSize()); + assertThat(config.isMapUnderscoreToCamelCase(), is(false)); + assertThat(config.isSafeRowBoundsEnabled(), is(false)); + assertThat(config.getLocalCacheScope(), is(LocalCacheScope.SESSION)); + assertThat(config.getJdbcTypeForNull(), is(JdbcType.OTHER)); + assertThat(config.getLazyLoadTriggerMethods(), is((Set) new HashSet(Arrays.asList("equals", "clone", "hashCode", "toString")))); + assertThat(config.isSafeResultHandlerEnabled(), is(true)); + assertThat(config.getDefaultScriptingLanuageInstance(), is(instanceOf(XMLLanguageDriver.class))); + assertThat(config.isCallSettersOnNulls(), is(false)); + assertNull(config.getLogPrefix()); + assertNull(config.getLogImpl()); + assertNull(config.getConfigurationFactory()); } enum MyEnum { @@ -102,4 +135,37 @@ public void registerJavaTypeInitializingTypeHandler() { assertTrue(typeHandler instanceof EnumOrderTypeHandler); assertArrayEquals(MyEnum.values(), ((EnumOrderTypeHandler) typeHandler).constants); } + + @Test + public void shouldSuccessfullyLoadXMLConfigFile() throws Exception { + String resource = "org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + XMLConfigBuilder builder = new XMLConfigBuilder(inputStream); + Configuration config = builder.parse(); + + assertThat(config.getAutoMappingBehavior(), is(AutoMappingBehavior.NONE)); + assertThat(config.isCacheEnabled(), is(false)); + assertThat(config.getProxyFactory(), is(instanceOf(JavassistProxyFactory.class))); + assertThat(config.isLazyLoadingEnabled(), is(true)); + assertThat(config.isAggressiveLazyLoading(), is(false)); + assertThat(config.isMultipleResultSetsEnabled(), is(false)); + assertThat(config.isUseColumnLabel(), is(false)); + assertThat(config.isUseGeneratedKeys(), is(true)); + assertThat(config.getDefaultExecutorType(), is(ExecutorType.BATCH)); + assertThat(config.getDefaultStatementTimeout(), is(10)); + assertThat(config.getDefaultFetchSize(), is(100)); + assertThat(config.isMapUnderscoreToCamelCase(), is(true)); + assertThat(config.isSafeRowBoundsEnabled(), is(true)); + assertThat(config.getLocalCacheScope(), is(LocalCacheScope.STATEMENT)); + assertThat(config.getJdbcTypeForNull(), is(JdbcType.NULL)); + assertThat(config.getLazyLoadTriggerMethods(), is((Set) new HashSet(Arrays.asList("equals", "clone", "hashCode", "toString", "xxx")))); + assertThat(config.isSafeResultHandlerEnabled(), is(false)); + assertThat(config.getDefaultScriptingLanuageInstance(), is(instanceOf(RawLanguageDriver.class))); + assertThat(config.isCallSettersOnNulls(), is(true)); + assertThat(config.getLogPrefix(), is("mybatis_")); + assertThat(config.getLogImpl().getName(), is(Slf4jImpl.class.getName())); + assertThat(config.getConfigurationFactory().getName(), is(String.class.getName())); + + } + } diff --git a/src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java b/src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java index 33e82971b41..9c491e08f1c 100644 --- a/src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java +++ b/src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java @@ -58,6 +58,7 @@ public BaseExecutorTest() { config.setMultipleResultSetsEnabled(true); config.setUseColumnLabel(true); config.setDefaultStatementTimeout(5000); + config.setDefaultFetchSize(100); } @Test diff --git a/src/test/java/org/apache/ibatis/executor/ExecutorTestHelper.java b/src/test/java/org/apache/ibatis/executor/ExecutorTestHelper.java index b812fdc7c68..0b596cac835 100644 --- a/src/test/java/org/apache/ibatis/executor/ExecutorTestHelper.java +++ b/src/test/java/org/apache/ibatis/executor/ExecutorTestHelper.java @@ -198,7 +198,7 @@ public static MappedStatement prepareSelectAllAuthorsAutoMappedStatement(final C } }).build()); } - }).build(); + }).fetchSize(1000).build(); } public static MappedStatement prepareSelectOneAuthorMappedStatementWithConstructorResults(final Configuration config) {