1
1
/*
2
- * Copyright 2018-2022 the original author or authors.
2
+ * Copyright 2018-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.
16
16
package org .mybatis .scripting .thymeleaf ;
17
17
18
18
import java .lang .reflect .InvocationTargetException ;
19
+ import java .util .ArrayList ;
19
20
import java .util .Arrays ;
20
21
import java .util .Collection ;
21
22
import java .util .HashMap ;
22
23
import java .util .HashSet ;
24
+ import java .util .List ;
23
25
import java .util .Locale ;
24
26
import java .util .Map ;
25
27
import java .util .Optional ;
26
28
import java .util .Properties ;
27
29
import java .util .Set ;
28
30
import java .util .function .BiFunction ;
29
31
32
+ import org .apache .ibatis .builder .ParameterMappingTokenHandler ;
30
33
import org .apache .ibatis .builder .SqlSourceBuilder ;
31
34
import org .apache .ibatis .mapping .BoundSql ;
35
+ import org .apache .ibatis .mapping .ParameterMapping ;
32
36
import org .apache .ibatis .mapping .SqlSource ;
37
+ import org .apache .ibatis .parsing .GenericTokenParser ;
33
38
import org .apache .ibatis .reflection .MetaClass ;
34
39
import org .apache .ibatis .scripting .xmltags .DynamicContext ;
35
40
import org .apache .ibatis .session .Configuration ;
@@ -54,7 +59,6 @@ private static class TemporaryTakeoverKeys {
54
59
55
60
private final Configuration configuration ;
56
61
private final SqlGenerator sqlGenerator ;
57
- private final SqlSourceBuilder sqlSourceBuilder ;
58
62
private final String sqlTemplate ;
59
63
private final Class <?> parameterType ;
60
64
@@ -76,7 +80,6 @@ private static class TemporaryTakeoverKeys {
76
80
this .sqlGenerator = sqlGenerator ;
77
81
this .sqlTemplate = sqlTemplate ;
78
82
this .parameterType = parameterType ;
79
- this .sqlSourceBuilder = new SqlSourceBuilder (configuration );
80
83
}
81
84
82
85
/**
@@ -101,13 +104,23 @@ public BoundSql getBoundSql(Object parameterObject) {
101
104
customVariables .put (TemporaryTakeoverKeys .PROCESSING_PARAMETER_TYPE , processingParameterType );
102
105
String sql = sqlGenerator .generate (sqlTemplate , parameterObject , bindings ::put , customVariables );
103
106
104
- SqlSource sqlSource = sqlSourceBuilder . parse (sql , processingParameterType , bindings );
107
+ SqlSource sqlSource = parse (configuration , sql , parameterObject , bindings );
105
108
BoundSql boundSql = sqlSource .getBoundSql (parameterObject );
106
109
bindings .forEach (boundSql ::setAdditionalParameter );
107
110
108
111
return boundSql ;
109
112
}
110
113
114
+ private static SqlSource parse (Configuration configuration , String originalSql , Object parameterObject ,
115
+ Map <String , Object > additionalParameters ) {
116
+ Class <?> parameterType = parameterObject == null ? Object .class : parameterObject .getClass ();
117
+ List <ParameterMapping > parameterMappings = new ArrayList <>();
118
+ ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler (parameterMappings , configuration ,
119
+ parameterObject , parameterType , additionalParameters , true );
120
+ GenericTokenParser parser = new GenericTokenParser ("#{" , "}" , handler );
121
+ return SqlSourceBuilder .buildSqlSource (configuration , parser .parse (originalSql ), parameterMappings );
122
+ }
123
+
111
124
/**
112
125
* The factory class for Thymeleaf's context.
113
126
*
@@ -120,7 +133,7 @@ static class ContextFactory implements BiFunction<Object, Map<String, Object>, I
120
133
@ Override
121
134
public IContext apply (Object parameter , Map <String , Object > customVariable ) {
122
135
Configuration configuration = (Configuration ) customVariable .remove (TemporaryTakeoverKeys .CONFIGURATION );
123
- Map <String , Object > bindings = (Map <String , Object >) customVariable .remove (TemporaryTakeoverKeys .DYNAMIC_CONTEXT );
136
+ Map <String , Object > bindings = (Map <String , Object >) customVariable .remove (TemporaryTakeoverKeys .DYNAMIC_CONTEXT );
124
137
Class <?> processingParameterType = (Class <?>) customVariable
125
138
.remove (TemporaryTakeoverKeys .PROCESSING_PARAMETER_TYPE );
126
139
MyBatisBindingContext bindingContext = new MyBatisBindingContext (
0 commit comments