最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis中的異常BindingException詳解

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

BindingException異常

此異常是mybatis中拋出的。

意思是使用的這個方法找到,但是因為mapperScan()已經(jīng)掃描到了Mapper類了,在綁定Mapper.xml時沒有綁定到導(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的步驟

首先要創(chuàng)建一個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 {
}

在資源文件夾下配置對應(yīng)mapper.xml文件,并且寫對應(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中沒有寫對應(yīng)的方法在啟動項目的時候不會拋出該異常。而在用到該方法時會拋出異常

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

第二種

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

 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ù)。所以在使用時找不到。就會拋出該異常。

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

相關(guān)文章

  • 淺析Java方法傳值和傳引用問題

    淺析Java方法傳值和傳引用問題

    這篇文章主要是對Java方法傳值和傳引用問題進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-12-12
  • java搭建一個Socket服務(wù)器響應(yīng)多用戶訪問

    java搭建一個Socket服務(wù)器響應(yīng)多用戶訪問

    本篇文章主要介紹了java搭建一個Socket服務(wù)器響應(yīng)多用戶訪問,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • 深入分析Java異常

    深入分析Java異常

    本篇文章給大家詳細(xì)分享了關(guān)于Java異常的相關(guān)知識點,對此有需要的朋友跟著學(xué)習(xí)下吧。
    2018-05-05
  • swagger整合gateway實現(xiàn)文檔集中化過程

    swagger整合gateway實現(xiàn)文檔集中化過程

    本文介紹了如何將Swagger與Spring?Cloud?Gateway整合,實現(xiàn)API文檔的集中化管理,通過Spring?Boot的自動裝配,配置了網(wǎng)關(guān)Swagger資源提供程序,實現(xiàn)了通過gateway路由的方式聚合Swagger文檔,整個過程包括了文件夾結(jié)構(gòu)、自動裝配文件內(nèi)容等詳細(xì)步驟
    2026-02-02
  • Java ArrayList如何實現(xiàn)生成不重復(fù)隨機數(shù)

    Java ArrayList如何實現(xiàn)生成不重復(fù)隨機數(shù)

    這篇文章主要介紹了Java ArrayList如何實現(xiàn)生成不重復(fù)隨機數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • Java中static關(guān)鍵字的作用解析

    Java中static關(guān)鍵字的作用解析

    這篇文章主要介紹了Java中static關(guān)鍵字的作用解析,Java 中,不能在所有類之外定義全局變量,只能通過在一個類中定義公用、靜態(tài)的變量來實現(xiàn)一個全局變量,需要的朋友可以參考下
    2023-11-11
  • Spring MVC映射HTTP請求到Controller的處理方法

    Spring MVC映射HTTP請求到Controller的處理方法

    我們來詳細(xì)分析一下如何在 Spring MVC 中將 HTTP 請求映射到 Controller 的處理方法(Handler Methods)上,以及 @RequestMapping 注解的使用方法,需要的朋友可以參考下
    2025-05-05
  • Shiro + JWT + SpringBoot應(yīng)用示例代碼詳解

    Shiro + JWT + SpringBoot應(yīng)用示例代碼詳解

    這篇文章主要介紹了Shiro (Shiro + JWT + SpringBoot應(yīng)用),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Java如何實現(xiàn)HTTP斷點續(xù)傳功能

    Java如何實現(xiàn)HTTP斷點續(xù)傳功能

    其實斷點續(xù)傳的原理很簡單,就是在Http的請求上和一般的下載有所不同而已,本文將詳細(xì)介紹Java如何實現(xiàn)HTTP斷點續(xù)傳功能,需要的朋友可以參考下
    2012-11-11
  • Java中的Unsafe在安全領(lǐng)域的使用總結(jié)和復(fù)現(xiàn)(實例詳解)

    Java中的Unsafe在安全領(lǐng)域的使用總結(jié)和復(fù)現(xiàn)(實例詳解)

    unsafe里面有很多好用的方法,比如allocateInstance可以直接創(chuàng)建實例對象,defineAnonymousClass可以創(chuàng)建一個VM匿名類(VM?Anonymous?Class),以及直接從內(nèi)存級別修改對象的值。這篇文章主要介紹了Java中的Unsafe在安全領(lǐng)域的一些應(yīng)用總結(jié)和復(fù)現(xiàn),需要的朋友可以參考下
    2022-03-03

最新評論

徐州市| 东阿县| 雅安市| 厦门市| 锦屏县| 永修县| 贵南县| 乌兰浩特市| 阿勒泰市| 天水市| 新密市| 龙口市| 湘阴县| 甘泉县| 历史| 鄂托克前旗| 乌海市| 涡阳县| 神农架林区| 锡林郭勒盟| 茂名市| 汤阴县| 江阴市| 宣威市| 凉城县| 陵水| 刚察县| 营山县| 游戏| 拜城县| 新蔡县| 五大连池市| 观塘区| 嘉鱼县| 上栗县| 隆回县| 呼伦贝尔市| 平果县| 营口市| 卓资县| 二手房|