亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis中的異常BindingException詳解

 更新時(shí)間:2024年01月18日 08:58:51   作者:小白不很白  
這篇文章主要介紹了mybatis中的異常BindingException詳解,此異常是mybatis中拋出的,意思是使用的這個(gè)方法找到,但是因?yàn)閙apperScan()已經(jīng)掃描到了Mapper類了,在綁定Mapper.xml時(shí)沒有綁定到導(dǎo)致的,需要的朋友可以參考下

BindingException異常

此異常是mybatis中拋出的。

意思是使用的這個(gè)方法找到,但是因?yàn)閙apperScan()已經(jīng)掃描到了Mapper類了,在綁定Mapper.xml時(shí)沒有綁定到導(dǎo)致的。

具體異常信息

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的步驟

首先要?jiǎng)?chuàng)建一個(gè)interface接口Mapper類。

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

在啟動(dòng)類或者配置類加掃描Mapper類的路徑。使用的是@MapperScan注解。里面配置的是Mapper類的包路徑。

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

在資源文件夾下配置對(duì)應(yīng)mapper.xml文件,并且寫對(duì)應(yīng)上面方法(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

出現(xiàn)BindingException原因分析

第一種

如果mapper.xml中沒有寫對(duì)應(yīng)的方法在啟動(dòng)項(xiàng)目的時(shí)候不會(huì)拋出該異常。而在用到該方法時(shí)會(huì)拋出異常

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 對(duì)象是里面有很多的map。其中一個(gè)就是掃描mapper類和mapper.xml文件生成的每個(gè)方法對(duì)應(yīng)的sql數(shù)據(jù)。mappedStatements key對(duì)應(yīng)的是mapper類里面的全路徑方法名比如:com.xiaobai.front.cms.dao.mapper.PageTemplateMapper.findPageTemplateBasicInfoList。value對(duì)應(yīng)的就是mapper.xml的信息對(duì)象。
  2. 當(dāng)在調(diào)用這個(gè)findPageTemplateBasicInfoList方法的時(shí)候由于mapper.xml中沒有對(duì)應(yīng)的綁定這個(gè)方法,mappedStatements中就獲取不到數(shù)據(jù)。
  3. 因?yàn)閙appedStatements沒有數(shù)據(jù)所以resolveMappedStatement方法返回null。
  4. 所以最后會(huì)拋出throw new BindingException("Invalid bound statement (not found): "
  5. mapperInterface.getName() + “.” + methodName);

第二種

沒有配置@MapperScan(“com.xiaobai.front.cms.dao.mapper”)執(zhí)行某方法時(shí)也會(huì)拋出此異常

 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。
  • 導(dǎo)致mappedStatements里面沒有數(shù)據(jù)。所以在使用時(shí)找不到。就會(huì)拋出該異常。

到此這篇關(guān)于Java開發(fā)中的異常BindingException詳解的文章就介紹到這了,更多相關(guān)BindingException異常內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中常用的設(shè)計(jì)模式之裝飾器模式詳解

    Java中常用的設(shè)計(jì)模式之裝飾器模式詳解

    這篇文章主要為大家詳細(xì)介紹了Java中常用的設(shè)計(jì)模式之裝飾器模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Springboot獲取jar包中resources資源目錄下的文件

    Springboot獲取jar包中resources資源目錄下的文件

    今天在項(xiàng)目中遇到一個(gè)業(yè)務(wù)場(chǎng)景,需要用到resources資源目錄下的文件,本文主要介紹了Springboot獲取jar包中resources資源目錄下的文件,感興趣的可以了解一下
    2023-12-12
  • SpringBoot3集成和使用Jasypt的代碼詳解

    SpringBoot3集成和使用Jasypt的代碼詳解

    隨著信息安全的日益受到重視,加密敏感數(shù)據(jù)在應(yīng)用程序中變得越來越重要,Jasypt作為一個(gè)簡(jiǎn)化Java應(yīng)用程序中數(shù)據(jù)加密的工具,為開發(fā)者提供了一種便捷而靈活的加密解決方案,本文將深入解析Jasypt的工作原理,需要的朋友可以參考下
    2024-01-01
  • ThreadLocal數(shù)據(jù)存儲(chǔ)結(jié)構(gòu)原理解析

    ThreadLocal數(shù)據(jù)存儲(chǔ)結(jié)構(gòu)原理解析

    這篇文章主要為大家介紹了ThreadLocal數(shù)據(jù)存儲(chǔ)結(jié)構(gòu)原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Java實(shí)現(xiàn)的可選擇及拖拽圖片的面板功能【基于swing組件】

    Java實(shí)現(xiàn)的可選擇及拖拽圖片的面板功能【基于swing組件】

    這篇文章主要介紹了Java實(shí)現(xiàn)的可選擇及拖拽圖片的面板功能,涉及java基于swing組件選擇與操作圖片元素的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-01-01
  • Java導(dǎo)出CSV文件的方法

    Java導(dǎo)出CSV文件的方法

    這篇文章主要為大家詳細(xì)介紹了Java導(dǎo)出CSV文件的方法,分頁(yè)查詢大數(shù)據(jù)量,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 老生常談Java?網(wǎng)絡(luò)編程?——?Socket?詳解

    老生常談Java?網(wǎng)絡(luò)編程?——?Socket?詳解

    這篇文章主要介紹了Java?網(wǎng)絡(luò)編程?——?Socket?相關(guān)知識(shí),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • 圖書管理系統(tǒng)java版

    圖書管理系統(tǒng)java版

    這篇文章主要為大家詳細(xì)介紹了java版的圖書管理系統(tǒng),通過實(shí)例為大家快速掌握數(shù)據(jù)庫(kù)編程技術(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • IDEA中使用Git的圖文教程

    IDEA中使用Git的圖文教程

    本文主要介紹了IDEA中使用Git的圖文教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • java生成excel并導(dǎo)出到對(duì)應(yīng)位置的方式

    java生成excel并導(dǎo)出到對(duì)應(yīng)位置的方式

    這篇文章主要介紹了java生成excel并導(dǎo)出到對(duì)應(yīng)位置的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評(píng)論