【问题描述】
2018-06-09 17:33:09.910 ERROR 7424 --- [io-2008-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] 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 'payDate' not found. Available parameters are [arg1, arg0, param1, param2]] with root cause
org.apache.ibatis.binding.BindingException: Parameter 'payDate' not found. Available parameters are [arg1, arg0, param1, param2]
at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:202) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:115) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2671) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:50) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.ASTAnd.getValueBody(ASTAnd.java:61) ~[mybatis-3.4.5.jar:3.4.5]
【原因分析】
在XxxxMapper.xml文件中,定义的parameterType为"java.util.Map":
<select id="xxxxxxxx" parameterType="java.util.Map" resultType="com.xxxx.framework.model.xxxx.xxxxxxxxxxxx">
而在XxxxServiceImpl.java文件中使用XxxxMapper.java文件中的方法进行查询时,传过去的参数不是map集合,而是两个参数;
【解决方法】
XxxxServiceImpl.java文件中,调用XxxxMapper类中的查询方法时,需要把相应的参数封装到一个map集合中,然后再传递过去进行查询:
Map<String, Object> map = new HashMap<String, Object>();
map.put("userId", userId);
map.put("payDate", payDate);
list = xxxxMapper.selectAllByUserIdOrMonth(map);