mybatis接口中传参报错 Parameter 'XXXXX' not found
使用mybatis框架查询数据库,mapper类中的接口参数总是报错,找不到参数,最近总是遇到,可能是我不太常用此框架,遇到这个问题总是记不住,抽点时间记录下,报错信息如下
19-Aug-2022 14:10:40.986 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
19-Aug-2022 14:10:41.661 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
19-Aug-2022 14:11:09.189 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Initializing Spring SpringDispatcherServlet 'Dispatcher'
19-Aug-2022 14:16:36.180 严重 [http-nio-8080-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'reviewStatus' not found. Available parameters are [arg1, arg0, param1, param2]] with root cause
org.apache.ibatis.binding.BindingException: Parameter 'reviewStatus' not found. Available parameters are [arg1, arg0, param1, param2]
at
mapper类中的接口:
List<JxkhevebtRecordStatisticsVO> getEventRecordListAndHuiShenIdea(String reviewStatus, Long summaryId);
修改为如下,参数加注解
List<JxkhevebtRecordStatisticsVO> getEventRecordListAndHuiShenIdea(@Param("reviewStatus")String reviewStatus, @Param("summaryId")Long summaryId);
为何加注解,看报错信息,接口中的(String reviewStatus, Long summaryId)成了[arg1, arg0 ],mybatis只能识别一个参数,当传入的参数大于1时需要加注解@Param()指定参数名称
org.apache.ibatis.binding.BindingException: Parameter 'reviewStatus' not found. Available parameters are [arg1, arg0, param1, param2]
查资料时发现,如果在动态 SQL 中使用参数作为变量,那么也需要 @Param 注解,即使只有一个参数。例如如下接口:
JxkhAdvise queryByEventRecordId(@Param("eventRecordId") Long eventRecordId, @Param("nodeType") String nodeType, @Param("creUserId") String creUserId);
xml文件对应方法:
<select id="queryByEventRecordId" resultMap="JxkhAdviseMap">
<include refid="selectCol"/>
<where>
<if test="eventRecordId != null and eventRecordId != ''">
and event_record_id = #{eventRecordId}
</if>
<if test="creUserId != null and creUserId != ''">
and cre_user_id = #{creUserId}
</if>
<if test="nodeType != null and nodeType != ''">
and node_type = #{nodeType}
</if>
</where>
</select>
```