maven项目报Invalid bound statement (not found): com.**.*解决方案

本文针对项目中出现的MyBatis BindingException错误,提供了一套详细的排查与解决流程,包括检查配置文件、Spring配置及POM文件等关键步骤。

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

原本好好的项目突然之间报错了,百思不得解:
Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.*.*.dao.UserDao.*
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:189)
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:43)
	at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:58)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:51)



后来经多方查证,发现问题所在,废话不多说,直接上解决方案:

1.检查配置文件namespace是否写错,检查文件中是否
有parameterType类型书写错误,是否有resultType
和resultMap写错,同时检查是否有大小写或者拼写错误的。
2.如果不是以上错误,那么请检查是否在spring的配置
文件中的映射路径写错了。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        <property name="basePackage" value="com.misscandy.inter" />
        <property name="annotationClass" value="com.misscandy.mapper.Mapper" />
    </bean>

或者
    
<property name="mapperLocations"
        value="classpath*:com/******/mappers/*Mapper.xml"></property>

3.如果以上都没有问题的话,那就是在pom.xml中出的错
了,在pom.xml文件中的build标签下添加如下内容:
<resources>  
	        <resource>  
	            <directory>src/main/java</directory>  
	            <includes>  
	                <include>**/*.xml</include>  
	            </includes>  
	        </resource>  
	        <resource>  
	            <directory>src/main/resources</directory>  
	        </resource>  
	    </resources>



当遇到 `Invalid bound statement (not found)` 错误时,通常表示 MyBatis 无法找到对应的 SQL 映射语句。以下是可能的原因及解决方法: 1. **检查 Mapper 接口和 XML 文件的绑定关系** 确保 `UserMapper` 接口中的方法名与 XML 文件中 `<select>`, `<insert>`, `<update>`, `<delete>` 标签的 `id` 属性完全一致。例如,如果接口方法为 `countByMap`,则 XML 文件中的标签应如下定义: ```xml <select id="countByMap" resultType="int"> SELECT COUNT(*) FROM user WHERE username = #{username} AND status = #{status} </select> ``` 2. **确认 MyBatis 配置文件中的 mapper-locations 设置正确** 在 `application.yml` 或 `application.properties` 中配置的 `mapper-locations` 应指向包含映射文件的目录。例如,在 `application.yml` 中: ```yaml mybatis-plus: mapper-locations: classpath:mapper/**/*.xml ``` 此配置确保 MyBatis 能够扫描到所有 `.xml` 映射文件[^2]。 3. **检查包扫描配置** 确保 Spring Boot 应用程序的主类上有 `@MapperScan` 注解,并且指定了正确的包路径以扫描 Mapper 接口。例如: ```java @SpringBootApplication @MapperScan("com.sky.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样可以保证 Spring 容器能够自动注册所有的 Mapper 接口[^1]。 4. **验证 XML 文件的位置和命名** 如果 XML 文件位于 `resources/mapper` 目录下,则其名称应该与对应的 Mapper 接口相匹配。例如,对于 `UserMapper` 接口,XML 文件应命名为 `UserMapper.xml` 并放置在 `resources/mapper` 下。此外,还需要确认项目构建时是否将这些资源文件正确地复制到了输出目录[^3]。 5. **清理并重新构建项目** 有时候旧的编译文件可能会导致问题,尝试清理项目并重新构建。使用 Maven 可以运行以下命令: ```bash mvn clean install ``` 或者对于 Gradle 项目: ```bash gradle clean build ``` 清理后重新启动应用服务器,看看问题是否得到解决。 6. **启用 MyBatis 日志记录** 开启 MyBatis 的日志功能可以帮助诊断更详细的错误信息。可以在 `application.yml` 中添加如下配置来开启控制台日志输出: ```yaml mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` 这有助于查看实际执行的 SQL 语句以及参数绑定情况,从而进一步排查问题。 通过以上步骤,应该能够定位并修复导致 `Invalid bound statement (not found)` 异常的问题。如果仍然存在疑问或需要更多帮助,请提供具体的代码片段或错误堆栈信息以便深入分析。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值