Reputation: 668
When I try to create dynamic query with MyBatis, the sql is generated successfully but parameters are not replaced in the SQL placeholders.
Mapper @SelectProvider definition
@SelectProvider(type = ActivitySqlProvider.class, method = "getActivitiesByUserAndType")
@ResultMap("activityResult")
List<Activities> getActivities(@Param("userId") long userId, @Param("type") String type);
ActivitySqlProvider class
public String getActivitiesByUserAndType(final Map<String, Object> params) {
String COLUMNS = "ACTIVITYID, USERID, TYPE, CREATED, DESCRIPTION";
String TABLE_NAME = "ACTIVITY";
boolean hasType = params.containsKey("type");
final String sql = new SQL() {{
SELECT(COLUMNS);
FROM(TABLE_NAME);
WHERE("USERID = #{userId}");
if (hasType) {
WHERE("TYPE = #{type}");
}
}}.toString();
System.out.println(sql);
return sql;
}
The SQL string is printed correctly and I can see the placeholders. Not sure what I'm missing.
Upvotes: 0
Views: 1015
Reputation: 25976
A blind guess: you are using wrong @Param
annotation.
Make sure you have:
import org.apache.ibatis.annotations.Param;
and not:
import org.springframework.data.repository.query.Param;
Print the contents of the params
map to validate this claim.
Upvotes: 1