解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)异常

简介: 解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)异常

解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)异常

1、问题描述

今天在把普通的SpringBoot项目改造成SpringBoot微服务分布式架构项目的时候遇到一个问题:

image-20230815092849732

前端项目运行时显示500错误,无法显示,于是来到后台查看报错

消费者端如下:

image-20230815093614611

提供者端:

image-20230815093715439

具体报错:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.team.news.mapper.NewsDetailMapper.selectByExample
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.1.jar:3.5.1]
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53) ~[mybatis-3.5.1.jar:3.5.1]
    at org.apache.ibatis.binding.MapperProxy.lambda$cachedMapperMethod$0(MapperProxy.java:62) ~[mybatis-3.5.1.jar:3.5.1]

我只复制了关键几行,其中指出问题的是:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.team.news.mapper.NewsDetailMapper.selectByExample

大概意思就是:错误绑定(没有找到):com.team.news.mapper.NewsDetailMapper.selectByExample

说我的NewsDetailMapper.selectByExample类中并没有找到selectByExample方法。

2、解决思路

一般网上会提供几种解决方案:

1、xml文件名与mapper接口名不一致

image-20230815094728972

一致,排除

2、xml文件中namespace与mapper接口的类名不一致

<mapper namespace="com.team.news.mapper.NewsDetailMapper">

可以看到,是一致的,排除

3、xml文件中的方法名和mapper接口方法名不一致

image-20230815095205412

点插件的这个图标,跳转到xml文件中的对应的映射:

<select id="selectByExample" parameterType="com.team.news.entity.NewsDetailExample" resultMap="BaseResultMap">
    select
        <if test="distinct">
              distinct
        </if>
        'false' as QUERYID,
        <include refid="Base_Column_List" />
            from news_detail
        <if test="_parameter != null">
              <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
              order by ${orderByClause}
        </if>
</select>

id,parameterType,包括resultMap都检查完毕后,没有问题

4、在主启动类上没有标注@MapperScan或在mapper接口类上没有标注@Mapper

首先声明这两个注解不能同时使用,用一个就够了,查看我的启动类:

@SpringBootApplication
@EnableDiscoveryClient
@EnableTransactionManagement
@MapperScan(value = "com.team.news.mapper")
public class NewsProviderApplication {
   
   

    public static void main(String[] args) {
   
   
        SpringApplication.run(NewsProviderApplication.class, args);
    }

}

注解也是存在的,并且路径也是正确的

那么,接下来,问题出在哪儿呢?基本上已经把能排除的都排除了。依旧是这个问题。

接下来是我的解决方案,感觉也是最容易被忽略的部分:

5、在pom文件中添加相关资源类打包配置

之后我鼓捣了一下午,猛然在以前的一个项目里看到了pom文件中的一个配置:

<build>
    <!--配置相关的资源进行打包-->
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>
    </resources>
</build>

这是将包下的xml资源和resources包下的配置类文件进行对应打包,并将其编译成为.class运行文件

于是我查看了我的mapper工程:

image-20230815100039766

打开这个target=>classes:

image-20230815100124067

在mapper包下果然只看到了两个mapper类,并没有看到对应的xml文件,那么在运行时检测不到对应映射的xml文件,自然找不到对应的方法了。

于是我在该工程下的pom文件中添加:

<build>
    <!--配置相关的资源进行打包-->
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>
    </resources>
</build>

再次运行,再打开target=>classes=>mapper:

image-20230815100533718

可以看到所有的xml文件都已经打包完成,再次项目功能正常跑通。

相关文章
|
2月前
|
Prometheus Cloud Native 关系型数据库
实时计算 Flink版操作报错合集之实时计算 Flink版操作报错合集之当从保存点恢复并添加新的表时,出现了org.apache.flink.util.FlinkRuntimeException异常,该怎么办
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
|
2月前
|
Java 数据库连接 mybatis
Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid
Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid
|
3月前
|
XML Java 数据库连接
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):XXXXX
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):XXXXX
|
3月前
|
IDE Java 应用服务中间件
解决org.apache.jasper.JasperException异常
解决org.apache.jasper.JasperException异常
|
4月前
|
XML Java 数据库连接
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.forum.d
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.forum.d
46 1
|
3月前
|
Java 数据库连接 mybatis
【已解决】nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘qcBizname‘ not found
【已解决】nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘qcBizname‘ not found
99 0
|
25天前
|
存储 消息中间件 Java
Apache Flink 实践问题之原生TM UI日志问题如何解决
Apache Flink 实践问题之原生TM UI日志问题如何解决
31 1
|
9天前
|
SQL 消息中间件 关系型数据库
Apache Doris Flink Connector 24.0.0 版本正式发布
该版本新增了对 Flink 1.20 的支持,并支持通过 Arrow Flight SQL 高速读取 Doris 中数据。
|
23天前
|
消息中间件 监控 数据挖掘
基于RabbitMQ与Apache Flink构建实时分析系统
【8月更文第28天】本文将介绍如何利用RabbitMQ作为数据源,结合Apache Flink进行实时数据分析。我们将构建一个简单的实时分析系统,该系统能够接收来自不同来源的数据,对数据进行实时处理,并将结果输出到另一个队列或存储系统中。
90 2
|
25天前
|
消息中间件 分布式计算 Hadoop
Apache Flink 实践问题之Flume与Hadoop之间的物理墙问题如何解决
Apache Flink 实践问题之Flume与Hadoop之间的物理墙问题如何解决
35 3