当前位置: 首页 > news >正文

【Mybatis系列】Mybatis常见的分页方法以及源码理解

Mybatis-Plus的selectPage

  1. 引入依赖
        <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency>
  1. 添加分页插件
@Configuration
public class MybatisConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));return interceptor;}
}
  1. Mapper类和实体类

Mapper类

public interface UserMapper extends BaseMapper<UserInfo> {}

实体类

@Data
@EqualsAndHashCode(callSuper = false)
@TableName(value = "cls_user", autoResultMap = true)
@NoArgsConstructor
public class UserInfo {private static final long serialVersionUID = 1L;@TableId(value = "id", type = IdType.AUTO)private Integer id;@TableField("user_name")private String userName;@TableField("password")private String password;
}
  1. 使用方法
    @Overridepublic Page<UserInfo> pageQuery(String name) {return userMapper.selectPage(new Page<>(1,10), new QueryWrapper<>());}
  1. 查询示例结果
{"records": [{"id": 1,"userName": "cc","password": "12345"},{"id": 2,"userName": "cc","password": "12345"},{"id": 3,"userName": "cc","password": "12345"}],"total": 11,"size": 3,"current": 1,"orders": [],"optimizeCountSql": true,"searchCount": true,"countId": null,"maxLimit": null,"pages": 4
}

源码解析

MybatisPlusInterceptor#intercept进行查询拦截,遍历其中的InnerInterceptor,核心方法是InnerInterceptor#willDoQueryInnerInterceptor#beforeQuery

    public Object intercept(Invocation invocation) throws Throwable {Object target = invocation.getTarget();Object[] args = invocation.getArgs();if (target instanceof Executor) {Executor executor = (Executor)target;Object parameter = args[1];boolean isUpdate = args.length == 2;MappedStatement ms = (MappedStatement)args[0];if (!isUpdate && ms.getSqlCommandType() == SqlCommandType.SELECT) {RowBounds rowBounds = (RowBounds)args[2];ResultHandler resultHandler = (ResultHandler)args[3];BoundSql boundSql;if (args.length == 4) {boundSql = ms.getBoundSql(parameter);} else {boundSql = (BoundSql)args[5];}Iterator var11 = this.interceptors.iterator();while(var11.hasNext()) {InnerInterceptor query = (InnerInterceptor)var11.next();if (!query.willDoQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql)) {return Collections.emptyList();}query.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);}CacheKey cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);return executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);}if (isUpdate) {Iterator var8 = this.interceptors.iterator();while(var8.hasNext()) {InnerInterceptor update = (InnerInterceptor)var8.next();if (!update.willDoUpdate(executor, ms, parameter)) {return -1;}update.beforeUpdate(executor, ms, parameter);}}} else {StatementHandler sh = (StatementHandler)target;if (null == args) {Iterator var14 = this.interceptors.iterator();while(var14.hasNext()) {InnerInterceptor innerInterceptor = (InnerInterceptor)var14.next();innerInterceptor.beforeGetBoundSql(sh);}} else {Connection connections = (Connection)args[0];Integer transactionTimeout = (Integer)args[1];Iterator var18 = this.interceptors.iterator();while(var18.hasNext()) {InnerInterceptor innerInterceptor = (InnerInterceptor)var18.next();innerInterceptor.beforePrepare(sh, connections, transactionTimeout);}}}return invocation.proceed();}

PaginationInnerInterceptor#willDoQuery,核心逻辑就是根据参数中的Page对象构造查询总数的sql,进行sql查询,存放到Page对象中。

    public boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {IPage<?> page = (IPage)ParameterUtils.findPage(parameter).orElse((Object)null);if (page != null && page.getSize() >= 0L && page.searchCount()) {MappedStatement countMs = this.buildCountMappedStatement(ms, page.countId());BoundSql countSql;if (countMs != null) {countSql = countMs.getBoundSql(parameter);} else {countMs = this.buildAutoCountMappedStatement(ms);String countSqlStr = this.autoCountSql(page, boundSql.getSql());MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);countSql = new BoundSql(countMs.getConfiguration(), countSqlStr, mpBoundSql.parameterMappings(), parameter);PluginUtils.setAdditionalParameter(countSql, mpBoundSql.additionalParameters());}CacheKey cacheKey = executor.createCacheKey(countMs, parameter, rowBounds, countSql);List<Object> result = executor.query(countMs, parameter, rowBounds, resultHandler, cacheKey, countSql);long total = 0L;if (CollectionUtils.isNotEmpty(result)) {Object o = result.get(0);if (o != null) {total = Long.parseLong(o.toString());}}page.setTotal(total);return this.continuePage(page);} else {return true;}}

PaginationInnerInterceptor#beforeQuery,对原有的查询语句进行封装改造,增加limit ?,?

    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {IPage<?> page = (IPage)ParameterUtils.findPage(parameter).orElse((Object)null);if (null != page) {boolean addOrdered = false;String buildSql = boundSql.getSql();List<OrderItem> orders = page.orders();if (CollectionUtils.isNotEmpty(orders)) {addOrdered = true;buildSql = this.concatOrderBy(buildSql, orders);}Long _limit = page.maxLimit() != null ? page.maxLimit() : this.maxLimit;if (page.getSize() < 0L && null == _limit) {if (addOrdered) {PluginUtils.mpBoundSql(boundSql).sql(buildSql);}} else {this.handlerLimit(page, _limit);IDialect dialect = this.findIDialect(executor);Configuration configuration = ms.getConfiguration();DialectModel model = dialect.buildPaginationSql(buildSql, page.offset(), page.getSize());MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);List<ParameterMapping> mappings = mpBoundSql.parameterMappings();Map<String, Object> additionalParameter = mpBoundSql.additionalParameters();model.consumers(mappings, configuration, additionalParameter);mpBoundSql.sql(model.getDialectSql());mpBoundSql.parameterMappings(mappings);}}}

PageHelper.startPage(int pageNum, int pageSize);

  1. 引入依赖
        <dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><!-- 特别注意版本问题 --><version>1.4.5</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.2</version></dependency>
  1. Mapper类和实体类

Mapper类

public interface UserMapper extends BaseMapper<UserInfo> {}

实体类

@Data
@EqualsAndHashCode(callSuper = false)
@TableName(value = "cls_user", autoResultMap = true)
@NoArgsConstructor
public class UserInfo {private static final long serialVersionUID = 1L;@TableId(value = "id", type = IdType.AUTO)private Integer id;@TableField("user_name")private String userName;@TableField("password")private String password;
}
  1. 使用方法
    public PageInfo search() {PageHelper.startPage(1, 3);QueryWrapper<UserInfo> wrapper = new QueryWrapper();List<UserInfo> testEntityList = userMapper.selectList(wrapper);return new PageInfo<UserInfo>(testEntityList);}
  1. 运行结果
{"total": 11,"list": [{"id": 1,"userName": "cc","password": "12345"},{"id": 2,"userName": "cc","password": "12345"},{"id": 3,"userName": "cc","password": "12345"}],"pageNum": 1,"pageSize": 3,"size": 3,"startRow": 1,"endRow": 3,"pages": 4,"prePage": 0,"nextPage": 2,"isFirstPage": true,"isLastPage": false,"hasPreviousPage": false,"hasNextPage": true,"navigatePages": 8,"navigatepageNums": [1,2,3,4],"navigateFirstPage": 1,"navigateLastPage": 4
}

源码解析

  1. 引入pagehelper-spring-boot-starter,Springboot会扫描jar包中的spring.factories,自动装配机制可以看我这篇文章,原理讲的透透的,SpringBoot自动装配的实现原理。在pagehelper-spring-boot-autoconfigure的jar中有spring.factories,会引入PageHelperAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration
  1. PageHelperAutoConfiguration该类实现了InitializingBean,在启动的时候创建该对象之后会执行afterPropertiesSet,Spring的Bean的生命周期,详细了解可以看这篇文章。该类主要是添加了PageInterceptor进行拦截。
public class PageHelperAutoConfiguration implements InitializingBean {@Overridepublic void afterPropertiesSet() throws Exception {PageInterceptor interceptor = new PageInterceptor();interceptor.setProperties(this.properties);for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();if (!containsInterceptor(configuration, interceptor)) {configuration.addInterceptor(interceptor);}}}
}
  1. PageInterceptor#intercept,该方法是拦截查询语句,分别组装查询总数的sql和分页查询的sql。
    public Object intercept(Invocation invocation) throws Throwable {try {Object[] args = invocation.getArgs();MappedStatement ms = (MappedStatement) args[0];Object parameter = args[1];RowBounds rowBounds = (RowBounds) args[2];ResultHandler resultHandler = (ResultHandler) args[3];Executor executor = (Executor) invocation.getTarget();CacheKey cacheKey;BoundSql boundSql;//由于逻辑关系,只会进入一次if (args.length == 4) {//4 个参数时boundSql = ms.getBoundSql(parameter);cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);} else {//6 个参数时cacheKey = (CacheKey) args[4];boundSql = (BoundSql) args[5];}checkDialectExists();//对 boundSql 的拦截处理if (dialect instanceof BoundSqlInterceptor.Chain) {boundSql = ((BoundSqlInterceptor.Chain) dialect).doBoundSql(BoundSqlInterceptor.Type.ORIGINAL, boundSql, cacheKey);}List resultList;//调用方法判断是否需要进行分页,如果不需要,直接返回结果if (!dialect.skip(ms, parameter, rowBounds)) {//开启debug时,输出触发当前分页执行时的PageHelper调用堆栈// 如果和当前调用堆栈不一致,说明在启用分页后没有消费,当前线程再次执行时消费,调用堆栈显示的方法使用不安全debugStackTraceLog();//判断是否需要进行 count 查询if (dialect.beforeCount(ms, parameter, rowBounds)) {//查询总数Long count = count(executor, ms, parameter, rowBounds, null, boundSql);//处理查询总数,返回 true 时继续分页查询,false 时直接返回if (!dialect.afterCount(count, parameter, rowBounds)) {//当查询总数为 0 时,直接返回空的结果return dialect.afterPage(new ArrayList(), parameter, rowBounds);}}resultList = ExecutorUtil.pageQuery(dialect, executor,ms, parameter, rowBounds, resultHandler, boundSql, cacheKey);} else {//rowBounds用参数值,不使用分页插件处理时,仍然支持默认的内存分页resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);}return dialect.afterPage(resultList, parameter, rowBounds);} finally {if (dialect != null) {dialect.afterAll();}}}
  1. PageHelper.startPage,核心就是组装Page对象存放到线程变量中。那么会导致线程污染吗?拦截器中有remove的操作吗?看到线程变量,最关心的点应该就是使用ThreadLocal可能会导致内存泄露,线程变量ThreadLocal详解,该篇对ThreadLocal的相关知识点讲的明明白白。
    public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero) {Page<E> page = new Page<E>(pageNum, pageSize, count);page.setReasonable(reasonable);page.setPageSizeZero(pageSizeZero);//当已经执行过orderBy的时候Page<E> oldPage = getLocalPage();if (oldPage != null && oldPage.isOrderByOnly()) {page.setOrderBy(oldPage.getOrderBy());}setLocalPage(page);return page;}
  1. PageHelper#skip,PageHelper判断是否需要分页查询。如果Page对象是null,则选择跳过分页查询的逻辑
    public boolean skip(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {Page page = pageParams.getPage(parameterObject, rowBounds);if (page == null) {return true;} else {//设置默认的 count 列if (StringUtil.isEmpty(page.getCountColumn())) {page.setCountColumn(pageParams.getCountColumn());}autoDialect.initDelegateDialect(ms, page.getDialectClass());return false;}}

PageParams#getPage,会从线程变量中获取Page对象,进行组装

    public Page getPage(Object parameterObject, RowBounds rowBounds) {Page page = PageHelper.getLocalPage();if (page == null) {if (rowBounds != RowBounds.DEFAULT) {if (offsetAsPageNum) {page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount);} else {page = new Page(new int[]{rowBounds.getOffset(), rowBounds.getLimit()}, rowBoundsWithCount);//offsetAsPageNum=false的时候,由于PageNum问题,不能使用reasonable,这里会强制为falsepage.setReasonable(false);}if (rowBounds instanceof PageRowBounds) {PageRowBounds pageRowBounds = (PageRowBounds) rowBounds;page.setCount(pageRowBounds.getCount() == null || pageRowBounds.getCount());}} else if (parameterObject instanceof IPage || supportMethodsArguments) {try {page = PageObjectUtil.getPageFromObject(parameterObject, false);} catch (Exception e) {return null;}}if (page == null) {return null;}PageHelper.setLocalPage(page);}//分页合理化if (page.getReasonable() == null) {page.setReasonable(reasonable);}//当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果if (page.getPageSizeZero() == null) {page.setPageSizeZero(pageSizeZero);}if (page.getKeepOrderBy() == null) {page.setKeepOrderBy(keepOrderBy);}if (page.getKeepSubSelectOrderBy() == null) {page.setKeepSubSelectOrderBy(keepSubSelectOrderBy);}return page;}
  1. PageHelper#afterAll,清除线程变量。所以PageInterceptor已经帮我们把线程变量给清除了,不会产生线程污染。
    @Overridepublic void afterAll() {//这个方法即使不分页也会被执行,所以要判断 nullAbstractHelperDialect delegate = autoDialect.getDelegate();if (delegate != null) {delegate.afterAll();autoDialect.clearDelegate();}clearPage();}

Mybatisplus的PageHelper

  1. 引入依赖
        <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>2.1.9</version></dependency>
  1. 引入Mybatis配置
@Configuration
public class MybatisPlusConfig {@Beanpublic PaginationInterceptor paginationInterceptor() {PaginationInterceptor paginationInterceptor = new PaginationInterceptor();}
  1. 分页查询,PageHelper是com.baomidou.mybatisplus.plugins.pagination.PageHelper
	@RequestMapping("/list")public Pagination search() {Wrapper wrapper = new EntityWrapper();PageHelper.startPage(1, 10);List<TestEntity> testEntityList = testMapper.selectList(wrapper);Pagination pagination = PageHelper.getPagination();PageHelper.remove();return pagination;}

源码解析

  1. PaginationInterceptor#intercept,核心也是从PageHelper中获取线程变量,对象封装存储在线程变量中。
    public Object intercept(Invocation invocation) throws Throwable {StatementHandler statementHandler = (StatementHandler)PluginUtils.realTarget(invocation.getTarget());MetaObject metaObject = SystemMetaObject.forObject(statementHandler);this.sqlParser(metaObject);MappedStatement mappedStatement = (MappedStatement)metaObject.getValue("delegate.mappedStatement");if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {return invocation.proceed();} else {RowBounds rowBounds = (RowBounds)metaObject.getValue("delegate.rowBounds");if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {if (!this.localPage) {return invocation.proceed();}rowBounds = PageHelper.getPagination();if (rowBounds == null) {return invocation.proceed();}}BoundSql boundSql = (BoundSql)metaObject.getValue("delegate.boundSql");String originalSql = boundSql.getSql();Connection connection = (Connection)invocation.getArgs()[0];DBType dbType = StringUtils.isNotEmpty(this.dialectType) ? DBType.getDBType(this.dialectType) : JdbcUtils.getDbType(connection.getMetaData().getURL());if (rowBounds instanceof Pagination) {Pagination page = (Pagination)rowBounds;boolean orderBy = true;if (page.isSearchCount()) {SqlInfo sqlInfo = SqlUtils.getOptimizeCountSql(page.isOptimizeCountSql(), this.sqlParser, originalSql);orderBy = sqlInfo.isOrderBy();this.queryTotal(this.overflowCurrent, sqlInfo.getSql(), mappedStatement, boundSql, page, connection);if (page.getTotal() <= 0) {return invocation.proceed();}}String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);originalSql = DialectFactory.buildPaginationSql(page, buildSql, dbType, this.dialectClazz);} else {originalSql = DialectFactory.buildPaginationSql((RowBounds)rowBounds, originalSql, dbType, this.dialectClazz);}metaObject.setValue("delegate.boundSql.sql", originalSql);metaObject.setValue("delegate.rowBounds.offset", 0);metaObject.setValue("delegate.rowBounds.limit", 2147483647);return invocation.proceed();}}

总结一下

  1. Mybatis-Plus的selectPage是我最喜欢用的,不需要引入额外的jar包;pagehelper-spring-boot-starter中的PageHelper.startPage,封装形式也很简单。

  2. Mybatisplus的PageHelper真的是大坑,在项目中我以为是pagehelper-spring-boot-starter类的PageHelper,结果查询结果中pages返回的数据一直是0,跟踪源码发现PageHelper获取的Page对象为null,不进行分页查询。跟踪设置环境变量的方法,发现原来是调用的MybatisPlus老版本的PageHelper,关键的是Mybatis-Plus使用PageHelper和pagehelper-spring-boot-starter使用PageHelper格式会如此的相似。这意味着不看包名,很容易搞混。这种出现一样的类名很容易出现你以为用的是A包,实际上用的是B包的情况。
    在这里插入图片描述

相关文章:

【Mybatis系列】Mybatis常见的分页方法以及源码理解

Mybatis-Plus的selectPage 引入依赖 <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency>添加分页插件 Configuration public class My…...

Java面向对象:多态特性的学习

本文介绍了Java面向对象多态特性, 多态的介绍. 多态的实现条件–1.发生继承.2.发生重写(重写与重载的区别)3.向上转型与向下转型.4.静态绑定和动态绑定5. 实现多态 举例总结多态的优缺点 避免在构造方法内调用被重写的方法… Java面向对象:多态特性的学习一.什么是多态?二.多态…...

id函数 / 可变类型变量 / 不可变类型变量 / +=操作

前言 再说正文之前&#xff0c;需要大家先了解一下对象&#xff0c;指针和引用的含义&#xff0c;不懂得同学可以参考我上一篇博客“(12条消息) 引用是否有地址的讨论的_xx_xjm的博客-CSDN博客” 正文 一&#xff1a;python中一切皆对象 “python中一切皆对象”这句话我相信…...

aws apigateway 使用apigateway集成lambda

参考资料 代理集成&#xff0c;https://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html非代理集成&#xff0c;https://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/getting-started-…...

Linux SPI 驱动实验

目录 一、Linux 下 SPI 驱动框架简介 1、SPI 主机驱动 2、SPI 设备驱动 SPI 设备数据收发处理流程 3、SPI 设备和驱动匹配过程 二、添加SPI 设备信息 1、添加 ICM20608 所使用的 IO 2、 在 ecspi3 节点追加 icm20608 子节点 三、编写 ICM20608 驱动 1、修改makefile​…...

[1.4]计算机系统概述——操作系统的体系结构

第一章 计算机系统概述 操作系统的体系结构 大内核/单内核/宏内核微内核 通过之前的学习&#xff0c;我们知道计算机系统的层次结构是这样的。 但是操作系统的内部其实还可以再进一步地划分。 一部分是内核的功能&#xff0c;一部分是非内核的功能。 操作系统最核心的功能&…...

FPGA的GigE Vision IP相机图像采集方案设计,转换为千兆UDP,支持10G MAC

1 概述 GigE Vision是一个比较复杂的协议&#xff0c;要在FPGA中完全实现具有较大的难度。如果FPGA作为接收端希望实现GigE Vision相机的配置和图像采集功能&#xff0c;则只需要实现其中小部分功能即可。本文对原有GigE Vision协议的结构进行了裁剪&#xff0c;仅保留设备搜索…...

大数据相关面试题

linux 常见linux高级命令&#xff1f; top、iotopnetstatdf -hjmap -heaptarrpmps -efshell 用过的shell工具&#xff1f; awk Awk 命令详解 - 简书 awk是行处理器: 相比较屏幕处理的优点&#xff0c;在处理庞大文件时不会出现内存溢出或是处理缓慢的问题&#xff0c;通常用来…...

AI绘画第二步,抄作业复现超赞的效果!

上一篇&#xff0c;讲了如何安装AI绘画软件&#xff0c;但是装完后发现生成效果很渣&#xff01;而网上那些效果都很赞。真的是理想很丰满&#xff0c;现实很骨感。今天就是来聊聊如何抄作业&#xff0c;最大程度的还原那些超赞的效果。换一种说法就是&#xff0c;教大家如何使…...

Python的并发编程

我们将一个正在运行的程序称为进程。每个进程都有它自己的系统状态&#xff0c;包含内存状态、打开文件列表、追踪指令执行情况的程序指针以及一个保存局部变量的调用栈。通常情况下&#xff0c;一个进程依照一个单序列控制流顺序执行&#xff0c;这个控制流被称为该进程的主线…...

【Linux】基本系统维护命令

&#x1f60a;&#x1f60a;作者简介&#x1f60a;&#x1f60a; &#xff1a; 大家好&#xff0c;我是南瓜籽&#xff0c;一个在校大二学生&#xff0c;我将会持续分享C/C相关知识。 &#x1f389;&#x1f389;个人主页&#x1f389;&#x1f389; &#xff1a; 南瓜籽的主页…...

高数:数列的收敛

数列特点无限个数特定顺序数列和集合区别集合可以乱序&#xff0c;数列不行集合出现重复元素依然相同&#xff0c;数列出现新的重复元素就不相等[1&#xff0c;2&#xff0c;3&#xff0c;4][1&#xff0c;2&#xff0c;3&#xff0c;3&#xff0c;4]对集合来说相等&#xff0c…...

不平凡的一天——

作者&#xff1a;指针不指南吗 专栏&#xff1a;个人日常记录 &#x1f43e;或许会很慢&#xff0c;但是不可以停下来&#x1f43e; 文章目录1.自我介绍2.上学期3.不凡的一天4.新学期写个博客&#xff0c;简单记录一下&#xff0c;新学期加油&#xff01;&#xff01;&#xff…...

【Java基础】Map遍历的5种方式

目录 创建一个集合 方式一&#xff1a;Iterator 迭代器遍历 map.entrySet().iterator(); map.keySet().iterator(); 方式二&#xff1a;For Each方式遍历 map.forEach(BiConsumer action) 方式三&#xff1a;获取Collection集合 map.values().forEach() 方式四&#x…...

第十四届蓝桥杯三月真题刷题训练——第 2 天

目录 题目1&#xff1a;奇数倍数 代码: 题目2&#xff1a;求值 代码: 题目3&#xff1a;求和 代码: 题目4&#xff1a;数位排序 代码: 题目1&#xff1a;奇数倍数 题目描述 本题为填空题&#xff0c;只需要算出结果后&#xff0c;在代码中使用输出语句将所填结果输出即…...

自然语言处理历史最全预训练模型(部署)汇集分享

什么是预训练模型&#xff1f;预练模型是其他人为解决类似问题而创建的且已经训练好的模型。代替从头开始建立模型来解决类似的问题&#xff0c;我们可以使用在其他问题上训练过的模型作为起点。预训练的模型在相似的应用程序中可能不是100&#xff05;准确的。本文整理了自然语…...

csdn写文章自定义表格怎么做

前言 CSDN写文章时&#xff0c;经常会用到表格&#xff0c;不同于Word文档中直接插入表格&#xff08;自定义几行几列&#xff09;&#xff0c;使用CSDN自带的md文本编辑器时&#xff0c;很难快速插入想要的表格样式&#xff0c;追究原因&#xff0c;也是因为md的语法问题&…...

Pytorch处理数据与训练网络问题汇总(协同训练)

基础语法 模型训练 【Swin-Unet】官方代码预训练权重加载函数load_from() 实际上由于SwinUnet是一个encoder-decoder对称的结构&#xff0c;因此加载权重时&#xff0c;作者并没有像通常那样仅仅加载encoder部分而不加载decoder部分&#xff0c;而是同时将encoder的权重对称地…...

机器学习:基于神经网络对用户评论情感分析预测

机器学习&#xff1a;基于神经网络对用户评论情感分析预测 作者&#xff1a;AOAIYI 作者简介&#xff1a;Python领域新星作者、多项比赛获奖者&#xff1a;AOAIYI首页 &#x1f60a;&#x1f60a;&#x1f60a;如果觉得文章不错或能帮助到你学习&#xff0c;可以点赞&#x1f4…...

Vue3之组件间传值避坑指南

组件间传值的两个坑 我们都知道父组件可以把值传递到自组件中&#xff0c;但是有时候子组件需要修改这个父组件传递过来的这个值&#xff0c;我们可以想象下能修改成功吗&#xff1f;这是坑之一。我们在组件间传值的时候&#xff0c;都是一个属性名对应一个值&#xff0c;接收…...

C++:std::is_convertible

C++标志库中提供is_convertible,可以测试一种类型是否可以转换为另一只类型: template <class From, class To> struct is_convertible; 使用举例: #include <iostream> #include <string>using namespace std;struct A { }; struct B : A { };int main…...

【Oracle APEX开发小技巧12】

有如下需求&#xff1a; 有一个问题反馈页面&#xff0c;要实现在apex页面展示能直观看到反馈时间超过7天未处理的数据&#xff0c;方便管理员及时处理反馈。 我的方法&#xff1a;直接将逻辑写在SQL中&#xff0c;这样可以直接在页面展示 完整代码&#xff1a; SELECTSF.FE…...

黑马Mybatis

Mybatis 表现层&#xff1a;页面展示 业务层&#xff1a;逻辑处理 持久层&#xff1a;持久数据化保存 在这里插入图片描述 Mybatis快速入门 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/6501c2109c4442118ceb6014725e48e4.png //logback.xml <?xml ver…...

Python实现prophet 理论及参数优化

文章目录 Prophet理论及模型参数介绍Python代码完整实现prophet 添加外部数据进行模型优化 之前初步学习prophet的时候&#xff0c;写过一篇简单实现&#xff0c;后期随着对该模型的深入研究&#xff0c;本次记录涉及到prophet 的公式以及参数调优&#xff0c;从公式可以更直观…...

LeetCode - 199. 二叉树的右视图

题目 199. 二叉树的右视图 - 力扣&#xff08;LeetCode&#xff09; 思路 右视图是指从树的右侧看&#xff0c;对于每一层&#xff0c;只能看到该层最右边的节点。实现思路是&#xff1a; 使用深度优先搜索(DFS)按照"根-右-左"的顺序遍历树记录每个节点的深度对于…...

算法岗面试经验分享-大模型篇

文章目录 A 基础语言模型A.1 TransformerA.2 Bert B 大语言模型结构B.1 GPTB.2 LLamaB.3 ChatGLMB.4 Qwen C 大语言模型微调C.1 Fine-tuningC.2 Adapter-tuningC.3 Prefix-tuningC.4 P-tuningC.5 LoRA A 基础语言模型 A.1 Transformer &#xff08;1&#xff09;资源 论文&a…...

算法:模拟

1.替换所有的问号 1576. 替换所有的问号 - 力扣&#xff08;LeetCode&#xff09; ​遍历字符串​&#xff1a;通过外层循环逐一检查每个字符。​遇到 ? 时处理​&#xff1a; 内层循环遍历小写字母&#xff08;a 到 z&#xff09;。对每个字母检查是否满足&#xff1a; ​与…...

Ubuntu Cursor升级成v1.0

0. 当前版本低 使用当前 Cursor v0.50时 GitHub Copilot Chat 打不开&#xff0c;快捷键也不好用&#xff0c;当看到 Cursor 升级后&#xff0c;还是蛮高兴的 1. 下载 Cursor 下载地址&#xff1a;https://www.cursor.com/cn/downloads 点击下载 Linux (x64) &#xff0c;…...

高防服务器价格高原因分析

高防服务器的价格较高&#xff0c;主要是由于其特殊的防御机制、硬件配置、运营维护等多方面的综合成本。以下从技术、资源和服务三个维度详细解析高防服务器昂贵的原因&#xff1a; 一、硬件与技术投入 大带宽需求 DDoS攻击通过占用大量带宽资源瘫痪目标服务器&#xff0c;因此…...

Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解

文章目录 1. 题目描述1.1 链表节点定义 2. 理解题目2.1 问题可视化2.2 核心挑战 3. 解法一&#xff1a;HashSet 标记访问法3.1 算法思路3.2 Java代码实现3.3 详细执行过程演示3.4 执行结果示例3.5 复杂度分析3.6 优缺点分析 4. 解法二&#xff1a;Floyd 快慢指针法&#xff08;…...