Mybatis-Plus源码解析之MybatisPlusAutoConfiguration(二)

news2024/10/5 10:50:00

group : com.baomidou

version:3.5.2.2-SNAPSHOT

SpringBoot是自动装配。Spring则可以在配置类上@Import(MybatisPlusAutoConfiguration.class)

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration,\
  com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration

类的头信息

首先我们查看MybatisPlusAutoConfiguration类上的注解。

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisPlusProperties.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisPlusLanguageDriverAutoConfiguration.class})
public class MybatisPlusAutoConfiguration implements InitializingBean {

@Configuration(proxyBeanMethods = false)中的proxyBeanMethods属性的作用是控制是否对配置类中的方法进行代理。

@ConditionalOnClass是Spring中的一个条件注解,用于基于类路径上是否存在某些类的情况来决定是否在加载配置。具体来说,当类路径上存在指定的类时,@ConditionalOnClass所注解的配置类或bean才会被注册到容器中。

@ConditionalOnSingleCandidate 注解会检查容器中是否有且仅有一个符合条件的 bean,如果是,则相应的配置类或 bean 才会被注册到 Spring 容器中。如果存在多个符合条件的 bean 或者一个都没有,那么相应的配置将被忽略。

@EnableConfigurationProperties 是 Spring Boot 中的注解,用于启用对配置属性类的支持。当你在应用中使用 @ConfigurationProperties 注解定义了一个用于绑定配置属性的类时,通过使用 @EnableConfigurationProperties 注解,你可以告诉 Spring Boot 启用这些配置属性类的支持。

@AutoConfigureAfter 是 Spring Boot 中的一个注解,用于指定自动配置类的加载顺序。当一个配置类需要在另一个配置类之后被加载时,可以使用 @AutoConfigureAfter 注解来明确指定顺序。

Bean的加载

SqlSessionTemplate和SqlSessionFactory将会在这个类被加载的时候也加载到容器中。

@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    // mybatisPlus自动配置类配置sql会话工厂,将sql注入器等组件存入GlobalConfig全局配置钟
    // 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()));
    }
    //设置了configuration
    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);
    }
    // 这个地方会将xml查询出来,放入到MapperLocations里面,后续循环进行解析,放入configuration
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
        factory.setMapperLocations(this.properties.resolveMapperLocations());
    }
    // TODO 修改源码支持定义 TransactionFactory
    this.getBeanThen(TransactionFactory.class, factory::setTransactionFactory);

    // TODO 对源码做了一定的修改(因为源码适配了老旧的mybatis版本,但我们不需要适配)
    Class<? extends LanguageDriver> defaultLanguageDriver = this.properties.getDefaultScriptingLanguageDriver();
    if (!ObjectUtils.isEmpty(this.languageDrivers)) {
        factory.setScriptingLanguageDrivers(this.languageDrivers);
    }
    Optional.ofNullable(defaultLanguageDriver).ifPresent(factory::setDefaultScriptingLanguageDriver);

    applySqlSessionFactoryBeanCustomizers(factory);

    // TODO 此处必为非 NULL
    GlobalConfig globalConfig = this.properties.getGlobalConfig();
    // TODO 注入填充器
    this.getBeanThen(MetaObjectHandler.class, globalConfig::setMetaObjectHandler);
    // TODO 注入主键生成器
    this.getBeansThen(IKeyGenerator.class, i -> globalConfig.getDbConfig().setKeyGenerators(i));
    // TODO 注入sql注入器
    this.getBeanThen(ISqlInjector.class, globalConfig::setSqlInjector);
    // TODO 注入ID生成器
    this.getBeanThen(IdentifierGenerator.class, globalConfig::setIdentifierGenerator);
    // TODO 设置 GlobalConfig 到 MybatisSqlSessionFactoryBean
    factory.setGlobalConfig(globalConfig);
    return factory.getObject();
}

SqlSessionFactory主要的作用就是openSession(),其实就是从连接或者数据源创建一个sqlSession。具体的实现就是MybatisSqlSessionFactoryBean。

SqlSessionTemplate的注入依赖SqlSessionFactory。

public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
      PersistenceExceptionTranslator exceptionTranslator) {

    notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
    notNull(executorType, "Property 'executorType' is required");

    this.sqlSessionFactory = sqlSessionFactory;
    this.executorType = executorType;
    this.exceptionTranslator = exceptionTranslator;
    this.sqlSessionProxy = (SqlSession) newProxyInstance(SqlSessionFactory.class.getClassLoader(),
        new Class[] { SqlSession.class }, new SqlSessionInterceptor());
  }

SqlSessionTemplate又实现了org.apache.ibatis.session.SqlSeesion,它是一个接口,有一个默认的实现DefaultSqlSession。既然已经有了一个DefaultSqlSession为什么还要有一个SqlSessionTemplate,而且SqlSessionTemplate中sqlSessionProxy就是使用代理去执行所有的SqlSession的方法。这么做的好处是什么?

SqlSessionFactorySqlSessionTemplate 是与 MyBatis 持久层框架密切相关的两个类,它们在整个数据访问的过程中扮演不同的角色。

  1. SqlSessionFactory:

    • SqlSessionFactory 是 MyBatis 的核心接口之一,负责创建 SqlSession 对象。
    • 通常,SqlSessionFactory 的实现类是 SqlSessionFactoryBean,它会被Spring容器管理。plus的实现类是MybatisSqlSessionFactoryBean
    • SqlSessionFactory 的主要作用是配置并创建 SqlSession 对象,SqlSession 用于执行SQL语句。
  2. SqlSessionTemplate:

    • SqlSessionTemplate 是 MyBatis-Spring 模块提供的一个实现了 SqlSession 接口的类。
    • 它包装了一个由 Spring 管理的 SqlSessionFactory,提供了一些便捷的方法来执行数据库操作。
    • SqlSessionTemplate 作为 Spring 提供的一个 MyBatis 的整合模板,简化了 MyBatis 的使用,无需手动处理 SqlSession 的开启、提交、回滚、关闭等操作。

为什么需要 SqlSessionTemplate

  1. 事务管理: SqlSessionTemplate 简化了事务管理。它会自动参与到 Spring 管理的事务中,无需手动调用 commitrollback 方法。

  2. 线程安全: SqlSession 不是线程安全的,而 SqlSessionTemplate 是线程安全的。这使得你可以在 Spring 容器中注入 SqlSessionTemplate,而不必担心线程安全性问题。

  3. 便捷性: SqlSessionTemplate 提供了一系列便捷的方法,例如 selectOneselectListupdateinsertdelete 等,减少了手动编写繁琐的 MyBatis 代码。

MybatisSqlSessionFactoryBean

SqlSessionFactory注入的最后一步就是执行getObject方法。

@Override
    public SqlSessionFactory getObject() throws Exception {
        if (this.sqlSessionFactory == null) {
            afterPropertiesSet();
        }

        return this.sqlSessionFactory;
    }

项目启动的时候,当前sqlSessionFactory一定是为空,所以会进入到afterPropertiesSet方法。

@Override
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");
    //TODO 清理掉资源  建议不要保留这个玩意了
    SqlRunner.DEFAULT.close();
    // 创建sql会话工厂的过程就伴随着配置的解析,也会解析XML文件和Mapper的类
    // 简单的说就是这里创建了sqlSessionfactory实例
    this.sqlSessionFactory = buildSqlSessionFactory();
}

