[Java] [Spring boot] Mybatis generator 生成Mapper.xml无效的问题

news2025/7/27 3:59:33

报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
百度出来的解决方案:
1:Mapper.xml中的namespace不对应和mapper接口不对应

2:Mapper.xml中的方法(即id)和mapper接口中的方法名字不同或对应的方法不存在

3:返回类型不匹配(即没有正确配置ResultMap或者ResultType)

4:可能xml文件有缓存或者修改后没保存

5:可能没有配置MapperScan导致dao方法没有被扫描注入

6:配置文件中mybatis.mapper-locations或mybatis.typeAliasesPackage配置不正确
不管用

背景介绍

我是把Mybatis generator集成到自己的项目中
在这里插入图片描述
generator.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>

    <!-- 数据库驱动包位置 -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- 配置内置的或者自定义的Plugin -->
        <plugin type="com.mybatis.plugin.MysqlPaginationPlugin" />
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 数据库链接URL、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/quick_project?characterEncoding=utf8&amp;useSSL=false"
                        userId="root" password="123456">
            <property name="useInformationSchema" value="true"/>
            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.quick.user.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- mapper生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="mybatis" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.quick.user.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
        <table tableName="user" domainObjectName="User"
               enableCountByExample="true"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               selectByExampleQueryId="true"
               enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true"
               selectByPrimaryKeyQueryId="true" >
            <columnOverride column="xx" javaType="java.lang.String" jdbcType="VARCHAR" />
        </table>
    </context>
</generatorConfiguration>

特别注意下上述配置的插件com.mybatis.plugin.MysqlPaginationPlugin
这是我自己本地重写的分页插件jar包。
以下是配置的pom

 <build>
        <finalName>user-service</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>
                        src/main/resources/generator.xml
                    </configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <!--本地分页插件包--><groupId>com.mybatisplugin</groupId>
                        <artifactId>generator-mysql-plugin-page</artifactId>
                        <version>1.0</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.13</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

    </build>

项目整合

我使用的nacos配置的yaml文件

spring:
#数据库配置
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/quick_project?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: 123456
      filters: config

#设置日志级别,ERROR/WARN/INFO/DEBUG,默认是INFO以上才显示
logging:
  level:
    root: INFO

