5

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.

2
  • can you add your java mapper code Commented Apr 16, 2018 at 14:23
  • So, did the solution work? Commented Apr 16, 2018 at 17:04

2 Answers 2

3

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).
Sign up to request clarification or add additional context in comments.

3 Comments

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"
If you have the answer, can you post it?
LocalDate and LocalDateTime can also be used.
1

Re posting the answer.

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

Note : Use $ instead of #

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.