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

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

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]

解决方法:

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

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

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

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

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

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

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

 

但是!

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

需要在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>

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值