mybatis:
  mapper-locations: classpath:mybatis/*.xml
  type-aliases-package: com.quick.user.model

问题分析

以上的都配置好了,而且给种网上提出的问题都已经检查过,但是还是报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

调试

打断点在问题逻辑处
找到MapperMethod.SqlCommand方法

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
      final String methodName = method.getName();
      final Class<?> declaringClass = method.getDeclaringClass();
      MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
          configuration);
      if (ms == null) {
        if (method.getAnnotation(Flush.class) != null) {
          name = null;
          type = SqlCommandType.FLUSH;
        } else {
          throw new BindingException("Invalid bound statement (not found): "
              + mapperInterface.getName() + "." + methodName);
        }
      } else {
        name = ms.getId();
        type = ms.getSqlCommandType();
        if (type == SqlCommandType.UNKNOWN) {
          throw new BindingException("Unknown execution method for: " + name);
        }
      }
    }

通过调试分析,得出问题出在ms==null这一块上面。接下来继续分析MapperMethod.resolveMappedStatement这个方法

private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
        Class<?> declaringClass, Configuration configuration) {
      String statementId = mapperInterface.getName() + "." + methodName;
      if (configuration.hasStatement(statementId)) {
        return configuration.getMappedStatement(statementId);
      } else if (mapperInterface.equals(declaringClass)) {
        return null;
      }
      for (Class<?> superInterface : mapperInterface.getInterfaces()) {
        if (declaringClass.isAssignableFrom(superInterface)) {
          MappedStatement ms = resolveMappedStatement(superInterface, methodName,
              declaringClass, configuration);
          if (ms != null) {
            return ms;
          }
        }
      }
      return null;
    }
  }

其中configuration.hasStatement(statementId)会判断有没有这个sql对应的方法。分析发现,org.apache.ibatis.session.Configuration方法中的属性 mappedStatements 为空。
那么为什么mappedStatements为空呢,我怀疑mybatis.mapper-locations属性没有被正确加载解析,导致无法正确的找到每个接口方法对应的sql。
而且我这边也没有相关的重写sqlSessionFactory这个方法。
此时应该想到自己写了分页插件包可能导致引包冲突的问题。

结果

针对以上分析原因大概率得出包冲突造成的。因此在pom的build去掉自己本地插件包generator-mysql-plugin-page
最终的pom

<build>
        <finalName>user-service</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
<!--            <plugin>-->
<!--                <groupId>org.mybatis.generator</groupId>-->
<!--                <artifactId>mybatis-generator-maven-plugin</artifactId>-->
<!--                <version>1.3.2</version>-->
<!--                <configuration>-->
<!--                    <configurationFile>-->
<!--                        src/main/resources/generator.xml-->
<!--                    </configurationFile>-->
<!--                    <verbose>true</verbose>-->
<!--                    <overwrite>true</overwrite>-->
<!--                </configuration>-->
<!--                <dependencies>-->
<!--                    <dependency>-->
<!--                        <groupId>com.mybatisplugin</groupId>-->
<!--                        <artifactId>generator-mysql-plugin-page</artifactId>-->
<!--                        <version>1.0</version>-->
<!--                    </dependency>-->
<!--                    <dependency>-->
<!--                        <groupId>mysql</groupId>-->
<!--                        <artifactId>mysql-connector-java</artifactId>-->
<!--                        <version>8.0.13</version>-->
<!--                    </dependency>-->
<!--                </dependencies>-->
<!--            </plugin>-->
        </plugins>

    </build>

搞定Mapper.xml无效问题

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/16742.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

使用 nexus 作为 npm 私服

背景: 之前公司内部是使用的 verdaccio 作为私有前端 npm 仓库的工具的, 由于最近安装包时总是遇到问题, 发包 和 安装都比较慢, 不太确定具体是什么问题 几经解决还是没能够解决掉, 索性换一个工具试试, 公司后端的 maven 库私服是使用的 nexus, 找资料时发现 nexus 也是可…

C++,STL,,vector容器

文章目录一、vector介绍1.vector对象构造2.vector的赋值3.vector的大小4.vector末尾的添加移除操作5.vector的数据存取6.vector的插入7.vector的删除8.vector迭代器一、vector介绍 vector相当于顺序表 vector是将元素置于一个动态数组中加以管理的容器。 vector可以随机存取元…

天元宠物上市首日破发:市值蒸发约5亿元,薛元潮兄妹为实控人

11月18日&#xff0c;杭州天元宠物用品股份有限公司&#xff08;下称“天元宠物”&#xff0c;SZ:301335&#xff09;在深圳证券交易所创业板上市。本次上市&#xff0c;天元宠物的发行价格为49.98元/股&#xff0c;发行数量为2250万股&#xff0c;募资总额约为11.25亿元&#…

华玉通软发布“云雀”执行管理中间件,为智能驾驶关键应用提供确定性执行保障

11月17日&#xff0c;华玉通软&#xff08;下称“华玉”&#xff09;宣布正式发布“云雀”执行管理中间件&#xff08;LARK EM Execution Management&#xff09;。 这是继“雨燕”通信中间件&#xff08;SWIFT DDS&#xff09;后&#xff0c;华玉的又一款完全自主研发的智能驾…

[iOS]分析Mach-O文件

一、Mach-O文件介绍 我们拿到IPA文件后&#xff0c;解压后就能拿到一个与APP同名的可执行文件。 Mach-O为Mach Object文件格式的缩写&#xff0c;它是一种用于可执行文件&#xff0c;目标代码&#xff0c;动态库&#xff0c;内核转储的文件格式。 每个Mach-O文件包括一个Mach-…

Design A NearBy Friends

title: Notes of System Design No.09 — Design a Uber backend description: Design a PasteBin ’ date: 2022-05-13 18:01:58 tags: 系统设计 categories: 系统设计 00. What is Newarby Friends? 01.Functional Requirement 02. Non-Functional Requirement 03. Assump…

Java新特性(2):Java 10以后

您好&#xff0c;我是湘王&#xff0c;这是我的CSDN博客&#xff0c;欢迎您来&#xff0c;欢迎您再来&#xff5e; 虽然到目前为止Java的版本更新还没有什么惊天动地的改变&#xff0c;但总是会冒出一些有趣的小玩意。前面列举了Java9和Java10的一些特色&#xff0c;现在接着来…

shell修改永久性别名,压缩与解压缩(zip gzip bzip2)文件上传预下载(sftp)

命令别名&#xff08;永久有效&#xff09; 敲重点&#xff08;写入文件&#xff09;&#xff08;1&#xff09;仅对root有效&#xff0c;写一个命令命为hello,实现的功能为每输入一次hello命令&#xff0c;就有hello&#xff0c;everyone写入文件/file.txt中。 root用户…

[UE][C++]Assimp库安装编译,UE_Assimp插件安装使用,各种三维格式转换

[UE][C]Assimp库安装编译&#xff0c;UE_Assimp插件安装使用&#xff0c;各种三维格式转换写在前面1.作者碎碎念2.结果1.需要准备的软件2.Assimp库编译步骤3.UE_Assimp插件的安装4.UE_Assimp插件样例使用5.Assimp库各种三维格式转换小程序写在前面 1.作者碎碎念 &#xff08;…

ElasticSearch 拼音插件elasticsearch-analysis-pinyin + IK 分词器

ElasticSearch kibana 部署略 创建索引 PUT /nba_20220101 {"mappings": {"properties": {"age": {"type": "integer"},"birthDay": {"type": "date"},"birthDayStr": {"t…

软件方面的文档标准GB/T

在文档标准方面&#xff0c;主要有《软件文档管理指南》&#xff08;GB/T 16680-1996&#xff09;、《计算机软件产品开发文件编制指南》&#xff08;GB/T 8567-2006&#xff09;和《计算机软件需求说明编制指南》&#xff08;GB/T 9385-2008&#xff09;等三个标准。 1. GB/T…

C语言源代码系列-管理系统之学生信息管理系统

往期文章分享点击跳转>《导航贴》- Unity手册&#xff0c;系统实战学习点击跳转>《导航贴》- Android手册&#xff0c;重温移动开发 &#x1f449;关于作者 众所周知&#xff0c;人生是一个漫长的流程&#xff0c;不断克服困难&#xff0c;不断反思前进的过程。在这个过…

竞赛开源项目汇总

1、Kaggle Titanic 一个Kaggle竞赛的案例&#xff0c;演示基本的数据转换、分析和可视化技术 https://github.com/agconti/kaggle-titanic 2、Humpback Whale Identification 1st https://github.com/earhian/Humpback-Whale-Identification-1st- 3、Data Science Competi…

【附源码】计算机毕业设计JAVA砂石矿山管理系统

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat8.5 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; Springboot mybatis Maven Vue 等等组成&#xff0c;B/…

无代码开发工具突破数据“孤岛“,让数据“动”起来

在信息化的发展过程中&#xff0c;每个独立运作的企业和机构都会使用2-3种以上的软件&#xff0c;而在某些比较成熟的公司&#xff0c;其软件产品的数量就更加惊人了&#xff0c;一个中等规模的公司可以拥有数百套软件系统来支持公司的正常运转。 许多公司的CIO或CTO&#xff0…

智能漏电断路器主要有哪些功能?

安科瑞 华楠 ASCB1 系列智能微型断路器是安科瑞电气股份有限公司 全新推出的智慧用电产品&#xff0c;产品由智能微型断路器与智 能网关两部分组成&#xff0c;可用于对用电线路的关键电气因 素&#xff0c;如电压、电流、功率、温度、漏电、能耗等进行实 时监测&#xff0c;具…

DOX-TF/Ce6/IgG 转铁蛋白/光敏剂/单抗IgG修饰阿霉素/阿霉素白蛋白纳米粒的制备

今天要分享的科研知识是DOX-TF/Ce6/IgG 转铁蛋白/光敏剂/单抗IgG修饰阿霉素/阿霉素白蛋白纳米粒&#xff0c;下面和小编一起来看&#xff01; DOX-IgG 单抗IgG偶联阿霉素的制备&#xff1a; 设计了一种叶酸-聚乙二醇-免疫球蛋白G-阿霉素(FA-PEG-IgG-DOX)的四元靶向给药系统.其…

石子合并系列问题

石子合并 石子合并问题在网上有三个版本&#xff1a; AcWing 282. 石子合并 设有 N 堆石子排成一排&#xff0c;其编号为 1&#xff0c;2&#xff0c;3&#xff0c;…&#xff0c;N。 每堆石子有一定的质量&#xff0c;可以用一个整数来描述&#xff0c;现在要将这 N 堆石子合…

Android 编译C++

Android 编译C项目前言正文一、基本知识① 要做什么&#xff1f;② JNI是什么&#xff1f;③ NDK是什么&#xff1f;二、配置NDK三、创建新工程① 工程目录说明② 分析cpp文件③ JNI数据类型四、现有工程使用C① 创建C文件② 创建CMake③ 使用C五、源码前言 在开发过程中&#…

集采报告丨国家药品带量采购政策及趋势分析

本人在医药领域从事药品数据分析工作多年&#xff0c;前几天在了解VBP药品带量采购相关事项时遇见了一些网上不好查找答案的问题&#xff08;国家药品带量采购目录、流程、区别、数据分析、政策、执行、结果、公示、网站、意思&#xff09;&#xff0c;对此笔者将其整理出来并解…