跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

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

发表于

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

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

下面是我的项目本体结构

20241004062901.png

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

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>

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

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.