简体   繁体   中英

MyBatis 3.4.5; @SelectProvider not replacing parameter placeholders

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM