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

Springboot 项目测试请求时总是报 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found),求大佬帮忙解决

  •  1
     
  •   tiRolin · 14 天前 · 1611 次点击

    我做了一个简单的 CURD 项目,但是这个项目测试接口时却总是报

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
    

    这个问题困扰了我快一天了,我一直没能解决它 下面是我的部分 Controller 和 Service 相关的代码 Controller 的部分

    @RestController
    @RequestMapping("/private/user/baseTestUser")
    public class BaseTestUserController {
    
        @Autowired
        private BaseTestUserService baseTestUserService;
    
        /**
         * 新增用戶
         * @param baseTestUserInsertBo 用戶插入請求對象
         * @return 新增用戶 id
         */
        @ApiOperation("增加用戶方法")
        @PostMapping("/add")
        @AvoidRepeatableCommit
        public ResponseDTO<Long> insert(
                @ApiParam(name = "BaseTestUserInsertBo",value = "用戶插入實體對象")
                @RequestBody @Valid BaseTestUserInsertBo baseTestUserInsertBo) {
            return ResponseDTO.ok(baseTestUserService.insert(baseTestUserInsertBo));
        }
    }
    

    Service 的部分

    @Service
    public class BaseTestUserService extends BaseService<BaseTestUserMapper, BaseTestUser> {
    
        @Autowired
        private BaseTestUserMapper baseTestUserMapper;
    
        @Transactional
        public Long insert(BaseTestUserInsertBo baseTestUserInsertBo) {
            BaseTestUser baseTestUser = baseTestUserInsertBo.toEntity();
            baseTestUser.setRoleID(1);
            exist(baseTestUser);
            insert(baseTestUser);
            return baseTestUser.getId();
        }
    
        /**
         * 用戶名唯一性檢查
         * @param baseTestUser 用戶對象
         */
        private void exist(BaseTestUser baseTestUser) {
            QueryWrapper<BaseTestUser> queryWrapper = new QueryWrapper<>();
    //        queryWrapper.lambda().ne(baseTestUser.getId() != null, BaseTestUser::getId, baseTestUser.getId());
            queryWrapper.lambda().eq(BaseTestUser::getUsername, baseTestUser.getUsername());
            long count = this.count(queryWrapper);
            BaseTestUserErrorEnum.BASE_USER_EXIST_SAME_NAME_ERROR.isTrue(count>0);
        }
    }
    

    我觉得我的代码部分是没什么问题的,但是每次代码运行到 this.count(queryWrapper);这一行的时候就会报我上面所说的异常,但是我项目编译可以通过,我甚至可以按住 ctrl 进入到 count 的字节码文件里,按说是没问题的,但是却会报问题,我尝试了很多方法都没能解决这个问题,我实在是没法了,所以我来问问各位 下面是一些有关于我项目的更多内容,方便各位解决问题

    首先我使用的 jdk 版本是 21 ,maven 版本是 3.5.3 我在 pom 文件中引入了 mybatis 相关依赖,具体内容如下

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>3.0.3</version>
            </dependency>
    

    事实上,我即使不引入这个依赖,在我的依赖中也已经存在 mybatis 和 mybatisplus 的相关内容了,但是我没引入这个依赖之前启动项目会报

    Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
    

    为了解决这个问题,我引入了这个依赖

    下面是我的项目本体结构

    我项目配置类的代码如下所示:

    spring.application.name=jarlearning21
    
    spring.datasource.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
    server.port=8081
    
    mybatis.mapper-locations= classpath*:org/example/jarlearning21/user/mapper/**/*.xml
    mybatis-plus.type-aliases-package=org.example.jarlearning21.user.model.po
    

    启动类的代码如下

    @SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
    @MapperScan("org.example.jarlearning21.user.mapper")
    public class Jarlearning21Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Jarlearning21Application.class, args);
        }
    }
    

    Mapper 的代码

    public interface BaseTestUserMapper extends BaseMapper<BaseTestUser> {}
    

    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="org.example.jarlearning21.user.mapper.BaseTestUserMapper">
    </mapper>
    

    以上就是所有内容,如果需要更多信息请随时跟我说,我会第一时间补充,先谢谢各位了

    26 条回复    2024-10-18 17:04:56 +08:00
    tiRolin
        1
    tiRolin  
    OP
       14 天前
    这几天在这里问的问题可能有些多,多有打扰,但是我都是确实解决不了才来问各位的,我在这里问的问题基本我自己是尝试过许多办法去解决都还行不通我才来问的,不是说单纯把各位当做什么好用的工具了有事就来问,我是实在没办法了才来向各位请教的
    如有打扰还望多多包涵,实在不好意思
    linmt
        2
    linmt  
       14 天前
    mybatis.mapper-locations= classpath*:org/example/jarlearning21/user/mapper/**/*.xml

    这个配置的意思是匹配文件 resources/org/example/jarlearning21/user/mapper/**/*.xml
    montaro2017
        3
    montaro2017  
       14 天前
    你的 xml 文件没放在 resource 下面,编译后 xml 文件没有写出,如果你非要放在`main/java`下面,需要在 pom.xml 中配置,或者直接放在 resource 文件夹下面
    montaro2017
        4
    montaro2017  
       14 天前
    @montaro2017 #3 你可以看你编译后的 target 目录里面有没有这个 xml 文件,看一眼就知道了,如果你把 xml 放在 resources 目录下,就没这个问题
    codingadog
        5
    codingadog  
       14 天前 via Android
    xml 路径问题
    chuck1in
        6
    chuck1in  
       14 天前
    这个论坛新手还挺少的,帮 lz 顶一下。
    chuck1in
        7
    chuck1in  
       14 天前
    对了,既然 op 都用 jdk21 了为什么不试试这个项目呢

    https://www.bilibili.com/video/BV1x6xXe2Erp/
    tiRolin
        8
    tiRolin  
    OP
       14 天前 via Android
    @codingadog 并非是路径问题,我已经在 pom 文件中加了对应的配置了, 我很确定编译后的项目是由 XML 这个文件的,事实上,当我在 XML 里自定义方法的时候,也可以正常调用,问题在于,mybatisplus 应该提供给我事先设置好的方法,但当我调用他们的时候,总是会报错我的问题,主要在这里
    tiRolin
        9
    tiRolin  
    OP
       14 天前
    @montaro2017 并非是路径问题,我已经在 pom 文件中加了对应的配置了, 我很确定编译后的项目是由 XML 这个文件的,事实上,当我在 XML 里自定义方法的时候,也可以正常调用,问题在于,mybatisplus 应该提供给我事先设置好的方法,但当我调用他们的时候,总是会报错我的问题,主要在这里
    montaro2017
        10
    montaro2017  
       14 天前
    你的 BaseService 全名是什么?
    tiRolin
        11
    tiRolin  
    OP
       14 天前
    @montaro2017 是 com.example.common.mybatis.service.BaseService;
    因为这个是我公司提供的依赖,这里我用 example 我替代掉了公司的依赖路径包名
    BaseService 中的源码我不能给你看,但是他的类是如下所示
    public class BaseService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {
    public BaseService() {
    }
    // ...略
    }
    其中略的代码内容是提供了分页、插入、删除方法
    我最初也考虑过可能是因为该依赖并没有提供 count 方法导致的 bug ,但是后面当我跳过 count 方法直接调用 insert 方法时也照样报这个异常,所以我认为原因应该不是该依赖没有提供 count 方法这么简单,问题可能是在其他地方导致的
    montaro2017
        12
    montaro2017  
       14 天前
    @tiRolin #11 你在这里打个断点看看有没有执行,你的 BaseService 不会是从公司项目里拷进来的吧,我感觉你可能少拷了什么
    tiRolin
        13
    tiRolin  
    OP
       14 天前
    @montaro2017 我在您说的地方打了断点,并没有执行,我的 BaseService 是从公司仓库里拉取到我的项目中的,具体到 pom 文件的代码是
    <dependency>
    <groupId>com.example.base</groupId>
    <artifactId>base-core</artifactId>
    <version>${com.example.version}</version>
    </dependency>
    <dependency>
    <groupId>com.example.common</groupId>
    <artifactId>common-redis</artifactId>
    <version>${com.example.version}</version>
    </dependency>
    我觉得我应该没缺少什么内容,当然也可能真缺少了,但是我现在也不确定,因为我还没搞懂这个问题到底是为什么而产生的
    night98
        14
    night98  
       13 天前
    long count = this.count(queryWrapper);

    改成
    long count = this.baseTestUserMapper.count(queryWrapper);
    试试,你这个问题应该就是楼上说的,你的 BaseService 有问题,导致他是直接去执行 mapper 的方法,然后提示找不到映射方法,或者你把这个 BaseService 换成 mybatis plus 的继承 service 也可以
    montaro2017
        15
    montaro2017  
       13 天前
    @tiRolin #13 mybatis plus 的内置方法是通过注入实现的,count 方法没注入当然会报错,我认为你也没必要一定得继承 BaseService 吧,先继承 ServiceImpl 看下正不正常
    tiRolin
        16
    tiRolin  
    OP
       13 天前
    @night98 不行,我试了下仍然报这个异常,我使用 IService 的方式来实现接口也同样报这个问题
    tiRolin
        17
    tiRolin  
    OP
       13 天前
    @montaro2017 我试了下,通过 IService 的方式来实现接口也同样报这个异常,我的意思是我通过在 Service 接口中继承 IService ,然后构建一个 Service 实现类,这个实现类继承 ServiceImpl 的方式来实现方法调用,也是同样报这个问题
    night98
        18
    night98  
       13 天前   ❤️ 1
    @tiRolin #16 你还要检查一下你的 BaseTestUserMapper 是否继承的是来自于 mybatis plus 的 BaseMapper ,必须要两者统一才行,如果是用的你公司的依赖,那么他可能是用的其他的方式实现的
    Edaa
        19
    Edaa  
       11 天前   ❤️ 1
    用下下面的依赖试一下?
    ```xml
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.6</version>
    <exclusions>
    <exclusion>
    <artifactId>mybatis-spring</artifactId>
    <groupId>org.mybatis</groupId>
    </exclusion>
    </exclusions>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>3.0.3</version>
    </dependency>
    ```
    montaro2017
        20
    montaro2017  
       11 天前   ❤️ 1
    @tiRolin #17 你先把引入的依赖去掉,在使用 ServiceImpl 试试
    tiRolin
        21
    tiRolin  
    OP
       11 天前
    @montaro2017 我解决这个问题了,问题的原因是 SpringBoot3 的版本一旦配合使用内嵌的 mybatis3 就会导致这个错误,解决这个错误的方法是将 springboot 的版本降级为 3.1.6 同时引入下面的两个依赖
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3</version>
    </dependency>

    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-core</artifactId>
    <version>3.5.3</version>
    </dependency>
    就可以完美解决该问题,之所以之前迟迟解决不了是因为一直怀疑是我的自己的配置出了问题,没想到问题是直接出在依赖上,这个依赖组合是存在未修复的 Bug 的
    tiRolin
        22
    tiRolin  
    OP
       11 天前
    @Edaa 我解决这个问题了,问题的原因是 SpringBoot3 的版本一旦配合使用内嵌的 mybatis3 就会导致这个错误,解决这个错误的方法是将 springboot 的版本降级为 3.1.6 同时引入下面的两个依赖
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3</version>
    </dependency>

    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-core</artifactId>
    <version>3.5.3</version>
    </dependency>
    就可以完美解决该问题,之所以之前迟迟解决不了是因为一直怀疑是我的自己的配置出了问题,没想到问题是直接出在依赖上,这个依赖组合是存在未修复的 Bug 的
    tiRolin
        23
    tiRolin  
    OP
       11 天前
    @night98 我解决这个问题了,问题的原因是 SpringBoot3 的版本一旦配合使用内嵌的 mybatis3 就会导致这个错误,解决这个错误的方法是将 springboot 的版本降级为 3.1.6 同时引入下面的两个依赖
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3</version>
    </dependency>

    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-core</artifactId>
    <version>3.5.3</version>
    </dependency>
    就可以完美解决该问题,之所以之前迟迟解决不了是因为一直怀疑是我的自己的配置出了问题,没想到问题是直接出在依赖上,这个依赖组合是存在未修复的 Bug 的
    RedBeanIce
        24
    RedBeanIce  
       11 天前
    在 springboot2 中,一般是你的 myabtis plus config 出了问题。
    WilliamZeyar
        25
    WilliamZeyar  
       5 小时 32 分钟前
    springboot3 mybatis-plus 依赖问题,可以升级 mybatis-plus 到 3.5.7 版本,去掉其他 mybatis 依赖,我就是这么解决的。
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
    <version>3.5.7</version>
    </dependency>
    WilliamZeyar
        26
    WilliamZeyar  
       5 小时 31 分钟前
    我的 springboot 版本是 3.3.3 mybatis-plus 版本 3.5.7
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2457 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 14:36 · PVG 22:36 · LAX 07:36 · JFK 10:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.