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

14 天前
 tiRolin

我做了一个简单的 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>

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

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

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

https://www.bilibili.com/video/BV1x6xXe2Erp/
tiRolin
14 天前
@codingadog 并非是路径问题,我已经在 pom 文件中加了对应的配置了, 我很确定编译后的项目是由 XML 这个文件的,事实上,当我在 XML 里自定义方法的时候,也可以正常调用,问题在于,mybatisplus 应该提供给我事先设置好的方法,但当我调用他们的时候,总是会报错我的问题,主要在这里
tiRolin
14 天前
@montaro2017 并非是路径问题,我已经在 pom 文件中加了对应的配置了, 我很确定编译后的项目是由 XML 这个文件的,事实上,当我在 XML 里自定义方法的时候,也可以正常调用,问题在于,mybatisplus 应该提供给我事先设置好的方法,但当我调用他们的时候,总是会报错我的问题,主要在这里
montaro2017
14 天前
你的 BaseService 全名是什么?
tiRolin
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
14 天前
@tiRolin #11 你在这里打个断点看看有没有执行,你的 BaseService 不会是从公司项目里拷进来的吧,我感觉你可能少拷了什么
tiRolin
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
13 天前
long count = this.count(queryWrapper);

改成
long count = this.baseTestUserMapper.count(queryWrapper);
试试,你这个问题应该就是楼上说的,你的 BaseService 有问题,导致他是直接去执行 mapper 的方法,然后提示找不到映射方法,或者你把这个 BaseService 换成 mybatis plus 的继承 service 也可以
montaro2017
13 天前
@tiRolin #13 mybatis plus 的内置方法是通过注入实现的,count 方法没注入当然会报错,我认为你也没必要一定得继承 BaseService 吧,先继承 ServiceImpl 看下正不正常
tiRolin
13 天前
@night98 不行,我试了下仍然报这个异常,我使用 IService 的方式来实现接口也同样报这个问题
tiRolin
13 天前
@montaro2017 我试了下,通过 IService 的方式来实现接口也同样报这个异常,我的意思是我通过在 Service 接口中继承 IService ,然后构建一个 Service 实现类,这个实现类继承 ServiceImpl 的方式来实现方法调用,也是同样报这个问题
night98
13 天前
@tiRolin #16 你还要检查一下你的 BaseTestUserMapper 是否继承的是来自于 mybatis plus 的 BaseMapper ,必须要两者统一才行,如果是用的你公司的依赖,那么他可能是用的其他的方式实现的
Edaa
11 天前
用下下面的依赖试一下?
```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
11 天前
@tiRolin #17 你先把引入的依赖去掉,在使用 ServiceImpl 试试

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1077591

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX