我创建了一个sql查询如下
<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 >= #{beginDate,jdbcType=VARCHAR}
</if>
<if test="endDate != null">
AND r2.rdt_date <= #{endDate,jdbcType=VARCHAR}
</if>
</select>
参数的值分别是
String stuName = "nike";
String beginDate = "2018-03-01";
String endDate = "2018-06-01";
String consultName = "";
错误是
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).
参数“endDate”正好是String类型,为什么是“javaType=class java.lang.Object”。
我该如何解决?
谢谢你的帮助。
java mapper代码如下:
List<ReservationRecord> getReservationByConditions(@Param("stuName")String stuName, @Param("beginDate")String beginDate, @Param("endDate")String endDate, @Param("consultName")String consultName);
好吧。我知道我的代码有什么问题。 SQL 查询中的“%#{consultName}%”应为“%${consultName}%”。我改变了它,它工作正常。这真是一个荒谬的问题。我想我应该更加小心。
最佳答案
不要使用字符串,而是真正的日期。变化:
#{beginDate,jdbcType=VARCHAR}
对于:
#{beginDate,jdbcType=DATE}
(一天中没有时间),或#{beginDate,jdbcType=TIMESTAMP}
(如果您需要包括当天的时间)。
对 endDate
参数进行相同的更改。
并且您要应用的 Java 参数应为以下类型:
java.sql.Date
(没有时间的日期),java.sql.Timestamp
(时间戳),或者java.util.Date
(日期和时间)。
关于java - Mybatis-错误: Could not set parameters for mapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49858820/