Mybatis-Error: Could not set parameters for mapping

15,481

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).
Share:
15,481
范淦祥
Author by

范淦祥

Updated on July 09, 2022

Comments

  • 范淦祥
    范淦祥 almost 2 years

    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.

  • 范淦祥
    范淦祥 about 6 years
    It didn't work. I changed the code as you say. The errors are: Could not set parameters for mapping: ParameterMapping{property='endDate', mode=IN, javaType=class java.lang.Object, jdbcType=DATE, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}.Other errors just changed "jdbcType=DATE"
  • The Impaler
    The Impaler about 6 years
    If you have the answer, can you post it?
  • Alex78191
    Alex78191 over 5 years
    LocalDate and LocalDateTime can also be used.