解决mybatis/mybatis plus报错:Invalid bound statement (not found) 的方法汇总

2024-05-28 13:12

本文主要是介绍解决mybatis/mybatis plus报错:Invalid bound statement (not found) 的方法汇总,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题,即在mybatis中dao接口与mapper配置文件在做映射绑定的时候接口与xml不匹配,要么是找不到,要么是找到了却匹配不到。

  我的问题是项目没有把最新的方法xml打包进项目,mybatis-plus从xml中找到与dao对应的接口名称。按第6种方法解决了。

  若大家遇到这个错,首页到项目打到处检查mapper的xml中是否有最新方法。不然,你按网上的方法查找很久才能找到问题点。

  

我的maven项目引入mybatis-plus的pom.xml配置如下:

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency>

我的yml配置如下:

mybatis-plus:configuration:map-underscore-to-camel-case: trueauto-mapping-behavior: full# log-impl: org.apache.ibatis.logging.stdout.StdOutImplmapper-locations: classpath*:/**/mapper/**/*.xmltype-aliases-package: com.tfq.dao

我在CategoryBrandRelationDao.xml中添加了一个方法updateCategory。

<?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.tfq.dao.CategoryBrandRelationDao"><!-- 可根据自己的需求,是否要使用 --><resultMap type="com.tfq.entity.CategoryBrandRelationEntity" id="categoryBrandRelationMap"><result property="id" column="id"/><result property="brandId" column="brand_id"/><result property="catelogId" column="catelog_id"/><result property="brandName" column="brand_name"/><result property="catelogName" column="catelog_name"/></resultMap><update id="updateCategory">UPDATE pms_category_brand_relationSET catelog_name=#{name}WHERE catelog_id = #{catId}</update></mapper>

于是我google搜可能出现的问题,按网上汇总问题依次排查:

1、检查xml文件的namespace是否对应接口,要是全路径。

xml文件名不需要和接口名一致,namespace和接口全类名一致即可。

2、xml中的函数id和接口中的函数名是否对得上,参数类型、返回值类型是否对得上

3、去看输出目录中有没有xml映射文件,maven项目默认把资源文件放在src/main/resources下,默认只识别src/main/resources下的资源文件。

如果你把xml映射文件等资源文件放到src/main/java的某个目录下,识别不了,需要在pom.xml中配置一下:

<resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes><filtering>true</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes><filtering>true</filtering></resource></resources>

4、看一下mybatis的配置在yml中对不对

我的项目yml配置在上面已贴出来。

我的mapper.xml文件都放到resources的标红目录。而Sprinboot启动时 log中Property 'mapperLocations' was not specified这句提示开始没引起了我的注意,后面找到是配置的xml的文件中update没被加载到Mybatis-plus的缓存中才看到。其实在yml中配置了mybatis-plus.mapper-locations=classpath*:**/mapper/xml/*.xml(没用),启动器没扫描到新加的xml中方法,十分奇怪。

注意:Maven 多模块项目的扫描路径需以 classpath*: 开头 (即加载多个 jar 包下的 XML 文件)

5、看一下xml映射文件是否带了后缀名.xml

这个很容易被忽略,一次SpringBoot中使用MyBatis时我调了半天,其它可能性都被排除了,愣是找不到原因,最后发现是我创建映射文件时直接输入UserDaoMapper,没带后缀.xml。

你不带后缀.xml,IDEA根据文件内容能识别它是xml文件,显示的图标也是xml文件的,但不带.xml后缀就不是映射文件,运行时识别不了。

我按以上方法都试了,还是报相同错。这让我很纳闷。于我开始根据mybatis-plus报错的源代码进行debug,首先进入MybatisMapperMethod.java

public class MybatisMapperMethod {public MybatisMapperMethod(Class<?> mapperInterface, Method method, Configuration config) {//找dao接口与xml中配置的方法名称进行匹配入口this.command = new MapperMethod.SqlCommand(config, mapperInterface, method);this.method = new MapperMethod.MethodSignature(config, mapperInterface, method);}}

在进入MapperMethod.java类

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {final String methodName = method.getName();final Class<?> declaringClass = method.getDeclaringClass();//这个方法从mapper的xml中加载所有的配置数据,然后把dao接口名称与xml中配置对比返回一个mapper对象。MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,configuration);if (ms == null) {if (method.getAnnotation(Flush.class) != null) {name = null;type = SqlCommandType.FLUSH;} else {//如果查找的mapper对象为空就抛出dao接口与xml不一致。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);}}}

在resolveMappedStatement方法

private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,Class<?> declaringClass, Configuration configuration) {String statementId = mapperInterface.getName() + "." + methodName;//特别注意configuration,它是加载你项目中配置的所有mapper的xml,若你发现这里面没有,//则是项目启动时没有把xml最新数据加载,然后去检查你的配置。if (configuration.hasStatement(statementId)) {return configuration.getMappedStatement(statementId);} else if (mapperInterface.equals(declaringClass)) {return null;}//后面代码省略...
}

解决方法

6.将配置文件放在 resource 文件夹中,对于 Maven 项目,可指定 POM 文件的 resource

<build><resources><!--指定资源的位置(xml放在resources下,可以不用指定)--><resource><directory>src/main/resources</directory></resource></resources>
</build>

加了这个配置,项目clean--->compile---->package,要你的项目就有最新的mapper的xml方法了。

参考blog:

mybatis-plus配置找不到Mapper接口路径的坑_mybatis-plus.mapper-locations-CSDN博客

https://www.cnblogs.com/chy18883701161/p/12695356.html

IDEA使用mybatis-plus 时出现的问题 Property ‘mapperLocations‘ was not specified &Invalid bound statement_property "mapperlocations-CSDN博客

这篇关于解决mybatis/mybatis plus报错:Invalid bound statement (not found) 的方法汇总的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1010667

相关文章

Ubuntu固定虚拟机ip地址的方法教程

《Ubuntu固定虚拟机ip地址的方法教程》本文详细介绍了如何在Ubuntu虚拟机中固定IP地址,包括检查和编辑`/etc/apt/sources.list`文件、更新网络配置文件以及使用Networ... 1、由于虚拟机网络是桥接,所以ip地址会不停地变化,接下来我们就讲述ip如何固定 2、如果apt安

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.

Java多线程父线程向子线程传值问题及解决

《Java多线程父线程向子线程传值问题及解决》文章总结了5种解决父子之间数据传递困扰的解决方案,包括ThreadLocal+TaskDecorator、UserUtils、CustomTaskDeco... 目录1 背景2 ThreadLocal+TaskDecorator3 RequestContextH

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤

《SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤》本文主要介绍了SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤,文中通过示例代码介绍的非常详... 目录 目标 步骤 1:确保 ProxySQL 和 mysql 主从同步已正确配置ProxySQL 的

解决JavaWeb-file.isDirectory()遇到的坑问题

《解决JavaWeb-file.isDirectory()遇到的坑问题》JavaWeb开发中,使用`file.isDirectory()`判断路径是否为文件夹时,需要特别注意:该方法只能判断已存在的文... 目录Jahttp://www.chinasem.cnvaWeb-file.isDirectory()遇

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec