Skip to content

Commit bc8663c

Browse files
author
Jarvis Song
committed
fix a @param and :param bug
1 parent 399eabf commit bc8663c

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

spring-data-mybatis/src/main/java/org/springframework/data/mybatis/repository/support/MybatisSimpleQueryMapperBuilder.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.springframework.data.mybatis.repository.support;
22

33
import java.util.Map;
4+
import java.util.Optional;
45
import java.util.regex.Pattern;
6+
import java.util.stream.Stream;
57

68
import org.springframework.data.mapping.PersistentEntity;
79
import org.springframework.data.mybatis.repository.query.InvalidMybatisQueryMethodException;
@@ -18,7 +20,6 @@
1820

1921
import org.apache.ibatis.mapping.SqlCommandType;
2022
import org.apache.ibatis.session.Configuration;
21-
import org.apache.ibatis.type.TypeHandler;
2223

2324
public class MybatisSimpleQueryMapperBuilder extends MybatisMapperBuildAssistant {
2425

@@ -81,21 +82,33 @@ else if (SELECT_ALL_FROM.matcher(sql).matches()) {
8182
if (!CollectionUtils.isEmpty(stringQuery.getParameterBindings())) {
8283
for (ParameterBinding parameterBinding : stringQuery.getParameterBindings()) {
8384

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;
9186

9287
if (StringUtils.hasText(parameterBinding.getName())) {
9388
replace = ":" + parameterBinding.getName();
9489
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+
95103
}
96104
else {
105+
MybatisParameter mp = method.getParameters().getBindableParameter(
106+
parameterBinding.getRequiredPosition() - 1);
97107
replace = "?" + parameterBinding.getPosition();
98108
bindName = mp.getName().orElse("__p" + mp.getIndex());
109+
if (null != mp.getSpecifiedTypeHandler()) {
110+
typeHandler = mp.getSpecifiedTypeHandler().getName();
111+
}
99112
}
100113

101114
if (parameterBinding instanceof InParameterBinding) {
@@ -104,8 +117,7 @@ else if (SELECT_ALL_FROM.matcher(sql).matches()) {
104117
+ bindName
105118
+ "\" open=\"(\" separator=\",\" close=\")\">#{__item"
106119
+ (null != typeHandler
107-
? (",typeHandler=" + typeHandler.getName())
108-
: "")
120+
? (",typeHandler=" + typeHandler) : "")
109121
+ "}</foreach>");
110122

111123
continue;
@@ -139,11 +151,9 @@ else if (SELECT_ALL_FROM.matcher(sql).matches()) {
139151

140152
}
141153

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+
+ "}");
147157

148158
}
149159

spring-data-mybatis/src/test/java/org/springframework/data/mybatis/repository/sample/UserRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ List<User> findByEmailAddressAndLastnameOrFirstname(String emailAddress,
6464

6565
@Transactional
6666
@Modifying
67-
@Query("update ds_user set lastname = ?1")
68-
void renameAllUsersTo(String lastname);
67+
@Query("update ds_user set lastname=:lastname")
68+
void renameAllUsersTo(@Param("lastname") String lastname);
6969

7070
List<User> findByFirstnameOrLastname(@Param("lastname") String lastname,
7171
@Param("firstname") String firstname);

0 commit comments

Comments
 (0)