From 56c3013fdfc291f4436d9e33a74f14086685170d Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Sat, 31 Dec 2022 14:31:25 +0900 Subject: [PATCH] Adjustment for https://github.com/mybatis/mybatis-3/pull/2760 --- .../freemarker/FreeMarkerSqlSource.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/mybatis/scripting/freemarker/FreeMarkerSqlSource.java b/src/main/java/org/mybatis/scripting/freemarker/FreeMarkerSqlSource.java index a546673..778525d 100644 --- a/src/main/java/org/mybatis/scripting/freemarker/FreeMarkerSqlSource.java +++ b/src/main/java/org/mybatis/scripting/freemarker/FreeMarkerSqlSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2022 the original author or authors. + * Copyright 2015-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,11 +19,15 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.apache.ibatis.builder.ParameterMappingTokenHandler; import org.apache.ibatis.builder.SqlSourceBuilder; import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.ParameterMapping; import org.apache.ibatis.mapping.SqlSource; +import org.apache.ibatis.parsing.GenericTokenParser; import org.apache.ibatis.session.Configuration; import freemarker.template.Template; @@ -110,9 +114,18 @@ public BoundSql getBoundSql(Object parameterObject) { } // Pass retrieved SQL into MyBatis engine, it will substitute prepared-statements parameters - SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration); - Class parameterType1 = parameterObject == null ? Object.class : parameterObject.getClass(); - SqlSource sqlSource = sqlSourceParser.parse(sql, parameterType1, new HashMap()); + SqlSource sqlSource = parse(configuration, sql, parameterObject, new HashMap()); return sqlSource.getBoundSql(parameterObject); } + + private static SqlSource parse(Configuration configuration, String originalSql, Object parameterObject, + Map additionalParameters) { + Class parameterType = parameterObject == null ? Object.class : parameterObject.getClass(); + List parameterMappings = new ArrayList<>(); + ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(parameterMappings, configuration, + parameterObject, parameterType, additionalParameters, true); + GenericTokenParser parser = new GenericTokenParser("#{", "}", handler); + return SqlSourceBuilder.buildSqlSource(configuration, parser.parse(originalSql), parameterMappings); + } + }