所以核心就是buildSqlSessionFactory。

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);
    }

    // TODO 无配置启动所必须的
    this.globalConfig = Optional.ofNullable(this.globalConfig).orElseGet(GlobalConfigUtils::defaults);
    this.globalConfig.setDbConfig(Optional.ofNullable(this.globalConfig.getDbConfig()).orElseGet(GlobalConfig.DbConfig::new));

    // TODO 初始化 id-work 以及 打印骚东西
    GlobalConfigUtils.setGlobalConfig(targetConfiguration, this.globalConfig);

    Optional.ofNullable(this.objectFactory).ifPresent(targetConfiguration::setObjectFactory);
    Optional.ofNullable(this.objectWrapperFactory).ifPresent(targetConfiguration::setObjectWrapperFactory);
    Optional.ofNullable(this.vfs).ifPresent(targetConfiguration::setVfsImpl);

    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 (!isEmpty(this.typeAliases)) {
        Stream.of(this.typeAliases).forEach(typeAlias -> {
            targetConfiguration.getTypeAliasRegistry().registerAlias(typeAlias);
            LOGGER.debug(() -> "Registered type alias: '" + typeAlias + "'");
        });
    }

    if (!isEmpty(this.plugins)) {
        Stream.of(this.plugins).forEach(plugin -> {
            targetConfiguration.addInterceptor(plugin);
            LOGGER.debug(() -> "Registered plugin: '" + plugin + "'");
        });
    }

    if (hasLength(this.typeHandlersPackage)) {
        scanClasses(this.typeHandlersPackage, TypeHandler.class).stream().filter(clazz -> !clazz.isAnonymousClass())
            .filter(clazz -> !clazz.isInterface()).filter(clazz -> !Modifier.isAbstract(clazz.getModifiers()))
            .forEach(targetConfiguration.getTypeHandlerRegistry()::register);
    }

    if (!isEmpty(this.typeHandlers)) {
        Stream.of(this.typeHandlers).forEach(typeHandler -> {
            targetConfiguration.getTypeHandlerRegistry().register(typeHandler);
            LOGGER.debug(() -> "Registered type handler: '" + typeHandler + "'");
        });
    }

    targetConfiguration.setDefaultEnumTypeHandler(defaultEnumTypeHandler);

    if (!isEmpty(this.scriptingLanguageDrivers)) {
        Stream.of(this.scriptingLanguageDrivers).forEach(languageDriver -> {
            targetConfiguration.getLanguageRegistry().register(languageDriver);
            LOGGER.debug(() -> "Registered scripting language driver: '" + languageDriver + "'");
        });
    }
    Optional.ofNullable(this.defaultScriptingLanguageDriver)
        .ifPresent(targetConfiguration::setDefaultScriptingLanguage);

    if (this.databaseIdProvider != null) {// fix #64 set databaseId before parse mapper xmls
        try {
            targetConfiguration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
        } catch (SQLException e) {
            throw new NestedIOException("Failed getting a databaseId", e);
        }
    }

    Optional.ofNullable(this.cache).ifPresent(targetConfiguration::addCache);

    if (xmlConfigBuilder != null) {
        try {
            // 猜测这里就是xml钟sql的解析,其实不是
            xmlConfigBuilder.parse();
            LOGGER.debug(() -> "Parsed configuration file: '" + this.configLocation + "'");
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    }

    targetConfiguration.setEnvironment(new Environment(MybatisSqlSessionFactoryBean.class.getSimpleName(),
        this.transactionFactory == null ? new SpringManagedTransactionFactory() : this.transactionFactory,
        this.dataSource));

    if (this.mapperLocations != null) {
        if (this.mapperLocations.length == 0) {
            LOGGER.warn(() -> "Property 'mapperLocations' was specified but matching resources are not found.");
        } else {
            for (Resource mapperLocation : this.mapperLocations) {
                if (mapperLocation == null) {
                    continue;
                }
                try {
                    XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                        targetConfiguration, mapperLocation.toString(), targetConfiguration.getSqlFragments());
                    // 真正的解析在这里,mapperxml文件
                    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;
}

需要注意返回的SqlSessionFactory返回的是DefaultSqlSessionFactory。这个对我们后面很重要,因为后面使用的DefaultSqlSession就是他里面创建的。

在这里插入图片描述

并且这个方法内部xmlMapperBuilder.parse()对我们的xml进行解析,存放在sqlSessionFactory中的configuration属性中。但是如果我们的mapper interface没有xml,那他是怎么知道执行什么sql?

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1308320.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

如何想成为嵌入式工程师?(这些东西您必须知道)

嵌入式的发展怎么样&#xff1f; 嵌入式系统领域一直在迅速发展&#xff0c;伴随着物联网、智能设备、汽车电子、医疗设备等应用的不断增加&#xff0c;对嵌入式技术的需求也在不断扩大。因此&#xff0c;嵌入式领域仍然是一个充满机会的领域&#xff0c;为专业人士提供…

AC修炼计划(AtCoder Beginner Contest 332)

传送门&#xff1a;AtCoder Beginner Contest 332 - AtCoder a,b,c都还是很基础了。d题是一个bfs的纯暴力问题。 E - Lucky bag 看看范围&#xff0c;n15&#xff0c;第一个想法是dfs纯暴力&#xff0c;但所有的情况太大&#xff0c;各种决策层出不穷&#xff0c;会t。所以转…

黑马程序员Javaweb重点笔记(五)(2023版)

文章目录 前言事务管理事务进阶AOPAOP基础 前言 我个人有一个学习习惯就是把学过的内容整理出来一份重点笔记&#xff0c;笔记往往只会包括我认为比较重要的部分或者容易忘记的部分&#xff0c;以便于我快速复习&#xff0c;如果有错误欢迎大家批评指正。 另外&#xff1a;本篇…

GetGuru替代方案:4个理由告诉你为什么选择HelpLook

随着知识管理在现代企业中的重要性日益凸显&#xff0c;选择一款高效、稳定的知识库软件成为了关键。然而&#xff0c;市场上的知识库软件众多&#xff0c;如何选择适合自己的产品呢&#xff1f;本文将为你介绍GetGuru的替代方案——HelpLook&#xff0c;并从四个方面为你分析为…

大数据机器学习深度解读决策树算法:技术全解与案例实战

大数据机器学习深度解读决策树算法&#xff1a;技术全解与案例实战 本文深入探讨了机器学习中的决策树算法&#xff0c;从基础概念到高级研究进展&#xff0c;再到实战案例应用&#xff0c;全面解析了决策树的理论及其在现实世界问题中的实际效能。通过技术细节和案例实践&…

学习深度强化学习---第2部分----RL动态规划相关算法

文章目录 2.1节 动态规划简介2.2节 值函数与贝尔曼方程2.3节 策略评估2.4节 策略改进2.5节 最优值函数与最优策略2.6节 值迭代与策略迭代2.7节 动态规划求解最优策略 本部分视频所在地址&#xff1a;深度强化学习的理论与实践 2.1节 动态规划简介 态规划有两种思路&#xff1…

20章节多线程

20.1线程简介 世间有很多工作都是可以同时完成的。例如&#xff0c;人体可以同时进行呼吸、血液循环、思考问题等活用户既可以使用计算机听歌&#xff0c;也可以使用它打印文件。同样&#xff0c;计算机完全可以将多种活动同时进这种思想放在 Java 中被称为并发&#xff0c;而将…

文件系统理解

先前的博客我写了关于缓冲区的理解&#xff0c;顺便提及了在内存的文件是怎样管理的&#xff0c;本文就来描述在磁盘上的文件是怎么样。但要先了解了解磁盘。 在笔记本上机械磁盘被固态硬盘代替&#xff0c;因为固态硬盘更快&#xff0c;而且方便携带&#xff0c;机械硬盘若是受…

使用Selenium库模拟浏览器操作

Selenium是一个用于自动化Web浏览器的Python库。它提供了一组强大的工具和API&#xff0c;使开发者能够以编程方式控制浏览器的行为&#xff0c;模拟用户与网页的交互。 Selenium可以用于各种Web自动化任务&#xff0c;包括网页测试、数据采集、UI自动化等。它支持主流的Web浏览…

C#实现支付宝转账功能

环境 .net 6 AlipaySDKNet.OpenAPI 2.4.0 申请证书 登录支付宝开放平台https://open.alipay.com/ 进入控制台 授权回调地址也设置一下&#xff0c;加密方式AES 新建.net 6空白的web项目 证书除了java都需要自己生成一下pkcs1的密钥 privatekey.txt就是根据应用私钥生成…

揭开苹果3兆美元市值的秘密:创新因素

苹果公司的创新战略如何使其在竞争中脱颖而出&#xff1f;并成为当今全球用户最追捧的品牌&#xff0c;拥有各个人群中最忠诚的客户基础。苹果公司的市值超过$3万亿以及百亿美元净利&#xff0c;彰显了这家世界上最具创新力的公司的实力。从标志性的麦金塔到iPod、iPhone和iWat…

SQL小技巧3:分层汇总

前几天&#xff0c;QQ学习群有个小伙伴问我一个使用SQL分层汇总的问题。 今天正好分享下。 需求描述 在数据报表开发的工作中&#xff0c;经常会遇到需要对数据进行分组汇总的情况。 假设有一个销售数据表sales&#xff0c;包含列region&#xff08;地区&#xff09;、mont…

与「高通」最像的芯片公司出道,杀入主流智驾芯片市场

作者 |德新 编辑 |王博 单芯片全时行泊一体 上个月&#xff0c;AI芯片研发及基础算力平台公司爱芯元智推出面向车载智能驾驶领域的品牌「爱芯元速」。这意味着这家创办四年多&#xff0c;累计融资近20亿元的芯片公司正式进军车载市场。 实际上&#xff0c;爱芯元速序列的第一…

算法导论复习(二)

算法导论第二次复习以 分治法 为专题 文章目录 分治算法是什么归并排序Strassen矩阵乘法最近点对 求解递推表达式 分治算法是什么 归并排序 代码如下&#xff1a; #include <iostream> #include <vector>using namespace std;// 归并函数&#xff0c;将两个有序数…

k8s debug 浅谈

一 k8s debug 浅谈 说明&#xff1a; 本文只是基于对kubectl debug浅显认识总结的知识点,后续实际使用再补充案例 Kubernetes 官方出品调试工具上手指南(无需安装&#xff0c;开箱即用) debug-application 简化 Pod 故障诊断: kubectl-debug 介绍 1.18 版本之前需要自己…

【docker】Hello World

搜索hello-world镜像 docker search hello-world拉去镜像 docker pull hello-world查看本地镜像 docker images 运行镜像 docker run hello-world查看所有的容器 docker ps -a查询start状态容器 docker ps 输出介绍 CONTAINER ID: 容器 ID。IMAGE: 使用的镜像。COMMAN…

No module named ‘osgeo’解决办法

from osgeo import gdal 报错&#xff1a;No module named ‘osgeo’ pycharm安装osgeo、GDAL都失败 pip install osgeo失败 最后先下载对应版本的GDAL文件 再cmd命令行中用对应环境的python进行GDAL包安装 1.我将我的Anaconda某个环境文件夹D:\software\pinstall\Anaconda3…

window系统使用ESP8266开发板(CP2102)

连接开发板到电脑 虚拟机中选择连接的开发板硬件 查看设备管理器 更新驱动: CP210x USB to UART Bridge VCP Drivers - Silicon Labs 驱动安装成功

每日一练【将 x 减到 0 的最小操作数】

一、题目描述 给你一个整数数组 nums 和一个整数 x 。每一次操作时&#xff0c;你应当移除数组 nums 最左边或最右边的元素&#xff0c;然后从 x 中减去该元素的值。请注意&#xff0c;需要 修改 数组以供接下来的操作使用。 如果可以将 x 恰好 减到 0 &#xff0c;返回 最小…

为什么QLC NAND才是ZNS SSD最大的赢家?-part3

在ZNS SSD设计中&#xff0c;也有很多的挑战&#xff1a; Open Zones 对写入缓冲区的需求&#xff1a;保持大量的 open zones&#xff08;例如 1K&#xff09;会增加对带宽的需求&#xff0c;并要求控制器提供足够的缓冲空间来管理并发写入请求。这需要较大的高带宽写入缓冲区以…