Parameter 'username' not found. Available parameters are [0, 1, param1, param2]

来源:互联网 发布:金融数据分析员 编辑:程序博客网 时间:2024/05/21 09:33

Type Exception Report

Message Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'username' not found. Available parameters are [0, 1, param1, param2]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter '2' not found. Available parameters are [0, 1, param1, param2]org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)javax.servlet.http.HttpServlet.service(HttpServlet.java:661)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)javax.servlet.http.HttpServlet.service(HttpServlet.java:742)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)

Root Cause

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'username' not found. Available parameters are [0, 1, param1, param2]org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)com.sun.proxy.$Proxy13.selectOne(Unknown Source)org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:163)org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:69)org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)com.sun.proxy.$Proxy14.select(Unknown Source)cn.edu.cdu.service.impl.UserServiceImpl.Login(UserServiceImpl.java:20)cn.edu.cdu.controller.UserController.Login(UserController.java:32)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invoke(Method.java:498)org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)javax.servlet.http.HttpServlet.service(HttpServlet.java:661)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)javax.servlet.http.HttpServlet.service(HttpServlet.java:742)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)

Root Cause

org.apache.ibatis.binding.BindingException: Parameter 'username' not found. Available parameters are [0, 1, param1, param2]org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:168)org.apache.ibatis.reflection.wrapper.MapWrapper.get(MapWrapper.java:45)org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:212)org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:139)org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:120)org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:113)org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:73)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invoke(Method.java:498)org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358)com.sun.proxy.$Proxy13.selectOne(Unknown Source)org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:163)org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:69)org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)com.sun.proxy.$Proxy14.select(Unknown Source)cn.edu.cdu.service.impl.UserServiceImpl.Login(UserServiceImpl.java:20)cn.edu.cdu.controller.UserController.Login(UserController.java:32)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invoke(Method.java:498)org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)javax.servlet.http.HttpServlet.service(HttpServlet.java:661)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)javax.servlet.http.HttpServlet.service(HttpServlet.java:742)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
所用的是SpringMVC+Spring+Mybatis三大框架整合
改之前的代码:
Controller层的代码:
  1. public class UserController {  
  2.     @Autowired  
  3.     public UserServiceImpl userService;  
  4.     @RequestMapping(method=RequestMethod.POST,value="/login")  
  5.      public String login(@RequestParam("username")String username,@RequestParam("password")String password){  
  6.         login login=userService.login(username, password);  
mapper的代码:
  1. <select id="login"  resultMap="BaseResultMap">  
  2.         select * from login where name=#{name,jdbcType=VARCHAR} and password=#{password,jdbcType=VARCHAR}   
  3.      </select>  
  4.  </mapper>  

问题所在:controller中的方法参数与mapper.xml中的参数类型不对应,将controller中的方法参数写成对象的形式,将各种参数赋给该对象进行传递。

改之后的

Controller层的代码

  1. public String login(@RequestParam("username")String username,@RequestParam("password")String password){
  2.     login l=new login ();
  3.     l.setUsername(username);
  4.     l.setPassword(password);
  5.     login login =userService.login(l);

或者,改mapper的代码:
  1. <select id="login"  resultMap="BaseResultMap">  
  2.         select * from login where name=#{0} and password=#{1}   
  3.      </select>  
  4.  </mapper>  

阅读全文
1 0