欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis?sql與xml文件讀取方法詳細(xì)分析

 更新時(shí)間:2023年01月28日 10:50:45   作者:xl649138628  
這篇文章主要介紹了Mybatis?sql與xml文件讀取方法,在執(zhí)行一個(gè)自定義sql語句時(shí),dao對應(yīng)的代理對象時(shí)如何找到sql,也就是dao的代理對象和sql之間的關(guān)聯(lián)關(guān)系是如何建立的

在執(zhí)行一個(gè)自定義sql語句時(shí),dao對應(yīng)的代理對象時(shí)如何找到sql,也就是dao的代理對象和sql之間的關(guān)聯(lián)關(guān)系是如何建立的。

        在mybatis中的MybatisPlusAutoConfiguration類被@Configuration注解,在該類中通過被@Bean注解的sqlSessionFactory方法向spring上下文注入bean并生成SqlSessionFactory類型的bean實(shí)例。關(guān)注該方法的最后一行代碼。

    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        // TODO 使用 MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean
        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setVfs(SpringBootVFS.class);
        if (StringUtils.hasText(this.properties.getConfigLocation())) {
            factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
        }
        applyConfiguration(factory);
        if (this.properties.getConfigurationProperties() != null) {
            factory.setConfigurationProperties(this.properties.getConfigurationProperties());
        }
        if (!ObjectUtils.isEmpty(this.interceptors)) {
            factory.setPlugins(this.interceptors);
        }
        if (this.databaseIdProvider != null) {
            factory.setDatabaseIdProvider(this.databaseIdProvider);
        }
        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
            factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        }
        if (this.properties.getTypeAliasesSuperType() != null) {
            factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
        }
        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }
        if (!ObjectUtils.isEmpty(this.typeHandlers)) {
            factory.setTypeHandlers(this.typeHandlers);
        }
        Resource[] mapperLocations = this.properties.resolveMapperLocations();
        if (!ObjectUtils.isEmpty(mapperLocations)) {
            factory.setMapperLocations(mapperLocations);
        }
        // TODO 修改源碼支持定義 TransactionFactory
        this.getBeanThen(TransactionFactory.class, factory::setTransactionFactory);
        // TODO 對源碼做了一定的修改(因?yàn)樵创a適配了老舊的mybatis版本,但我們不需要適配)
        Class<? extends LanguageDriver> defaultLanguageDriver = this.properties.getDefaultScriptingLanguageDriver();
        if (!ObjectUtils.isEmpty(this.languageDrivers)) {
            factory.setScriptingLanguageDrivers(this.languageDrivers);
        }
        Optional.ofNullable(defaultLanguageDriver).ifPresent(factory::setDefaultScriptingLanguageDriver);
        // TODO 自定義枚舉包
        if (StringUtils.hasLength(this.properties.getTypeEnumsPackage())) {
            factory.setTypeEnumsPackage(this.properties.getTypeEnumsPackage());
        }
        // TODO 此處必為非 NULL
        GlobalConfig globalConfig = this.properties.getGlobalConfig();
        // TODO 注入填充器
        this.getBeanThen(MetaObjectHandler.class, globalConfig::setMetaObjectHandler);
        // TODO 注入主鍵生成器
        this.getBeanThen(IKeyGenerator.class, i -> globalConfig.getDbConfig().setKeyGenerator(i));
        // TODO 注入sql注入器
        this.getBeanThen(ISqlInjector.class, globalConfig::setSqlInjector);
        // TODO 注入ID生成器
        this.getBeanThen(IdentifierGenerator.class, globalConfig::setIdentifierGenerator);
        // TODO 設(shè)置 GlobalConfig 到 MybatisSqlSessionFactoryBean
        factory.setGlobalConfig(globalConfig);
        //關(guān)注該行代碼
        return factory.getObject();
    }

        進(jìn)入最后一行代碼找到MybatisSqlSessionFactoryBean類里的getObject方法,然后進(jìn)入到該類的afterPropertiesSet方法,找到了buildSqlSessionFactory方法。

public SqlSessionFactory getObject() throws Exception {
        if (this.sqlSessionFactory == null) {
            //關(guān)注該行方法
            afterPropertiesSet();
        }
        return this.sqlSessionFactory;
}
public void afterPropertiesSet() throws Exception {
        notNull(dataSource, "Property 'dataSource' is required");
        state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
            "Property 'configuration' and 'configLocation' can not specified with together");
        //關(guān)注該行方法
        this.sqlSessionFactory = buildSqlSessionFactory();
 }

 在buildSqlSessionFactory中有兩處代碼比較關(guān)鍵,第一處如下圖hasLength(this.typeAliasesPackage)判斷了在yml中配置的的mybatis-plus.typeAliasesPackage,并通過buildSqlSessionFactory方法里的scanClasses方法將讀取到的類路徑全部小寫后存放到TypeAliasRegistry類的typeAliases屬性的hashMap緩存中。

mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml
  typeAliasesPackage: >
    com.changshin.entity.po
  global-config:
    id-type: 0  # 0:數(shù)據(jù)庫ID自增   1:用戶輸入id  2:全局唯一id(IdWorker)  3:全局唯一ID(uuid)
    db-column-underline: false
    refresh-mapper: true
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: true #配置的緩存的全局開關(guān)
    lazyLoadingEnabled: true #延時(shí)加載的開關(guān)
    multipleResultSetsEnabled: true #開啟的話,延時(shí)加載一個(gè)屬性時(shí)會加載該對象全部屬性,否則按需加載屬性
 protected SqlSessionFactory buildSqlSessionFactory() throws Exception {
        final Configuration targetConfiguration;
        // TODO 使用 MybatisXmlConfigBuilder 而不是 XMLConfigBuilder
        MybatisXMLConfigBuilder xmlConfigBuilder = null;
        if (this.configuration != null) {
            targetConfiguration = this.configuration;
            if (targetConfiguration.getVariables() == null) {
                targetConfiguration.setVariables(this.configurationProperties);
            } else if (this.configurationProperties != null) {
                targetConfiguration.getVariables().putAll(this.configurationProperties);
            }
        } else if (this.configLocation != null) {
            // TODO 使用 MybatisXMLConfigBuilder
            xmlConfigBuilder = new MybatisXMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
            targetConfiguration = xmlConfigBuilder.getConfiguration();
        } else {
            LOGGER.debug(() -> "Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
            // TODO 使用 MybatisConfiguration
            targetConfiguration = new MybatisConfiguration();
            Optional.ofNullable(this.configurationProperties).ifPresent(targetConfiguration::setVariables);
        }
        //省略。。。。。。。
 
        if (hasLength(this.typeAliasesPackage)) {
            scanClasses(this.typeAliasesPackage, this.typeAliasesSuperType).stream()
                .filter(clazz -> !clazz.isAnonymousClass()).filter(clazz -> !clazz.isInterface())
                .filter(clazz -> !clazz.isMemberClass()).forEach(targetConfiguration.getTypeAliasRegistry()::registerAlias);
        }
        //省略。。。。。。。。。。。。。。。。。。。。。。。。
        if (this.mapperLocations != null) {
            if (this.mapperLocations.length == 0) {
                LOGGER.warn(() -> "Property 'mapperLocations' was specified but matching resources are not found.");
            } else {
                // 關(guān)注for循環(huán)結(jié)構(gòu)體里的代碼
                for (Resource mapperLocation : this.mapperLocations) {
                    if (mapperLocation == null) {
                        continue;
                    }
                    try {
                        XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                            targetConfiguration, mapperLocation.toString(), targetConfiguration.getSqlFragments());
                        xmlMapperBuilder.parse();
                    } catch (Exception e) {
                        throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
                    } finally {
                        ErrorContext.instance().reset();
                    }
                    LOGGER.debug(() -> "Parsed mapper file: '" + mapperLocation + "'");
                }
            }
        } else {
            LOGGER.debug(() -> "Property 'mapperLocations' was not specified.");
        }
        final SqlSessionFactory sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(targetConfiguration);
        // TODO SqlRunner
        SqlHelper.FACTORY = sqlSessionFactory;
        // TODO 打印騷東西 Banner
        if (globalConfig.isBanner()) {
            System.out.println(" _ _   |_  _ _|_. ___ _ |    _ ");
            System.out.println("| | |\\/|_)(_| | |_\\  |_)||_|_\\ ");
            System.out.println("     /               |         ");
            System.out.println("                        " + MybatisPlusVersion.getVersion() + " ");
        }
        return sqlSessionFactory;
    }

第二處xmlMapperBuilder.parse()方法解析了xml文件,mapperLocations屬性是一個(gè)數(shù)組類型的屬性,數(shù)組里存儲了xml文件的全路徑。通

過for循環(huán)對每一個(gè)xml進(jìn)行解析。進(jìn)入parse方法。在進(jìn)入parse方法前初始化了XMLMapperBuilder并將其configuration屬性設(shè)置為MybatisSqlSessionFactoryBean的configuration屬性屬性。

XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                            targetConfiguration, mapperLocation.toString(), targetConfiguration.getSqlFragments());

對parse方法進(jìn)行解析 

public void parse() {
    //判斷是否加載過配置文件
    if (!configuration.isResourceLoaded(resource)) {
      //解析mapper標(biāo)簽
      configurationElement(parser.evalNode("/mapper"));
      //標(biāo)注該配置已經(jīng)被加載過了
      configuration.addLoadedResource(resource);
      //將dao對應(yīng)的類與xml mapper標(biāo)簽的namespaces屬性做綁定
      bindMapperForNamespace();
    }
    parsePendingResultMaps();
    parsePendingCacheRefs();
    parsePendingStatements();
  }

