SpringBoot整合Mybatis报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

问题描述:

在SpringBoot整合Mybatis的环境下执行SQL时报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

application.yml文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3307/flying_ping?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: root
  web:
    resources:
      static-locations: classpath:static

mybatis:
  mapper-locations: com/test/mapper/*.xml
  type-aliases-package: com.test.pojo
  configuration:
    map-underscore-to-camel-case: true

看起来也没错,难道这些映射文件压根就没有编译进去吗?所以我跑到classpath目录去看了一下
在这里插入图片描述果然不出我所料,SpringBoot没有将src/main/java/下的xml文件编译进来。。。


原因分析:

在网上查找资料得知,这并不是SpringBoot的错,而是IDEA的错,误会SpringBoot了呀!!!
IDEA的maven项目中,默认源代码目录下(src/main/java目录)的xml等资源文件并不会在编译的时候一块打包进classes文件夹,而是直接舍弃掉。如果使用的是Eclipse,Eclipse的src目录下的xml等资源文件在编译的时候会自动打包进输出到classes文件夹。


解决方案:

解决方案有多种

  • 第一种:需要在pom.xml中添加如下插件
<build>
     <resources>
          注意这里要给springBoot指定需要打包的静态资源。否则SpringBoot将找不到配置文件
          <resource>
              <directory>src/main/java</directory>
              <includes>
                  <include>**/*.yml</include>
                  <include>**/*.properties</include>
                  <include>**/*.xml</include>
              </includes>
              <filtering>false</filtering>
          </resource>
          <resource>
              <directory>src/main/resources</directory>
              <includes>
                  <include>**/*.yml</include>
                  <include>**/*.properties</include>
                  <include>**/*.xml</include>
              </includes>
              <filtering>false</filtering>
          </resource>
      </resources>
  </build>

这种方式在项目实战中还是比较常用且靠谱的

  • 第二种:将mapper.xml文件放在resource目录下

如果没有什么特殊场景,也就是你仅仅只是练习就将mapper.xml放在resources包下。因为MAVE默认在编译的时候会将resources包下的文件也编译到classpath下
如图
在这里插入图片描述同时将properties文件或yml文件中的配置改为

mybatis.mapper-locations=classpath:mapper/*.xml
### Spring Boot 3.2.5 使用 Gradle 构建时解决 MyBatis BindingException 错误 MyBatis 的 `BindingException` 错误通常表示在运行时无法找到绑定的 SQL 语句。这可能是由于 Mapper 接口与对应的 XML 文件未正确映射,或者配置文件中某些路径设置不正确导致的。 #### 1. 检查依赖配置 确保在 `build.gradle` 中正确添加了 MyBatis-Plus 和相关依赖项。以下是推荐的依赖配置: ```gradle dependencies { implementation 'com.baomidou:mybatis-plus-spring-boot3-starter:3.5.5' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3' } ``` 上述依赖项分别引入了 MyBatis-PlusMyBatis-Spring 支持[^2]。 #### 2. 配置 `application.yml` 确保 `application.yml` 中的 `mybatis-plus` 配置正确,特别是 `mapper-locations` 和 `type-aliases-package` 的路径设置。以下是一个示例配置: ```yaml mybatis-plus: mapper-locations: classpath*:/mapper/*Mapper.xml type-aliases-package: com.example.arrow_smart_toilet_back_01.mapper configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: id-type: auto ``` 如果项目使用的是自定义包名,请根据实际情况调整 `mapper-locations` 和 `type-aliases-package` 的值[^2]。 #### 3. 确保 Mapper 接口和 XML 文件匹配 `BindingException` 常见原因之一是 Mapper 接口与对应的 XML 文件未正确关联。请检查以下几点: - Mapper 接口是否被标注为 `@Mapper` 或通过 `@MapperScan` 注解注册。 - XML 文件是否放置在正确的目录下(例如 `src/main/resources/mapper/`)。 - XML 文件中的 `namespace` 属性是否与 Mapper 接口的全限定名一致。 例如,假设有一个名为 `UserMapper` 的接口,其 XML 文件应如下配置: ```xml <mapper namespace="com.example.arrow_smart_toilet_back_01.mapper.UserMapper"> <!-- SQL statements --> </mapper> ``` #### 4. 删除冗余配置类 如果项目中存在手动编写的 Druid 数据源配置类(如 `MysqlDruidConfiguration.java` 和 `DruidDataSourceProperties.java`),建议删除这些类。Spring Boot 已经内置了对 Druid 的支持,手动配置可能会导致冲突[^3]。 #### 5. 实现分页功能 如果需要实现分页功能,可以引入 `PageHelper` 相关依赖。以下是 Gradle 配置示例: ```gradle dependencies { implementation 'com.github.pagehelper:pagehelper-spring-boot-starter:2.1.0' } ``` 同时,确保在服务层代码中正确调用分页方法。例如: ```java import com.baomidou.mybatisplus.extension.plugins.pagination.Page; public List<User> getUserList(int page, int size) { Page<User> userPage = new Page<>(page, size); return userMapper.selectPage(userPage, null).getRecords(); } ``` #### 6. 调试与验证 如果问题仍然存在,可以通过以下方式排查: - 启用 MyBatis 日志输出,检查 SQL 执行情况。 - 确保项目构建后,XML 文件已正确打包到 `resources` 目录下。 --- ### 示例代码 以下是一个完整的 Mapper 接口和 XML 文件示例: #### Mapper 接口 ```java package com.example.arrow_smart_toilet_back_01.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.arrow_smart_toilet_back_01.entity.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends BaseMapper<User> { } ``` #### XML 文件 ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.arrow_smart_toilet_back_01.mapper.UserMapper"> <select id="selectById" resultType="com.example.arrow_smart_toilet_back_01.entity.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值