Maven项目使用mybatis报错 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

maven项目使用mybatis时,找不到mapper文件(.xml)

错误信息提示:

项目可以正常运行,但是在有请求到达服务器时(有访问数据库的请求),会出现报错!!
错误原因:
  • mybatis没有找到对应的请求调用持久层的方法

查错方法:

首先检查target-->classes文件夹被dao层文件夹内是否有对应mapper.xml文件,如果有xml文件,则一般是配置文件出现的错误,如果没有xml类型的文件,则是没有拷贝资源文件的原因。下面是对应的解决方法。

1.名称不一致问题

排查步骤:
  • 检查mapper接口与mapper.xml文件名称是否一致

在这里插入图片描述

  • mapper.xml里面指定的namespace是否是mapper接口,名称是否正确

在这里插入图片描述

  • mapper接口内的方法名时候与mapper.xml文件中的id对应

  • 检查mybatis配置文件(SqlMapConfig.xml)与spring配置文件指定的名称是否一致

  • web.xml配置文件中指定的spring配置文件名称是否与真实的spring配置文件的名称路径一致

2. 默认拷贝资源文件无效的问题(target目录下的classes文件夹内没有对应的mapper.xml)---默认不拷贝src/main/java下面的配置文件

解决方案【以下三种方案选一种即可】(ecplise):
  • 项目名--右击--build path--config build path--source--src/main/java--out.....---edit---add---**/*.xml

    推荐下面方式,ecplise和idea都可使用
  • pom.xml文件中 添加资源文件拷贝插件 添加自定义的资源文件拷贝行为(如果配置src/main/java的拷贝行为,会导致默认的拷贝行为失效,即不再拷贝src/main/resouce文件夹内的位置文件,应该将两个都拷贝行为都配置一下)

    <!-- 资源文件拷贝插件 -->
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-resources-plugin</artifactId>
                  <version>2.7</version>
                  <configuration>
                      <encoding>UTF-8</encoding>
                  </configuration>
              </plugin>

<!-- 资源文件拷贝插件 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
  • 将mapper接口和mapper.xml文件分开 在resouce文件夹内模仿mapper接口文件层次创建文件夹,将mapper.xml文件放在里面,编译输出时,会输出在target目录下的同一位置

猜你喜欢

转载自www.cnblogs.com/erkye/p/12078597.html