mybatis DAO和Mapper 的传值问题

本文讲述了在MyBatis中遇到的多表关联查询时的多参数问题,详细描述了从错误到正确解决方案的过程。通过一个具体的例子,指出当参数为表内不存在的字段时,需要注意的参数传递方式,旨在帮助开发者避免此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在Mapper中使用到了多表关联查询,于是乎就用到了多参数。所以按照以往的思维,写出了如下的代码

List<AccomodationInfoModel> selectByAll(String buildingName,int roomNumber, int bedNumber);

写完了其他剩余代码,高高兴兴去测试了,结果是这样的:

rg.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'buildingName' not found. Available parameters are [0, roomNumber, bedNumber, param3, param1, param2]
    org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
    org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
    com.sun.proxy.$Proxy26.selectList(Unknown Source)
    org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:198)
    org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:119)
    org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
    org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
    com.sun.proxy.$Proxy30.selectByAll(Unknown Source)
    com.willu.serviceImpl.currency.Accomodation.AccomodationQueryInfo(Accomodation.java:20)
    com.willu.serviceImpl.manager.Manager.selectAccomodation(Manager.java:38)
    com.willu.rest.ManagerRest.getTest(ManagerRest.java:53)
    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)
    com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

楼楼顿时傻住了,开始以为是model中写的不对,后面才知道是参数的问题。百度之后,知道应该这样写

List<AccomodationInfoModel> selectByAll(@Param("buildingName") String buildingName,@Param("roomNumber")int roomNumber,@Param("bedNumber") int bedNumber);

自己个人就稍微总结了一下,之前用到的参数都是表内有的参数,所以碰巧能查询,但是这次是表内没有的字段,所以无法识别参数。希望对大家有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值