Mybatis报错:无法为映射设置参数,请求排查SQL配置问题
Hey there, let's dig into why you're running into this parameter binding issue in MyBatis. After checking your SQL statement, I can spot several key problems that are likely causing the error:
Common Causes & Fixes
1. Invalid Use of ${} and #{} in Fuzzy Queries
Your line c1.con_name LIKE '%#{consultName}%' is incorrect. MyBatis treats #{} as a precompiled placeholder (which gets converted to ? in the final SQL), so wrapping it inside single quotes breaks the parameter binding logic.
Also, using ${stuName} directly works for string substitution but carries a SQL injection risk. Instead, use #{} with the CONCAT function to safely build your fuzzy match condition.
2. Incomplete XML Syntax
Your closing <if> tag for endDate is cut off (jdbcType=...), which will cause MyBatis to fail parsing the XML mapping file entirely. Always ensure all tags and attributes are fully completed.
3. Potential Type Mismatch in JDBC Type
If your rdt_date column is a date type (not VARCHAR), specifying jdbcType=VARCHAR could lead to type conversion errors when binding the parameter. Match the jdbcType to your actual database column type (e.g., DATE or TIMESTAMP).
Corrected SQL Mapping Example
Here's the fixed version of your query:
<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 CONCAT('%', #{stuName}, '%') AND c1.con_name LIKE CONCAT('%', #{consultName}, '%') <if test="beginDate != null and beginDate != ''"> AND r2.rdt_date >= #{beginDate,jdbcType=DATE} </if> <if test="endDate != null and endDate != ''"> AND r2.rdt_date <= #{endDate,jdbcType=DATE} </if> </select>
Additional Checks
- If your mapper interface method uses multiple parameters, make sure you've added
@Paramannotations to each parameter (e.g.,List<Record> getReservationByConditions(@Param("stuName") String stuName, @Param("consultName") String consultName, ...)). This helps MyBatis map method parameters to XML variables correctly. - Verify that your parameter names in the XML exactly match the ones in your mapper interface (case-sensitive in most cases).
内容的提问来源于stack exchange,提问作者范淦祥

