Skip to content

Commit 56c3013

Browse files
committed
Adjustment for mybatis/mybatis-3#2760
1 parent 287fce2 commit 56c3013

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/main/java/org/mybatis/scripting/freemarker/FreeMarkerSqlSource.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2025 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.
@@ -19,11 +19,15 @@
1919
import java.io.IOException;
2020
import java.util.ArrayList;
2121
import java.util.HashMap;
22+
import java.util.List;
2223
import java.util.Map;
2324

25+
import org.apache.ibatis.builder.ParameterMappingTokenHandler;
2426
import org.apache.ibatis.builder.SqlSourceBuilder;
2527
import org.apache.ibatis.mapping.BoundSql;
28+
import org.apache.ibatis.mapping.ParameterMapping;
2629
import org.apache.ibatis.mapping.SqlSource;
30+
import org.apache.ibatis.parsing.GenericTokenParser;
2731
import org.apache.ibatis.session.Configuration;
2832

2933
import freemarker.template.Template;
@@ -110,9 +114,18 @@ public BoundSql getBoundSql(Object parameterObject) {
110114
}
111115

112116
// Pass retrieved SQL into MyBatis engine, it will substitute prepared-statements parameters
113-
SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
114-
Class<?> parameterType1 = parameterObject == null ? Object.class : parameterObject.getClass();
115-
SqlSource sqlSource = sqlSourceParser.parse(sql, parameterType1, new HashMap<String, Object>());
117+
SqlSource sqlSource = parse(configuration, sql, parameterObject, new HashMap<String, Object>());
116118
return sqlSource.getBoundSql(parameterObject);
117119
}
120+
121+
private static SqlSource parse(Configuration configuration, String originalSql, Object parameterObject,
122+
Map<String, Object> additionalParameters) {
123+
Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
124+
List<ParameterMapping> parameterMappings = new ArrayList<>();
125+
ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(parameterMappings, configuration,
126+
parameterObject, parameterType, additionalParameters, true);
127+
GenericTokenParser parser = new GenericTokenParser("#{", "}", handler);
128+
return SqlSourceBuilder.buildSqlSource(configuration, parser.parse(originalSql), parameterMappings);
129+
}
130+
118131
}

0 commit comments

Comments
 (0)