org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.demo.da

IDEA中整合Springboot+SpringMVC+Mybatis报错:java

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.demo.dao.UserMapper.insertUserInfo
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.5.jar:3.5.5]
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53) ~[mybatis-3.5.5.jar:3.5.5]
    at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:115) ~[mybatis-3.5.5.jar:3.5.5]
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_144]
    at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:102) ~[mybatis-3.5.5.jar:3.5.5]
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) ~[mybatis-3.5.5.jar:3.5.5]
    at com.sun.proxy.$Proxy56.insertUserInfo(Unknown Source) ~[na:na]
    at com.example.demo.service.UserService.AddUserInfo(UserService.java:20) ~[classes/:na]
    at com.example.demo.controller.UserController.AddUserInfo(UserController.java:19) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]spring

解决方法:

   下面是一个简单的项目结构:apache

Mapper接口的全类名是:com.example.demo.daomybatis

Mapper.xml配置文件的路径是:resources.mappersapp

1.mapper.xml的namespace是mapper接口文件的全类名maven

2.mapper.xml和mapper接口文件中的方法名称要一致spring-boot

3.Springboot的配置文件中application.ymlui

4.项目名+Application.java文件中spa

 

可是!

有些时候,在上面的都填写正确后,仍是会报那个错误,缘由在于和mapper接口文件相互绑定的 .xml文件不是代码,无需编译,可是须要指定在编译时将指定的资源文件(此处为 .xml文件拷贝到对应目录下(此处应当为 编译好以后 类文件的对应目录3d

须要在pom.xml文件中配置resources,来指定mapper.xml所在的位置,否则mapper.xml文件就会被漏掉!

5.pom.xml文件中配置resources

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
	<resources>
		<resource>
			<directory>src/main/</directory>
			<!-- 此配置不可缺,不然mybatis的Mapper.xml将会丢失 -->
			<includes>
				<include>**/*.xml</include>
			</includes>
		</resource>
		<!--指定资源的位置-->
		<resource>
			<directory>src/main/resources</directory>
		</resource>
	</resources>
</build>