解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):xxx问题

导读:本篇文章讲解 解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):xxx问题,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1. 复现问题

今天在测试项目时,如下代码所示:

@SpringBootTest
class LowCodeApplicationTests {

  @Autowired private Userservice userservice;

  @Test
  void contextLoads() {
    List<User> users = userservice.queryByParam("测试");
    for (User user : users) {
      System.out.println(user);
    }
  }

出现如下错误信息:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.cloud.lowcode.mapper.UserMapper.queryByParam
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53)
	at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:108)
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
	at org.apache.ibatis.util.MapUtil.computeIfAbsent(MapUtil.java:35)
	at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:95)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
	at com.sun.proxy.$Proxy109.queryByParam(Unknown Source)
	at com.cloud.lowcode.service.impl.userServiceImpl.queryByParam(userServiceImpl.java:25)
	at com.cloud.lowcode.service.impl.userServiceImpl$$FastClassBySpringCGLIB$$298dc132.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
	at com.cloud.lowcode.service.impl.userServiceImpl$$EnhancerBySpringCGLIB$$7566cc.queryByParam(<generated>)
	at com.cloud.lowcode.LowCodeApplicationTests.contextLoads(LowCodeApplicationTests.java:19)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
	....

即错误信息为:nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.cloud.lowcode.mapper.UserMapper.queryByParam

2. 分析问题

将该错误信息翻译成中文为嵌套异常是 org.apache.ibatis.binding.BindingException: 无法绑定当前com.cloud.lowcode.mapper包下的UserMapper类的queryByParam方法

2.1 检查启动类的配置

检查启动类是否加上MapperScan,如下已加上该注解:

@SpringBootApplication
@MapperScan("com.**.mapper")
public class LowCodeApplication {

    public static void main(String[] args) {
        SpringApplication.run(LowCodeApplication.class, args);
    }

}

2.2 检查xml文件对应java类的配置是否有误

如下是userMapper.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.cloud.lowcode.mapper.UserMapper">
    <select id="queryByParam" parameterType="string" resultType="com.cloud.lowcode.entity.po.User">
        SELECT * FROM `user`
        <where>
            <if test="username != null || username.length > 0">
                name = #{username}
            </if>
        </where>
    </select>
</mapper>

再看对应的UserMapper.java类,如下所示:

package com.cloud.lowcode.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.cloud.lowcode.entity.po.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * @author 念兮为美
 * @datetime 2022/11/28 13:51
 * @desc 用户实体类
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
  List<User> queryByParam(String username);
}

userMapper.xmluserMapper.java对应如下:

  • userMapper.xmlUserMapper.java的名字一样(不包括后缀名)

  • userMapper.xml中的namespacecom.cloud.lowcode.mapper.UserMapper对应UserMapper.java中的包名+类名,没问题

  • userMapper.xml中的idqueryByParam对应UserMapper.java中的方法名,没问题

  • userMapper.xml中的参数类型parameterTypestring对应UserMapper.java中的queryByParam字符串类型,没问题

  • 经检查resultType也没问题

2.3 检查application.yml文件的mybatis配置是否有误

如下是application.yml配置的mybatis信息:

#mybatis配置
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath*:/mapper/*Mapper.xml
  type-aliases-package: com.cloud.lowcode.entity.po

该配置信息没有问题。

2.4 检查pon.xml文件

<build>
	<resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.yml</include>
            </includes>
        </resource>
    </resources>
</build>

pom.xml中已配置了resource信息,也没有问题。

以上四种情况,都没有问题,那么,问题出现在哪里呢?

于是,再去看mybatis的依赖,如下所示:

<properties>
    <java.version>1.8</java.version>
    <mybatisStarter.version>2.2.2</mybatisStarter.version>
    <baomidouStarter.version>1.0.5</baomidouStarter.version>
    <baomidouMybatisPlus.version>2.1.9</baomidouMybatisPlus.version>
</properties>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatisStarter.version}</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatisplus-spring-boot-starter</artifactId>
    <version>${baomidouStarter.version}</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>${baomidouMybatisPlus.version}</version>
</dependency>

忽然发现,我引入了baomidou包,因而,配置文件application.yml需要修改。

3. 解决问题

由于引入baomidou包,,配置文件application.yml中的mybatis便不能这样配置:

#mybatis配置
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath*:/mapper/*Mapper.xml
  type-aliases-package: com.cloud.lowcode.entity.po

而应该如是配置:

#mybatis配置
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath*:/mapper/*Mapper.xml
  type-aliases-package: com.cloud.lowcode.entity.po

修改后,重新启动测试类,如下代码所示:

在这里插入图片描述

4. 总结问题

  1. 检查启动类是否加上MapperScan注解

  2. 检查xml文件对应java类的配置是否有误

  3. 检查application.yml文件的mybatis配置是否有误

  4. 检查pom.xml文件中是否引入baomidou

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99115.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!