1
1
package org .springframework .data .mybatis .repository .support ;
2
2
3
3
import java .util .Map ;
4
+ import java .util .Optional ;
4
5
import java .util .regex .Pattern ;
6
+ import java .util .stream .Stream ;
5
7
6
8
import org .springframework .data .mapping .PersistentEntity ;
7
9
import org .springframework .data .mybatis .repository .query .InvalidMybatisQueryMethodException ;
18
20
19
21
import org .apache .ibatis .mapping .SqlCommandType ;
20
22
import org .apache .ibatis .session .Configuration ;
21
- import org .apache .ibatis .type .TypeHandler ;
22
23
23
24
public class MybatisSimpleQueryMapperBuilder extends MybatisMapperBuildAssistant {
24
25
@@ -81,21 +82,33 @@ else if (SELECT_ALL_FROM.matcher(sql).matches()) {
81
82
if (!CollectionUtils .isEmpty (stringQuery .getParameterBindings ())) {
82
83
for (ParameterBinding parameterBinding : stringQuery .getParameterBindings ()) {
83
84
84
- String replace , bindName ;
85
-
86
- MybatisParameter mp = method .getParameters ()
87
- .getBindableParameter (parameterBinding .getRequiredPosition () - 1 );
88
-
89
- Class <? extends TypeHandler <?>> typeHandler = mp
90
- .getSpecifiedTypeHandler ();
85
+ String replace , bindName , typeHandler = null ;
91
86
92
87
if (StringUtils .hasText (parameterBinding .getName ())) {
93
88
replace = ":" + parameterBinding .getName ();
94
89
bindName = parameterBinding .getName ();
90
+
91
+ Stream <MybatisParameter > stream = method .getParameters ()
92
+ .getBindableParameters ().get ();
93
+ Optional <MybatisParameter > first = stream
94
+ .filter (mp -> bindName .equals (mp .getName ().orElse (null )))
95
+ .findFirst ();
96
+ if (first .isPresent ()) {
97
+ MybatisParameter mp = first .get ();
98
+ if (null != mp .getSpecifiedTypeHandler ()) {
99
+ typeHandler = mp .getSpecifiedTypeHandler ().getName ();
100
+ }
101
+ }
102
+
95
103
}
96
104
else {
105
+ MybatisParameter mp = method .getParameters ().getBindableParameter (
106
+ parameterBinding .getRequiredPosition () - 1 );
97
107
replace = "?" + parameterBinding .getPosition ();
98
108
bindName = mp .getName ().orElse ("__p" + mp .getIndex ());
109
+ if (null != mp .getSpecifiedTypeHandler ()) {
110
+ typeHandler = mp .getSpecifiedTypeHandler ().getName ();
111
+ }
99
112
}
100
113
101
114
if (parameterBinding instanceof InParameterBinding ) {
@@ -104,8 +117,7 @@ else if (SELECT_ALL_FROM.matcher(sql).matches()) {
104
117
+ bindName
105
118
+ "\" open=\" (\" separator=\" ,\" close=\" )\" >#{__item"
106
119
+ (null != typeHandler
107
- ? (",typeHandler=" + typeHandler .getName ())
108
- : "" )
120
+ ? (",typeHandler=" + typeHandler ) : "" )
109
121
+ "}</foreach>" );
110
122
111
123
continue ;
@@ -139,11 +151,9 @@ else if (SELECT_ALL_FROM.matcher(sql).matches()) {
139
151
140
152
}
141
153
142
- sql = sql .replace (replace ,
143
- "#{" + bindName
144
- + (null != typeHandler
145
- ? (",typeHandler=" + typeHandler .getName ()) : "" )
146
- + "}" );
154
+ sql = sql .replace (replace , "#{" + bindName
155
+ + (null != typeHandler ? (",typeHandler=" + typeHandler ) : "" )
156
+ + "}" );
147
157
148
158
}
149
159
0 commit comments