先分析 parse方法里的configurationElement方法。該方法逐步解析mapper標(biāo)簽下的子標(biāo)簽,具體解析過程比較復(fù)雜在此就不分析了。

 private void configurationElement(XNode context) {
    try {
      String namespace = context.getStringAttribute("namespace");
      if (namespace == null || namespace.isEmpty()) {
        throw new BuilderException("Mapper's namespace cannot be empty");
      }
      builderAssistant.setCurrentNamespace(namespace);
      cacheRefElement(context.evalNode("cache-ref"));
      cacheElement(context.evalNode("cache"));
      parameterMapElement(context.evalNodes("/mapper/parameterMap"));
      resultMapElements(context.evalNodes("/mapper/resultMap"));
      sqlElement(context.evalNodes("/mapper/sql"));
      buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e);
    }
  }

bindMapperForNamespace方法將namespace將生成的的類類型通過

configuration.addMapper(boundType)方法放到Configuration類的MapperRegistry類型屬性mapperRegistry的knownMappers屬性的緩存中(該屬性是一個(gè)以類類型key,MapperProxyFactory為value的HashMap)。

private void bindMapperForNamespace() {
    String namespace = builderAssistant.getCurrentNamespace();
    if (namespace != null) {
      Class<?> boundType = null;
      try {
        boundType = Resources.classForName(namespace);
      } catch (ClassNotFoundException e) {
        // ignore, bound type is not required
      }
      if (boundType != null && !configuration.hasMapper(boundType)) {
        // Spring may not know the real resource name so we set a flag
        // to prevent loading again this resource from the mapper interface
        // look at MapperAnnotationBuilder#loadXmlResource
        configuration.addLoadedResource("namespace:" + namespace);
        configuration.addMapper(boundType);
      }
    }
  }

到此這篇關(guān)于Mybatis sql與xml文件讀取方法詳細(xì)分析的文章就介紹到這了,更多相關(guān)Mybatis sql與xml文件讀取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java如何通過"枚舉的枚舉"表示二級分類的業(yè)務(wù)場景

    Java如何通過"枚舉的枚舉"表示二級分類的業(yè)務(wù)場景

    這篇文章主要介紹了Java如何通過"枚舉的枚舉"表示二級分類的業(yè)務(wù)場景問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Elasticsearch寫入瓶頸導(dǎo)致skywalking大盤空白

    Elasticsearch寫入瓶頸導(dǎo)致skywalking大盤空白

    這篇文章主要為大家介紹了Elasticsearch寫入瓶頸導(dǎo)致skywalking大盤空白的解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • gson對象序列化的示例

    gson對象序列化的示例

    本文介紹如何將Java對象序列化為Json文件,然后讀取該Json文件讀取回Java對象。在下面的示例中,我們創(chuàng)建了一個(gè)Student類。然后生成一個(gè)student.json文件,該文件將具有Student對象的json數(shù)據(jù)。
    2020-11-11
  • SpringBoot項(xiàng)目中jar發(fā)布獲取jar包所在目錄路徑的最佳方法

    SpringBoot項(xiàng)目中jar發(fā)布獲取jar包所在目錄路徑的最佳方法

    在開發(fā)過程中,我們經(jīng)常要遇到上傳圖片、word、pdf等功能,但是當(dāng)我們把項(xiàng)目打包發(fā)布到服務(wù)器上時(shí),對應(yīng)的很多存儲路徑的方法就會失效,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目中jar發(fā)布獲取jar包所在目錄路徑的相關(guān)資料
    2022-07-07
  • IDEA配置碼云Gitee的使用詳解

    IDEA配置碼云Gitee的使用詳解

    這篇文章主要介紹了IDEA配置碼云Gitee的使用,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • java線程中synchronized和Lock區(qū)別及介紹

    java線程中synchronized和Lock區(qū)別及介紹

    這篇文章主要為大家介紹了java線程中synchronized和Lock區(qū)別及介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Java StackTraceElement實(shí)例代碼

    Java StackTraceElement實(shí)例代碼

    這篇文章主要介紹了Java StackTraceElement實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • Java語言實(shí)現(xiàn)基數(shù)排序代碼分享

    Java語言實(shí)現(xiàn)基數(shù)排序代碼分享

    這篇文章主要介紹了Java語言實(shí)現(xiàn)基數(shù)排序代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • 集合嵌套之ArrayList嵌套ArrayList實(shí)例

    集合嵌套之ArrayList嵌套ArrayList實(shí)例

    下面小編就為大家?guī)硪黄锨短字瓵rrayList嵌套ArrayList實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • StreamAPI多次消費(fèi)一個(gè)stream代碼實(shí)例

    StreamAPI多次消費(fèi)一個(gè)stream代碼實(shí)例

    這篇文章主要介紹了StreamAPI多次消費(fèi)一個(gè)stream代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04

最新評論