V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tiRolin
V2EX  ›  Java

SpringBoot 部分 xml 接口报 Invalid bound statement (not found)问题怎么解决?

  •  
  •   tiRolin · 6 天前 · 1362 次点击

    做毕设的时候遇到的这个问题

    首先我的项目在不适用 XML 的前提下的 CURD 都是成功的,但是只要一使用 XML ,那么就报上面的异常,项目结构如下

    我的 Spring 版本是 3.4.2 、mybatis-plus 版本是 3.5.9

    Mapper 接口中加入了 @Mapper 注解,启动类里也加入了 MapperScan 扫描

    resource 中的 xml 我是在 application 中指定了扫描路径的

    mybatis-plus:
      mapper-locations: classpath*:/mapper/**/*.xml
    

    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.rolin.orange.orangecommon.mapper.UserMapper">
    
        <select id="getAll" resultType="com.rolin.orange.orangecommon.model.user.entity.User">
            select * from user
        </select>
    </mapper>
    
    

    Mapper 代码也没有问题,可以成功编译,可以成功启动

    在编译后的结果里,可以找到对应的 xml 文件

    感觉不管是哪里都没有问题,但是只要调用接口,就会报下面的异常

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rolin.orange.orangecommon.mapper.UserMapper.getAll
    	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:229) ~[mybatis-3.5.16.jar:3.5.16]
    	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:50) ~[mybatis-plus-core-3.5.9.jar:3.5.9]
    	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedInvoker$0(MybatisMapperProxy.java:99) ~[mybatis-plus-core-3.5.9.jar:3.5.9]
    	at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) ~[na:na]
    	at org.apache.ibatis.util.MapUtil.computeIfAbsent(MapUtil.java:36) ~[mybatis-3.5.16.jar:3.5.16]
    	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedInvoker(MybatisMapperProxy.java:97) ~[mybatis-plus-core-3.5.9.jar:3.5.9]
    	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) ~[mybatis-plus-core-3.5.9.jar:3.5.9]
    	at jdk.proxy2/jdk.proxy2.$Proxy98.getAll(Unknown Source) ~[na:na]
    	at com.rolin.orange.orangecommon.service.UserService.getAll(UserService.java:84) ~[classes/:na]
    	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
    	at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
    

    我最后怀疑会是依赖的问题,但是查了下,springboot3.4.2 对应的 mybatisplus 版本就是 3.5.9 ,这个应该也没问题

    根据上一个帖子下面的回复,是我 xml 的路径配置有问题,所以我改动了结构,将 xml 文件直接放在 mapper 下,具体来说是下面这样的

    在编译后的文件里也能正确找到这两个 xml 文件

    启动之后的情况是,对于 UserMapper 的接口,是可以正确调用的,但是对于 Attachment 的接口,却还是报上面的问题

    AttachmentMapper 的代码如下

    @Mapper
    public interface AttachmentMapper extends BaseMapper<Attachment> {
    
        List<Attachment> findNeedCleared();
    
    }
    

    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.rolin.orange.orangecommon.mapper.AttachmentMapper">
    
        <select id="findNeedCleared" resultType="com.rolin.orange.orangecommon.model.attachment.entity.Attachment"
                databaseId="mysql">
            select *
            from attachment
            where confirmation_flag=0
              and to_days(now()) - to_days(update_date)>30
              and status=1
        </select>
        
    </mapper>
    

    我是哪里出了问题?这个问题又该如何解决呢?有没有懂得大佬来救下,小弟我是感激不尽啊

    17 条回复    2025-02-16 23:15:16 +08:00
    Suaxi
        1
    Suaxi  
       6 天前 via Android
    mapper-locations: classpath*:/mapper/*.xml

    顺便再检查下主启动类有没有加 @MapperScan() 扫描注解
    tiRolin
        2
    tiRolin  
    OP
       6 天前
    @Suaxi 加了,按照你的方法并没有解决这个问题
    EMMMMMMMMM
        3
    EMMMMMMMMM  
       6 天前 via Android
    大于号转义一下试试
    AkaGhost
        4
    AkaGhost  
       5 天前 via Android
    跟随了最佳实践(放在 resources 里)的话,你可以试试直接把 mapper-locations 的设置从 properties 里面移除,clean 一下再试试看,尽量不要手动指定避免引入人为错误。
    nice2cu
        5
    nice2cu  
       5 天前
    mybatis-plus:
    mapper-locations: classpath*:/mapper/**/*.xml

    你是 mapper 开头的路径 但是看你代码是不是不对
    RedBeanIce
        6
    RedBeanIce  
       5 天前
    你现在还是 mubatis ,不是 plus ,,有个 config 配置类
    Leoking222
        7
    Leoking222  
       5 天前
    @nice2cu 是的,他的包名和他的配置文件不符合
    billbob
        8
    billbob  
       5 天前
    你都用 3.4.2 了还玩 mybatis.

    https://github.com/vnobo/plate 拿这个项目当作示例,学习项目试试
    listen2wind
        9
    listen2wind  
       5 天前
    这里的问题?
    Van426326
        10
    Van426326  
       5 天前
    个人觉得还是对应版本的问题 很久之前升级 springboot 大版本的时候,因为这个问题也是焦头烂额。 后来还是更换 mybatisplus 的版本解决的
    tiRolin
        11
    tiRolin  
    OP
       5 天前
    @RedBeanIce 这个配置类里其实只配置了 swagger 的内容,mybatis 我确定我没配置
    tiRolin
        12
    tiRolin  
    OP
       5 天前
    @AkaGhost 试过了,无法解决这个问题

    @nice2cu 已经将 mapper 的路径改为 classpath*:/mapper/*.xml ,未能解决该问题

    @listen2wind 不是这里的问题,已经尝试过了,解决不了

    @Van426326 可以麻烦你告知我一下当时你更换的是什么版本的 mybatisplus 吗?
    ldyisbest
        14
    ldyisbest  
       4 天前
    日志等级设置为 debug ,看下启动日志,依赖是否都正常加载
    0xD800
        15
    0xD800  
       4 天前 via Android
    @tiRolin 遇到过 根本找不到原因,后面全部用注解才跑的起来
    tiRolin
        16
    tiRolin  
    OP
       4 天前
    @0xD800 已经找到这个问题了,问题原因就在于我的 databaseID ,删掉就可以跑了,这个坑真的无语了,没有日志只能猜,唐完了 mybatisplus
    0xD800
        17
    0xD800  
       3 天前
    @tiRolin data base id 是啥 在哪配置的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1181 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 23:12 · PVG 07:12 · LAX 15:12 · JFK 18:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.