一、问题描述

使用mybatis的项目在本地可以正常运行,但当使用maven或Jenkins打包部署到服务器上时出现了绑定错误,异常信息为:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.yo.news.user.mapper.UserMapper.getUserByTelPwd

二、问题分析和解决方法

首先,给定的异常提示信息并不精准,有多个错误原因都会抛出该异常。mybatis出现这个问题,通常是由Mapper interface和对应的xml文件的定义对应不上引起的,这时就需要仔细检查对比包名、xml中的namespace、接口中的方法名称等是否对应。我之前就因为称忘记在xml标签的id属性中添加方法名或写错方法名而出现这个错误。

出现这个错误时,按以下步骤检查一般就会解决问题:
1:检查xml文件所在package名称是否和Mapper interface所在的包名一一对应;
2:检查xml的namespace是否和xml文件的package名称一一对应;
3:检查方法名称是否对应;
4:去除xml文件中的中文注释;
5:随意在xml文件中加一个空格或者空行然后保存。

--------------------------------

但是!!!!!!最后发现问题所在:maven项目的约定配置文件必须放resources里,src目录下的xml文件默认不会编译到target。由于我把mapper.xml放在了src目录里,才导致了错误的发生,该问题的实质是,idea对classpath的规定。在eclipse中,把资源文件放在src文件夹下,是可以找到的;但是在idea中,直接把资源文件放在src文件夹下,如果不进行设置,是不能被找到的。

原来Maven 为我们提供了一致的项目目录配置(源文件夹、资源文件夹等),在自动构建项目时, Maven 会按照这个配置来执行操作(编译源文件、拷贝资源文件),Maven 默认的源文件夹及资源文件夹的配置代码如下:

<build>
<sourceDirectory>src/main/java</sourceDirectory >
<testSourceDirectory>src/test/java</testSourceDirectory >
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build>

解决方案有2种:

1、可以把xml文件放到resource目录下,这样项目构建的时候会加载到target。

2、在pom.xml文件build添加resource资源列表。

<!--这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里。-->
<resources>
<resource>
<!-- 描述存放资源的目录,该路径相对POM路径-->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

总结

如果出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误,一般的原因是Mapper interface和xml文件的定义对应不上,需要检查包名,namespace,函数名称等能否对应上,需要比较细致的对比,按以下步骤一一执行:

1、检查xml文件所在的package名称是否和interface对应的package名称一一对应

2、检查xml文件的namespace是否和xml文件的package名称一一对应

3、检查函数名称能否对应上

4、去掉xml文件中的中文注释

5、随意在xml文件中加一个空格或者空行然后保存

除此之外,我遇到的还有一些特殊情况,耽误了我不少时间,网上有其他原因也导致bound找不到:

1、Intellij Idea 的包名和目录名生成机制,新建一个包a.b.c.d,目录结构不是a->b->c->d,而是生成"a.b.c.d"的目录,进而导致mybatis映射错误,此错误很难排查。

2、xml文件定义如下:

<select id="countMembers" parameterMap="java.util.Map" resultType="java.lang.Integer">

parameterMap用错,应该为parameterType,此错误会导致mybatis所有的mapper都报绑定错误,很坑。

 

解决Invalid bound statement (not found)(Mybatis的Mapper绑定问题)的更多相关文章

  1. IDEA+Maven+Mybatis 巨坑:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.UserMapper.findAll

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.User ...

  2. [转载]Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByExample: 错误

    因碰到同样的问题,使用该方法对我有效,为方便以后查找,所以做了转载,原文请查看:https://www.cnblogs.com/fifiyong/p/5795365.html 在Maven工程下,想通 ...

  3. Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByExample: 错误

    在Maven工程下,想通过controller接收url中的参数Id查询数据库的数据,并且以json形式显示在页面. 在浏览器输入url后出现的问题: 八月 22, 2016 1:45:22 下午 o ...

  4. Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByExample问题解决

    最近在做一个关于ssm框架整合的项目,但是今天正合完后出现了问题: Invalid bound statement (not found): com.taotao.mapper.TbItemMappe ...

  5. 项目启动报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.wuhongyu.mapper.OrdersMapper.selectByExample

    在用maven配置mybatis环境时出现此BindingExceptiony异常,发现在classes文件下没有mapper配置文件,应该是maven项目没有扫描到mapper包下的xml文件, 在 ...

  6. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.shen.mapper.UserMapper.findById

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.shen.mapper.Use ...

  7. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bw.mapper.BillMapper.getBillList at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225

    这个错误是没有找到映射文件 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.b ...

  8. Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByPrimaryKey

    Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByPrimaryKey Invalid bound ...

  9. 解决Invalid bound statement (not found)的异常

    今天在搭建框架的时候,报了一个Invalid bound statement (not found)的异常 经过分析,得出原因: 我的mybatis相关的dao和mapper.xml是通过逆向工程生成 ...

随机推荐

  1. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时6

    课时6 线性分类器损失函数与最优化(上) 多类SVM损失:这是一个两分类支持向量机的泛化 SVM损失计算了所有不正确的例子,将所有不正确的类别的评分,与正确类别的评分之差加1,将得到的数值与0作比较, ...

  2. JAVA基础-面向对象05

    一.面向对象的引入 解决问题的时候,首先要会分析思路,这个思路就是我们生活中解决一个问题的方法步骤: 有了思路之后,再把思路使用java代码来解决: 但是 计算总分的需求变了 分析:原来在一个程序中, ...

  3. IT兄弟连 Java Web教程 Web开发的相关知识

    Web基本概念 Web,是环球信息网的缩写,也称作“WWW.W3”,英文全称为World Wide Web,中文名成为万维网,常简称为Web.Web分为Web客户端和Web服务器程序.Web可以让We ...

  4. 各大版本idea永久破解激活方法

    文章转载自:https://www.jiweichengzhu.com/article/a45902a1d7284c6291fe32a4a199e65c 如果还有问题,加群交流:686430774(就 ...

  5. spring boot :error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required

    配置多个数据源启动报错,error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSo ...

  6. spring mvc No mapping found for HTTP request with URI [/web/test.do] in DispatcherServlet with name 'spring'

    原因: spring-servlet.xml 中 <context:component-scan base-package="com.test.controller" /&g ...

  7. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  8. Linux修改文件的权限,拥有者,所属组

    修改文件的权限,拥有者,所属组 1.设置文件的权限(chmod) ①方式一(建议使用这种方式) 命名:chomd 755 文件名 ②方式二 命名:chomd -R +x 文件名 2.设置文件的拥有者( ...

  9. Win7执行应用报CLR20r3错误处理记录

    Windows7环境下运行应用报"CLR20r3"错误,错误信息如下: 问题详细信息: 问题签名: 问题事件名称: CLR20r3 问题签名 : qbbtools.exe 问题签名 ...

  10. JSP与Servlet的编解码

    一.java web中涉及编解码的地方 (1)浏览器端向后台发起请求时:URL.Cookie.Parameter: (2)后台响应返回数据时:页面编码,数据库数据编码: