mybatis中的异常BindingException详解

 更新时间:2024年01月18日 08:58:51   作者:小白不很白  
这篇文章主要介绍了mybatis中的异常BindingException详解,此异常是mybatis中抛出的,意思是使用的这个方法找到,但是因为mapperScan()已经扫描到了Mapper类了,在绑定Mapper.xml时没有绑定到导致的,需要的朋友可以参考下

BindingException异常

此异常是mybatis中抛出的。

意思是使用的这个方法找到,但是因为mapperScan()已经扫描到了Mapper类了,在绑定Mapper.xml时没有绑定到导致的。

具体异常信息

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xxx.xxx.xxx.dao.mapper.PageTemplateMapper.findPageTemplateBasicInfoList
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:227)
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:49)
    at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
    at com.sun.proxy.$Proxy202.findPageTemplateBasicInfoList(Unknown Source)
.....

使用mybatis的步骤

首先要创建一个interface接口Mapper类。

package com.xiaobai.front.cms.dao.mapper;
public interface PageTemplateMapper {
     List<PageTemplateBasicInfo> findPageTemplateBasicInfoList(PageTemplateQueryDto pageTemplateQueryDto);
}

在启动类或者配置类加扫描Mapper类的路径。使用的是@MapperScan注解。里面配置的是Mapper类的包路径。

@Configuration
@MapperScan("com.xiaobai.front.cms.dao.mapper")
public class MybatisConfig {
}

在资源文件夹下配置对应mapper.xml文件,并且写对应上面方法(findPageTemplateBasicInfoList)的sql。

<?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.xiaobai.front.cms.dao.mapper.PageTemplateMapper">
  <select id="findPageTemplateBasicInfoList" resultType="com.dffl.front.cms.domain.response.PageTemplateBasicInfo">
    .......
  </select>
</mapper>

在配置文件中配置mybatis扫描mapper.xml的路径

mybatis:
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mapper/**/*Mapper.xml

出现BindingException原因分析

第一种

如果mapper.xml中没有写对应的方法在启动项目的时候不会抛出该异常。而在用到该方法时会抛出异常

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
      final String methodName = method.getName();
      final Class<?> declaringClass = method.getDeclaringClass();
      MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
          configuration);
      if (ms == null) {
        if (method.getAnnotation(Flush.class) != null) {
          name = null;
          type = SqlCommandType.FLUSH;
        } else {
          throw new BindingException("Invalid bound statement (not found): "
              + mapperInterface.getName() + "." + methodName);
        }
      } else {
        name = ms.getId();
        type = ms.getSqlCommandType();
        if (type == SqlCommandType.UNKNOWN) {
          throw new BindingException("Unknown execution method for: " + name);
        }
      }
    }
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
        Class<?> declaringClass, Configuration configuration) {
      String statementId = mapperInterface.getName() + "." + methodName;
      if (configuration.hasStatement(statementId)) {
        return configuration.getMappedStatement(statementId);
      } else if (mapperInterface.equals(declaringClass)) {
        return null;
      }
      for (Class<?> superInterface : mapperInterface.getInterfaces()) {
        if (declaringClass.isAssignableFrom(superInterface)) {
          MappedStatement ms = resolveMappedStatement(superInterface, methodName,
              declaringClass, configuration);
          if (ms != null) {
            return ms;
          }
        }
      }
      return null;
    }
  1. configuration 对象是里面有很多的map。其中一个就是扫描mapper类和mapper.xml文件生成的每个方法对应的sql数据。mappedStatements key对应的是mapper类里面的全路径方法名比如:com.xiaobai.front.cms.dao.mapper.PageTemplateMapper.findPageTemplateBasicInfoList。value对应的就是mapper.xml的信息对象。
  2. 当在调用这个findPageTemplateBasicInfoList方法的时候由于mapper.xml中没有对应的绑定这个方法,mappedStatements中就获取不到数据。
  3. 因为mappedStatements没有数据所以resolveMappedStatement方法返回null。
  4. 所以最后会抛出throw new BindingException("Invalid bound statement (not found): "
  5. mapperInterface.getName() + “.” + methodName);

第二种

没有配置@MapperScan(“com.xiaobai.front.cms.dao.mapper”)执行某方法时也会抛出此异常

 public Resource[] resolveMapperLocations() {
    ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
    List<Resource> resources = new ArrayList<Resource>();
    if (this.mapperLocations != null) {
      for (String mapperLocation : this.mapperLocations) { 
        try {
          Resource[] mappers = resourceResolver.getResources(mapperLocation);
          resources.addAll(Arrays.asList(mappers));
        } catch (IOException e) {
          // ignore
        }
      }
    }
    return resources.toArray(new Resource[resources.size()]);
  }
  • 主要原因就是没有配置扫描mapper.xml。
  • 导致mappedStatements里面没有数据。所以在使用时找不到。就会抛出该异常。

到此这篇关于Java开发中的异常BindingException详解的文章就介绍到这了,更多相关BindingException异常内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 浅析Java方法传值和传引用问题

    浅析Java方法传值和传引用问题

    这篇文章主要是对Java方法传值和传引用问题进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2013-12-12
  • java搭建一个Socket服务器响应多用户访问

    java搭建一个Socket服务器响应多用户访问

    本篇文章主要介绍了java搭建一个Socket服务器响应多用户访问,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • 深入分析Java异常

    深入分析Java异常

    本篇文章给大家详细分享了关于Java异常的相关知识点,对此有需要的朋友跟着学习下吧。
    2018-05-05
  • swagger整合gateway实现文档集中化过程

    swagger整合gateway实现文档集中化过程

    本文介绍了如何将Swagger与Spring Cloud Gateway整合,实现API文档的集中化管理,通过Spring Boot的自动装配,配置了网关Swagger资源提供程序,实现了通过gateway路由的方式聚合Swagger文档,整个过程包括了文件夹结构、自动装配文件内容等详细步骤
    2026-02-02
  • Java ArrayList如何实现生成不重复随机数

    Java ArrayList如何实现生成不重复随机数

    这篇文章主要介绍了Java ArrayList如何实现生成不重复随机数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Java中static关键字的作用解析

    Java中static关键字的作用解析

    这篇文章主要介绍了Java中static关键字的作用解析,Java 中,不能在所有类之外定义全局变量,只能通过在一个类中定义公用、静态的变量来实现一个全局变量,需要的朋友可以参考下
    2023-11-11
  • Spring MVC映射HTTP请求到Controller的处理方法

    Spring MVC映射HTTP请求到Controller的处理方法

    我们来详细分析一下如何在 Spring MVC 中将 HTTP 请求映射到 Controller 的处理方法(Handler Methods)上,以及 @RequestMapping 注解的使用方法,需要的朋友可以参考下
    2025-05-05
  • Shiro + JWT + SpringBoot应用示例代码详解

    Shiro + JWT + SpringBoot应用示例代码详解

    这篇文章主要介绍了Shiro (Shiro + JWT + SpringBoot应用),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • Java如何实现HTTP断点续传功能

    Java如何实现HTTP断点续传功能

    其实断点续传的原理很简单,就是在Http的请求上和一般的下载有所不同而已,本文将详细介绍Java如何实现HTTP断点续传功能,需要的朋友可以参考下
    2012-11-11
  • Java中的Unsafe在安全领域的使用总结和复现(实例详解)

    Java中的Unsafe在安全领域的使用总结和复现(实例详解)

    unsafe里面有很多好用的方法,比如allocateInstance可以直接创建实例对象,defineAnonymousClass可以创建一个VM匿名类(VM Anonymous Class),以及直接从内存级别修改对象的值。这篇文章主要介绍了Java中的Unsafe在安全领域的一些应用总结和复现,需要的朋友可以参考下
    2022-03-03

最新评论