范淦祥
范淦祥

Reputation: 51

Mybatis-Error: Could not set parameters for mapping

I created a sql query as follows

<select id="getReservationByConditions" resultMap="record">
    SELECT *
    FROM (reservation_record r1 LEFT JOIN real_duty_time r2 ON r1.rdt_id = r2.rdt_id) LEFT JOIN  consultant c1 ON r2.con_id = c1.con_id
    WHERE r1.stu_name LIKE '%${stuName}%' AND c1.con_name LIKE '%#{consultName}%'
    <if test="beginDate != null">
        AND r2.rdt_date &gt;= #{beginDate,jdbcType=VARCHAR}
    </if>
    <if test="endDate != null">
        AND r2.rdt_date &lt;= #{endDate,jdbcType=VARCHAR}
    </if>
</select>

And the value of the parameters are

String stuName = "nike";
String beginDate = "2018-03-01";
String endDate = "2018-06-01";
String consultName = "";

And the errors are

org.mybatis.spring.MyBatisSystemException:
nested exception is org.apache.ibatis.type.TypeException:
Could not set parameters for mapping: ParameterMapping{property='endDate', mode=IN, javaType=class java.lang.Object, jdbcType=VARCHAR, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. 

Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #3 with JdbcType VARCHAR . 
Try setting a different JdbcType for this parameter or a different configuration property. 

Cause: org.apache.ibatis.type.TypeException:
Error setting non null for parameter #3 with JdbcType VARCHAR .
Try setting a different JdbcType for this parameter or a different configuration property.

Cause: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).

The parameter "endDate" is exactly String type,why "javaType=class java.lang.Object".

How do I fix it?

Thank you for help.


The java mapper code is as follows:

List<ReservationRecord> getReservationByConditions(@Param("stuName")String stuName, @Param("beginDate")String beginDate, @Param("endDate")String endDate, @Param("consultName")String consultName);

well.I know what's wrong with my code. The '%#{consultName}%' in sql query should be '%${consultName}%'. I changed it and it works fine. It is a really ridiculous problem. I think I should be more careful.

Upvotes: 5

Views: 21934

Answers (2)

Subin Chalil
Subin Chalil

Reputation: 3660

Re posting the answer.

Change c1.con_name LIKE'%#{consultName}%' to c1.con_name LIKE'%${consultName}%'.

Note : Use $ instead of #

Upvotes: 1

The Impaler
The Impaler

Reputation: 48770

Don't use strings but bona fide dates. Change:

#{beginDate,jdbcType=VARCHAR}

for:

  • #{beginDate,jdbcType=DATE} (no time of the day), or
  • #{beginDate,jdbcType=TIMESTAMP} (if you need to include the time of the day).

Make the same change for the endDate parameter.

And the Java parameter you want to apply should be of type:

  • java.sql.Date (date without time),
  • java.sql.Timestamp (timestamp), or
  • java.util.Date (date and time).

Upvotes: 3

Related Questions