解决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文件都已经打包完成,再次项目功能正常跑通。

相关文章
|
3月前
|
Prometheus Cloud Native 关系型数据库
实时计算 Flink版操作报错合集之实时计算 Flink版操作报错合集之当从保存点恢复并添加新的表时,出现了org.apache.flink.util.FlinkRuntimeException异常,该怎么办
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
|
3月前
|
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
|
4月前
|
XML Java 数据库连接
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):XXXXX
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):XXXXX
|
4月前
|
IDE Java 应用服务中间件
解决org.apache.jasper.JasperException异常
解决org.apache.jasper.JasperException异常
|
5月前
|
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
56 1
|
4月前
|
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
126 0
|
2月前
|
存储 消息中间件 Java
Apache Flink 实践问题之原生TM UI日志问题如何解决
Apache Flink 实践问题之原生TM UI日志问题如何解决
42 1
|
3天前
|
SQL Java API
Apache Flink 2.0-preview released
Apache Flink 社区正积极筹备 Flink 2.0 的发布,这是自 Flink 1.0 发布以来的首个重大更新。Flink 2.0 将引入多项激动人心的功能和改进,包括存算分离状态管理、物化表、批作业自适应执行等,同时也包含了一些不兼容的变更。目前提供的预览版旨在让用户提前尝试新功能并收集反馈,但不建议在生产环境中使用。
229 4
Apache Flink 2.0-preview released
|
8天前
|
存储 缓存 算法
分布式锁服务深度解析:以Apache Flink的Checkpointing机制为例
【10月更文挑战第7天】在分布式系统中,多个进程或节点可能需要同时访问和操作共享资源。为了确保数据的一致性和系统的稳定性,我们需要一种机制来协调这些进程或节点的访问,避免并发冲突和竞态条件。分布式锁服务正是为此而生的一种解决方案。它通过在网络环境中实现锁机制,确保同一时间只有一个进程或节点能够访问和操作共享资源。
25 3
|
1月前
|
SQL 消息中间件 关系型数据库
Apache Doris Flink Connector 24.0.0 版本正式发布
该版本新增了对 Flink 1.20 的支持,并支持通过 Arrow Flight SQL 高速读取 Doris 中数据。

推荐镜像

更多