Skip to content

Commit b4527d1

Browse files
committed
#503 Merged into 3.3.x branch
1 parent 599dcf9 commit b4527d1

File tree

4 files changed

+76
-95
lines changed

4 files changed

+76
-95
lines changed

src/main/java/org/apache/ibatis/builder/MapperBuilderAssistant.java

Lines changed: 71 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,9 @@ public Cache useNewCache(Class<? extends Cache> typeClass,
128128
boolean readWrite,
129129
boolean blocking,
130130
Properties props) {
131-
typeClass = valueOrDefault(typeClass, PerpetualCache.class);
132-
evictionClass = valueOrDefault(evictionClass, LruCache.class);
133131
Cache cache = new CacheBuilder(currentNamespace)
134-
.implementation(typeClass)
135-
.addDecorator(evictionClass)
132+
.implementation(valueOrDefault(typeClass, PerpetualCache.class))
133+
.addDecorator(valueOrDefault(evictionClass, LruCache.class))
136134
.clearInterval(flushInterval)
137135
.size(size)
138136
.readWrite(readWrite)
@@ -146,8 +144,7 @@ public Cache useNewCache(Class<? extends Cache> typeClass,
146144

147145
public ParameterMap addParameterMap(String id, Class<?> parameterClass, List<ParameterMapping> parameterMappings) {
148146
id = applyCurrentNamespace(id, false);
149-
ParameterMap.Builder parameterMapBuilder = new ParameterMap.Builder(configuration, id, parameterClass, parameterMappings);
150-
ParameterMap parameterMap = parameterMapBuilder.build();
147+
ParameterMap parameterMap = new ParameterMap.Builder(configuration, id, parameterClass, parameterMappings).build();
151148
configuration.addParameterMap(parameterMap);
152149
return parameterMap;
153150
}
@@ -167,13 +164,13 @@ public ParameterMapping buildParameterMapping(
167164
Class<?> javaTypeClass = resolveParameterJavaType(parameterType, property, javaType, jdbcType);
168165
TypeHandler<?> typeHandlerInstance = resolveTypeHandler(javaTypeClass, typeHandler);
169166

170-
ParameterMapping.Builder builder = new ParameterMapping.Builder(configuration, property, javaTypeClass);
171-
builder.jdbcType(jdbcType);
172-
builder.resultMapId(resultMap);
173-
builder.mode(parameterMode);
174-
builder.numericScale(numericScale);
175-
builder.typeHandler(typeHandlerInstance);
176-
return builder.build();
167+
return new ParameterMapping.Builder(configuration, property, javaTypeClass)
168+
.jdbcType(jdbcType)
169+
.resultMapId(resultMap)
170+
.mode(parameterMode)
171+
.numericScale(numericScale)
172+
.typeHandler(typeHandlerInstance)
173+
.build();
177174
}
178175

179176
public ResultMap addResultMap(
@@ -186,7 +183,6 @@ public ResultMap addResultMap(
186183
id = applyCurrentNamespace(id, false);
187184
extend = applyCurrentNamespace(extend, true);
188185

189-
ResultMap.Builder resultMapBuilder = new ResultMap.Builder(configuration, id, type, resultMappings, autoMapping);
190186
if (extend != null) {
191187
if (!configuration.hasResultMap(extend)) {
192188
throw new IncompleteElementException("Could not find a parent resultmap with id '" + extend + "'");
@@ -212,8 +208,9 @@ public ResultMap addResultMap(
212208
}
213209
resultMappings.addAll(extendedResultMappings);
214210
}
215-
resultMapBuilder.discriminator(discriminator);
216-
ResultMap resultMap = resultMapBuilder.build();
211+
ResultMap resultMap = new ResultMap.Builder(configuration, id, type, resultMappings, autoMapping)
212+
.discriminator(discriminator)
213+
.build();
217214
configuration.addResultMap(resultMap);
218215
return resultMap;
219216
}
@@ -246,8 +243,7 @@ public Discriminator buildDiscriminator(
246243
resultMap = applyCurrentNamespace(resultMap, true);
247244
namespaceDiscriminatorMap.put(e.getKey(), resultMap);
248245
}
249-
Discriminator.Builder discriminatorBuilder = new Discriminator.Builder(configuration, resultMapping, namespaceDiscriminatorMap);
250-
return discriminatorBuilder.build();
246+
return new Discriminator.Builder(configuration, resultMapping, namespaceDiscriminatorMap).build();
251247
}
252248

253249
public MappedStatement addMappedStatement(
@@ -279,22 +275,28 @@ public MappedStatement addMappedStatement(
279275
id = applyCurrentNamespace(id, false);
280276
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
281277

282-
MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType);
283-
statementBuilder.resource(resource);
284-
statementBuilder.fetchSize(fetchSize);
285-
statementBuilder.statementType(statementType);
286-
statementBuilder.keyGenerator(keyGenerator);
287-
statementBuilder.keyProperty(keyProperty);
288-
statementBuilder.keyColumn(keyColumn);
289-
statementBuilder.databaseId(databaseId);
290-
statementBuilder.lang(lang);
291-
statementBuilder.resultOrdered(resultOrdered);
292-
statementBuilder.resulSets(resultSets);
293-
setStatementTimeout(timeout, statementBuilder);
294-
295-
setStatementParameterMap(parameterMap, parameterType, statementBuilder);
296-
setStatementResultMap(resultMap, resultType, resultSetType, statementBuilder);
297-
setStatementCache(isSelect, flushCache, useCache, currentCache, statementBuilder);
278+
MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType)
279+
.resource(resource)
280+
.fetchSize(fetchSize)
281+
.timeout(timeout)
282+
.statementType(statementType)
283+
.keyGenerator(keyGenerator)
284+
.keyProperty(keyProperty)
285+
.keyColumn(keyColumn)
286+
.databaseId(databaseId)
287+
.lang(lang)
288+
.resultOrdered(resultOrdered)
289+
.resulSets(resultSets)
290+
.resultMaps(getStatementResultMaps(resultMap, resultType, id))
291+
.resultSetType(resultSetType)
292+
.flushCacheRequired(valueOrDefault(flushCache, !isSelect))
293+
.useCache(valueOrDefault(useCache, isSelect))
294+
.cache(currentCache);
295+
296+
ParameterMap statementParameterMap = getStatementParameterMap(parameterMap, parameterType, id);
297+
if (statementParameterMap != null) {
298+
statementBuilder.parameterMap(statementParameterMap);
299+
}
298300

299301
MappedStatement statement = statementBuilder.build();
300302
configuration.addMappedStatement(statement);
@@ -305,47 +307,33 @@ private <T> T valueOrDefault(T value, T defaultValue) {
305307
return value == null ? defaultValue : value;
306308
}
307309

308-
private void setStatementCache(
309-
boolean isSelect,
310-
boolean flushCache,
311-
boolean useCache,
312-
Cache cache,
313-
MappedStatement.Builder statementBuilder) {
314-
flushCache = valueOrDefault(flushCache, !isSelect);
315-
useCache = valueOrDefault(useCache, isSelect);
316-
statementBuilder.flushCacheRequired(flushCache);
317-
statementBuilder.useCache(useCache);
318-
statementBuilder.cache(cache);
319-
}
320-
321-
private void setStatementParameterMap(
322-
String parameterMap,
310+
private ParameterMap getStatementParameterMap(
311+
String parameterMapName,
323312
Class<?> parameterTypeClass,
324-
MappedStatement.Builder statementBuilder) {
325-
parameterMap = applyCurrentNamespace(parameterMap, true);
326-
327-
if (parameterMap != null) {
313+
String statementId) {
314+
parameterMapName = applyCurrentNamespace(parameterMapName, true);
315+
ParameterMap parameterMap = null;
316+
if (parameterMapName != null) {
328317
try {
329-
statementBuilder.parameterMap(configuration.getParameterMap(parameterMap));
318+
parameterMap = configuration.getParameterMap(parameterMapName);
330319
} catch (IllegalArgumentException e) {
331-
throw new IncompleteElementException("Could not find parameter map " + parameterMap, e);
320+
throw new IncompleteElementException("Could not find parameter map " + parameterMapName, e);
332321
}
333322
} else if (parameterTypeClass != null) {
334323
List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
335-
ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder(
324+
parameterMap = new ParameterMap.Builder(
336325
configuration,
337-
statementBuilder.id() + "-Inline",
326+
statementId + "-Inline",
338327
parameterTypeClass,
339-
parameterMappings);
340-
statementBuilder.parameterMap(inlineParameterMapBuilder.build());
328+
parameterMappings).build();
341329
}
330+
return parameterMap;
342331
}
343332

344-
private void setStatementResultMap(
333+
private List<ResultMap> getStatementResultMaps(
345334
String resultMap,
346335
Class<?> resultType,
347-
ResultSetType resultSetType,
348-
MappedStatement.Builder statementBuilder) {
336+
String statementId) {
349337
resultMap = applyCurrentNamespace(resultMap, true);
350338

351339
List<ResultMap> resultMaps = new ArrayList<ResultMap>();
@@ -359,24 +347,15 @@ private void setStatementResultMap(
359347
}
360348
}
361349
} else if (resultType != null) {
362-
ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(
350+
ResultMap inlineResultMap = new ResultMap.Builder(
363351
configuration,
364-
statementBuilder.id() + "-Inline",
352+
statementId + "-Inline",
365353
resultType,
366354
new ArrayList<ResultMapping>(),
367-
null);
368-
resultMaps.add(inlineResultMapBuilder.build());
369-
}
370-
statementBuilder.resultMaps(resultMaps);
371-
372-
statementBuilder.resultSetType(resultSetType);
373-
}
374-
375-
private void setStatementTimeout(Integer timeout, MappedStatement.Builder statementBuilder) {
376-
if (timeout == null) {
377-
timeout = configuration.getDefaultStatementTimeout();
355+
null).build();
356+
resultMaps.add(inlineResultMap);
378357
}
379-
statementBuilder.timeout(timeout);
358+
return resultMaps;
380359
}
381360

382361
public ResultMapping buildResultMapping(
@@ -400,19 +379,19 @@ public ResultMapping buildResultMapping(
400379
if (composites.size() > 0) {
401380
column = null;
402381
}
403-
ResultMapping.Builder builder = new ResultMapping.Builder(configuration, property, column, javaTypeClass);
404-
builder.jdbcType(jdbcType);
405-
builder.nestedQueryId(applyCurrentNamespace(nestedSelect, true));
406-
builder.nestedResultMapId(applyCurrentNamespace(nestedResultMap, true));
407-
builder.resultSet(resultSet);
408-
builder.typeHandler(typeHandlerInstance);
409-
builder.flags(flags == null ? new ArrayList<ResultFlag>() : flags);
410-
builder.composites(composites);
411-
builder.notNullColumns(parseMultipleColumnNames(notNullColumn));
412-
builder.columnPrefix(columnPrefix);
413-
builder.foreignColumn(foreignColumn);
414-
builder.lazy(lazy);
415-
return builder.build();
382+
return new ResultMapping.Builder(configuration, property, column, javaTypeClass)
383+
.jdbcType(jdbcType)
384+
.nestedQueryId(applyCurrentNamespace(nestedSelect, true))
385+
.nestedResultMapId(applyCurrentNamespace(nestedResultMap, true))
386+
.resultSet(resultSet)
387+
.typeHandler(typeHandlerInstance)
388+
.flags(flags == null ? new ArrayList<ResultFlag>() : flags)
389+
.composites(composites)
390+
.notNullColumns(parseMultipleColumnNames(notNullColumn))
391+
.columnPrefix(columnPrefix)
392+
.foreignColumn(foreignColumn)
393+
.lazy(lazy)
394+
.build();
416395
}
417396

418397
private Set<String> parseMultipleColumnNames(String columnName) {
@@ -438,8 +417,9 @@ private List<ResultMapping> parseCompositeColumnName(String columnName) {
438417
while (parser.hasMoreTokens()) {
439418
String property = parser.nextToken();
440419
String column = parser.nextToken();
441-
ResultMapping.Builder complexBuilder = new ResultMapping.Builder(configuration, property, column, configuration.getTypeHandlerRegistry().getUnknownTypeHandler());
442-
composites.add(complexBuilder.build());
420+
ResultMapping complexResultMapping = new ResultMapping.Builder(
421+
configuration, property, column, configuration.getTypeHandlerRegistry().getUnknownTypeHandler()).build();
422+
composites.add(complexResultMapping);
443423
}
444424
}
445425
return composites;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ public Statement prepare(Connection connection) throws SQLException {
102102

103103
protected void setStatementTimeout(Statement stmt) throws SQLException {
104104
Integer timeout = mappedStatement.getTimeout();
105-
Integer defaultTimeout = configuration.getDefaultStatementTimeout();
106105
if (timeout != null) {
107106
stmt.setQueryTimeout(timeout);
108-
} else if (defaultTimeout != null) {
107+
return;
108+
}
109+
Integer defaultTimeout = configuration.getDefaultStatementTimeout();
110+
if (defaultTimeout != null) {
109111
stmt.setQueryTimeout(defaultTimeout);
110112
}
111113
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public Builder(Configuration configuration, String id, SqlSource sqlSource, SqlC
7171
mappedStatement.statementType = StatementType.PREPARED;
7272
mappedStatement.parameterMap = new ParameterMap.Builder(configuration, "defaultParameterMap", null, new ArrayList<ParameterMapping>()).build();
7373
mappedStatement.resultMaps = new ArrayList<ResultMap>();
74-
mappedStatement.timeout = configuration.getDefaultStatementTimeout();
7574
mappedStatement.sqlCommandType = sqlCommandType;
7675
mappedStatement.keyGenerator = configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType) ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
7776
String logId = id;

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-
}).fetchSize(1000).build();
200+
}).fetchSize(1000).timeout(2000).build();
201201
}
202202

203203
public static MappedStatement prepareSelectOneAuthorMappedStatementWithConstructorResults(final Configuration config) {

0 commit comments

Comments
 (0)