|
1 | 1 | /*
|
2 |
| - * Copyright 2015-2022 the original author or authors. |
| 2 | + * Copyright 2015-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.util.ArrayList;
|
21 | 21 | import java.util.HashMap;
|
| 22 | +import java.util.List; |
22 | 23 | import java.util.Map;
|
23 | 24 |
|
| 25 | +import org.apache.ibatis.builder.ParameterMappingTokenHandler; |
24 | 26 | import org.apache.ibatis.builder.SqlSourceBuilder;
|
25 | 27 | import org.apache.ibatis.mapping.BoundSql;
|
| 28 | +import org.apache.ibatis.mapping.ParameterMapping; |
26 | 29 | import org.apache.ibatis.mapping.SqlSource;
|
| 30 | +import org.apache.ibatis.parsing.GenericTokenParser; |
27 | 31 | import org.apache.ibatis.session.Configuration;
|
28 | 32 |
|
29 | 33 | import freemarker.template.Template;
|
@@ -110,9 +114,18 @@ public BoundSql getBoundSql(Object parameterObject) {
|
110 | 114 | }
|
111 | 115 |
|
112 | 116 | // 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>()); |
116 | 118 | return sqlSource.getBoundSql(parameterObject);
|
117 | 119 | }
|
| 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 | + |
118 | 131 | }
|
0 commit comments