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

MyBatis详细教程!!(入门版)

目录

什么是MyBatis?

MyBatis入门

 1)创建工程

2)数据准备

3)配置数据库连接字符串

4)写持久层代码

5)生成测试类

MyBatis打印日志

传递参数

MyBatis的增、删、改

增(Insert)

删(Delete)

改(Update)

查(Select)

使用XML方式

增删改查

增(Insert)

删(Delete)

改(Update)

查(Select)

补充:MySQL开发规范


什么是MyBatis?

MyBatis是一款持久层框架,用于简化JDBC开发

持久层:持久化操作的层,通常指数据访问层(DAO),是用来操作数据库的

MyBatis入门

  1. 准备工作(创建springboot工程、数据库表准备、实体类)
  2. 引入MyBatis的相关依赖,配置MyBatis(数据库连接信息)
  3. 编写SQL语句(注解/XML)
  4. 测试

 1)创建工程

     创建springboot工程,并引入MyBatis、MySQL依赖

<!--Mybatis 依赖包--> 
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.1</version>
</dependency>
<!--mysql驱动包-->
<dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId>
</dependency>

MyBatis是一个持久层框架,具体的数据库存储和数据操作还是在MySQL中操作的

2)数据准备

  创建用户表

DROP DATABASE IF EXISTS mybatis_test; CREATE DATABASE mybatis_test;
DEFAULT CHARACTER SET utf8mb4;
USE mybatis_test;
DROP TABLE IF EXISTS userinfo;
CREATE TABLE `userinfo` ( `id` INT ( 11 ) NOT NULL AUTO_INCREMENT, `username` VARCHAR ( 127 ) NOT NULL, `password` VARCHAR ( 127 ) NOT NULL, `age` TINYINT ( 4 ) NOT NULL, `gender` TINYINT ( 4 ) DEFAULT '0' COMMENT '1-男 2-⼥ 0-默认', `phone` VARCHAR ( 15 ) DEFAULT NULL, `delete_flag` TINYINT ( 4 ) DEFAULT 0 COMMENT '0-正常, 1-删除', `create_time` DATETIME DEFAULT now(), `update_time` DATETIME DEFAULT now(), PRIMARY KEY ( `id` ) ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;
INSERT INTO mybatis_test.userinfo ( username, `password`, age, gender, phone ) VALUES ( 'admin', 'admin', 18, 1, '18612340001' ); INSERT INTO mybatis_test.userinfo ( username, `password`, age, gender, phone ) VALUES ( 'zhangsan', 'zhangsan', 18, 1, '18612340002' ); INSERT INTO mybatis_test.userinfo ( username, `password`, age, gender, phone ) VALUES ( 'lisi', 'lisi', 18, 1, '18612340003' ); INSERT INTO mybatis_test.userinfo ( username, `password`, age, gender, phone ) VALUES ( 'wangwu', 'wangwu', 18, 1, '18612340004' );

创建对应实体类UserInfo

(注意实体类中的属性名与表中的字段名一一对应)

import lombok.Data;
import java.util.Date;
@Data
public class UserInfo {private Integer id;private String username;private String password;private Integer age;private Integer gender;private String phone;private  Integer deleteFlag;private Date createTime;private Date updateTime;
}

3)配置数据库连接字符串
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=falseusername: rootpassword: root #改为你的数据库密码driver-class-name: com.mysql.cj.jdbc.Driver

4)写持久层代码

创建持久层接口UserInfoMapper

@Mapper
public interface UserInfoMapper {@Select("select username,`password`,age,gender,phone from userinfo")public List<UserInfo> queryAllUser();
}

Mybatis的持久层接⼝规范⼀般都叫XxxMapper

@Mapper注解:表⽰是MyBatis中的Mapper接⼝

程序运⾏时,框架会⾃动⽣成接⼝的实现类对象(代理对象),并给交Spring的IOC容器管理

5)生成测试类

在需要测试的Mapper接口中,右键->Generate->Test

书写测试代码

@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid queryAllUser() {List<UserInfo> userInfoList=userInfoMapper.queryAllUser();System.out.println(userInfoList);}
}

注解@SpringBootTest,该测试类在运行时,就会自动加载Spring的运行环境

MyBatis打印日志

通过日志看到sql语句的执行、执行传递的参数以及执行结果

mybatis:configuration: # 配置打印 MyBatis⽇志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

①:查询语句

②:传递参数及类型

③:SQL执行结果

传递参数
@Mapper
public interface UserInfoMapper {@Select("select username,`password`,age,gender,phone from userinfo where id=#{id}")public List<UserInfo> queryAllUser(Integer id);
}
@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid queryAllUser() {List<UserInfo> userInfoList=userInfoMapper.queryAllUser(4);System.out.println(userInfoList);}
}

MyBatis的增、删、改

增(Insert)

(Mapper中)

@Mapper
public interface UserInfoMapper {@Options(useGeneratedKeys = true,keyProperty = "id")@Insert("insert into userinfo(username,`password`,age,gender,phone) values (#{username},#{password},#{age},#{gender},#{phone})")public Integer insert(UserInfo userInfo);}

(MapperTest中)

@SpringBootTest
class UserInfoMapperTest {@Testvoid insert() {UserInfo userInfo=new UserInfo();userInfo.setAge(10);userInfo.setGender(2);userInfo.setUsername("赵六");userInfo.setPhone("123456");userInfo.setPassword("123");userInfoMapper.insert(userInfo);}
}

注意:如果使用了@Param属性来重命名,#{...}需要使用“参数.属性”来获取

@Insert("insert into userinfo(username,`password`,age,gender,phone) values (#{userinfo.username},#{userinfo.password},#{userinfo.age},#{userinfo.gender},#{userinfo.phone})")
public Integer insert(@Param("userinfo") UserInfo userInfo);

Insert语句返回值是收影响的行数

有些情况下我们需要获得新插入数据的id,此时使用@Options注解

@Options(useGeneratedKeys = true,keyProperty = "id")@Insert("insert into userinfo(username,`password`,age,gender,phone) values (#{username},#{password},#{age},#{gender},#{phone})")public Integer insert(UserInfo userInfo);
@Test
void insert() {UserInfo userInfo=new UserInfo()userInfo.setAge(10);userInfo.setGender(2);userInfo.setUsername("赵六");userInfo.setPhone("123456");userInfo.setPassword("123");Integer count = userInfoMapper.insert(userInfo);//设置useGeneratedKeys=true后,方法返回值仍然是受影响行数System.out.println("添加数据条数:" +count +",数据id:"+userInfo.getId());
}

useGeneratedKeys:令MyBatis使⽤JDBC的getGeneratedKeys⽅法来取出由数据库内部⽣成的主键,默认值:false.

keyProperty:这指定了实体类中用于存储数据库生成的主键的属性的名称,当 MyBatis 从数据库获取到新生成的主键后,它会将这个值设置到实体类的 id 属性中。

删(Delete)
@Delete("delete from userinfo where id=#{id}")public void delete(Integer id);
@Test
void delete() {userInfoMapper.delete(4);
}

改(Update)
@Update("update userinfo set username=#{username} where id=#{id}")
public void update(String username,Integer id);
@Test
void update() {userInfoMapper.update("王五",3);
}

查(Select)

上述查询中,我们发现,只有在java对象属性和数据库字段名一样(忽略大小写)时,才会进行赋值

(java对象中deleteFlag、createTime、updateTime属性与数据库中字段delete_flag、create_time、update_time对应不上)

解决办法

①起别名

@Select("select id, username, `password`, age, gender, phone, delete_flag as 
deleteFlag,create_time as createTime, update_time as updateTime from userinfo")
public List<UserInfo> queryAllUser();

②结果映射

@Select("select id, username, `password`, age, gender, phone, delete_flag, 
create_time, update_time from userinfo")
@Results({@Result(column = "delete_flag",property = "deleteFlag"),@Result(column = "create_time",property = "createTime"),@Result(column = "update_time",property = "updateTime")
})
List<UserInfo> queryAllUser();

可以给Results定义一个名称,使其他sql也能复用这个映射关系

@Select("select id, username, `password`, age, gender, phone, delete_flag, 
create_time, update_time from userinfo")
@Results(id = "resultMap",value = {@Result(column = "delete_flag",property = "deleteFlag"),@Result(column = "create_time",property = "createTime"),@Result(column = "update_time",property = "updateTime")
})
List<UserInfo> queryAllUser();
@Select("select id, username, `password`, age, gender, phone, delete_flag, 
create_time, update_time " +"from userinfo where id= #{userid} ")
@ResultMap(value = "resultMap")
UserInfo queryById(@Param("userid") Integer id);

③开启驼峰命名(推荐)

数据库中字段通常使用蛇形命名,java属性通常使用驼峰命名,可以通过配置使得这两种命名方式自动映射(abc_xyz => abcXyz)

mybatis:configuration:map-underscore-to-camel-case: true #配置驼峰⾃动转换 

MyBatis有使用注解和XML两种方式

下面,我们介绍

使用XML方式

①配置数据库连接字符串

#数据库连接配置
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=falseusername: rootpassword: root  //改成你自己的密码driver-class-name: com.mysql.cj.jdbc.Driver
#配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件
mybatis:mapper-locations: classpath:mapper/**Mapper.xml

②写持久层代码

1)方法定义Interface

@Mapper
public interface UserInfoXMLMapper {List<UserInfo> queryAllUser();
}

2)方法实现:UserInfoXMLMapper.xml(路径参考yml中的配置 mapper-locations: classpath:mapper/**Mapper.xml)

<?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.example.sql.mapper.UserInfoMapper"><select id="queryAllUser" resultType="com.example.sql.UserInfo">select username,`password`,age,gender,phone from userinfo</select>
</mapper>
  • <mapper>标签:需要指定namespace 属性,表⽰命名空间,值为mapper接⼝的全限定 名,包括全包名.类名。
  • <select>查询标签:是⽤来执⾏数据库的查询操作的:

        ◦id :是和Interface (接⼝)中定义的⽅法名称⼀样的,表⽰对接⼝的具体实现⽅法。

        ◦ resultType :是返回的数据类型,也就是开头我们定义的实体类

单元测试

@SpringBootTest
public class UserInfoXMLMapperTest {@Autowiredprivate UserInfoXMLMapper userInfoXMLMapper;@Testvoid queryAllUser(){List<UserInfo> userInfoList=userInfoXMLMapper.queryAllUser();System.out.println(userInfoList);}}

增删改查
增(Insert)
@Mapper
public interface UserInfoXMLMapper {Integer insertUser(UserInfo userInfo);
}
<?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.example.sql.mapper.UserInfoXMLMapper"><insert id="insertUser" useGeneratedKeys="true" keyProperty="id">insert into userinfo (username,`password`,age,gender,phone) values(#{username},#{password},#{age},#{gender},#{phone})</insert>
</mapper>
@SpringBootTest
public class UserInfoXMLMapperTest {@Autowiredprivate UserInfoXMLMapper userInfoXMLMapper;@Testvoid insertUser(){UserInfo userInfo=new UserInfo();userInfo.setAge(10);userInfo.setGender(2);userInfo.setUsername("赵七");userInfo.setPhone("123457");userInfo.setPassword("123");Integer count=userInfoXMLMapper.insertUser(userInfo);System.out.println(count);}
}

使用@Param设置参数名称与注解类似

Integer insertUser(@Param("userinfo") UserInfo userInfo);
<insert id="insertUser">insert into userinfo (username, `password`, age, gender, phone) values(#{userinfo.username},#{userinfo.password},#{userinfo.age},#
{userinfo.gender},#{userinfo.phone})
</insert>

返回自增id

<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">insert into userinfo (username, `password`, age, gender, phone) values(#{userinfo.username},#{userinfo.password},#{userinfo.age},#
{userinfo.gender},#{userinfo.phone})
</insert>

删(Delete)

UserInfoXMLMapper接⼝

Integer deleteUser(Integer id);

UserInfoXMLMapper.xml实现:

<delete id="deleteUser">delete from userinfo where id = #{id}
</delete>

改(Update)

UserInfoXMLMapper接⼝:

Integer updateUser(UserInfo userInfo);

UserInfoXMLMapper.xml实现:

update id="updateUser"> update userinfo set username=#{username} where id=#{id} update>

查(Select)

与注解方式类似,查找操作也涉及到java对象和数据库字段命名问题

解决办法

①起别名

②结果映射

③开启驼峰命名

(①③与注解一样),下面介绍结果映射

<resultMap id="BaseMap" type="com.example.demo.model.UserInfo"><id column="id" property="id"></id><result column="delete_flag" property="deleteFlag"></result><result column="create_time" property="createTime"></result><result column="update_time" property="updateTime"></result>
</resultMap>
<select id="queryAllUser" resultMap="BaseMap">select id, username,`password`, age, gender, phone, delete_flag, 
create_time, update_time from userinfo
</select>

开发中,建议简单sql使用注解方式,复杂sql(如动态sql)使用xml方式

注解方式和xml方式可以一起使用


 

补充:MySQL开发规范

①表名、字段名使⽤⼩写字⺟或数字,单词之间以下划线分割.尽量避免出现数字开头或者两个下划线,中间只出现数字.数据库字段名的修改代价很⼤,所以字段名称需要慎重考虑。

MySQL在Windows下不区分⼤⼩写,但在Linux下默认是区分⼤⼩写.

因此,数据库名、表名、字段名都不允许出现任何⼤写字⺟,避免节外⽣枝

②表必备三字段:id,create_time,update_time,id必为主键,类型为bigintunsigned,​​​​​​​create_time,update_time的类型均为datetime类型,create_time表⽰创建时间, update_time表⽰更新时间

③在表查询中,避免使⽤*作为查询的字段列表,标明需要哪些字段

1. 增加查询分析器解析成本

2. 增减字段容易与resultMap配置不⼀致

3. ⽆⽤字段增加⽹络消耗,尤其是text类型的字段

相关文章:

MyBatis详细教程!!(入门版)

目录 什么是MyBatis&#xff1f; MyBatis入门 1&#xff09;创建工程 2&#xff09;数据准备 3&#xff09;配置数据库连接字符串 4&#xff09;写持久层代码 5&#xff09;生成测试类 MyBatis打印日志 传递参数 MyBatis的增、删、改 增&#xff08;Insert&#xff0…...

c++ using 关键字

在C中&#xff0c;using 关键字有多种用途&#xff0c;但最常见的用途之一是在命名空间&#xff08;namespace&#xff09;中引入名称&#xff0c;以避免在代码中频繁使用命名空间前缀。此外&#xff0c;using 还可以用于类型别名&#xff08;typedef 的替代品&#xff09;和模…...

AIGC时代算法工程师的面试秘籍(2024.4.29-5.12第十三式) |【三年面试五年模拟】

写在前面 【三年面试五年模拟】旨在整理&挖掘AI算法工程师在实习/校招/社招时所需的干货知识点与面试方法&#xff0c;力求让读者在获得心仪offer的同时&#xff0c;增强技术基本面。也欢迎大家提出宝贵的优化建议&#xff0c;一起交流学习&#x1f4aa; 欢迎大家关注Rocky…...

Docker Portainer使用

Portainer是什么 Docker Portainer是一个轻量级的 Web UI 管理界面,可以用来管理Docker环境。它提供了一个直观的控制台,用户可以通过它来管理Docker主机、容器、网络、卷等Docker资源。 Portainer的主要功能和特点包括: 容器管理:可以查看、启动、停止、删除容器,以及查看容器…...

创新系列-既要保留<a/>标签右键功能, 又不要href导致点击页面刷新, 希望click实现vue-router跳转

发布时间&#xff1a;2024/05/22 如果您有适合我的项目机会给到我&#xff0c;这是我的简历&#xff1a;Resume 思路&#xff1a; 思路原理&#xff1a;实践发现href为null或者" "的时候是不起作用的 将href属性绑定的固定路径设置为响应式数据变量a&#xff0c;a初…...

【OceanBase诊断调优】—— KVCache 排查手册

原文链接&#xff1a;OceanBase分布式数据库-海量数据 笔笔算数 本文介绍 KVcache 相关问题的排查方法。 KVCache 相关概念 在进行排查前&#xff0c;需要了解几个概念。 pin 一个 cache 块 ( memblock ) 被 pin 住&#xff0c;表示它正在被引用。 cache 的由多个定长的块组成…...

核函数的介绍

1.核函数的介绍&#xff1a; 1、用线性核等于没有用核。 2、多项式核&#xff1a;随着d越大&#xff0c;则 fai(X) 对应的维度将越高。&#xff08;可以通过d得到对应的fai(X)函数&#xff09;。 3、高斯核函数&#xff1a;无限维度。 4、tanh核。 2.如何选择核函数的参数&am…...

使用pytorch写一个简单的vae网络用于生成minist手写数字图像

文章目录 代码结果代码 import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F from torch.utils.data import DataLoader from torchvision impo...

Windows平台让标准输入(stdin)的阻塞立即返回

文章目录 背景介绍代码示例版本1-基本命令处理版本2-多线程命令处理&#xff0c;不阻塞主函数版本3-即使没有用户输入&#xff0c;也能立即退出 背景介绍 在开发命令行工具或控制台应用程序时&#xff0c;经常需要处理用户输入。常规做法是使用标准输入&#xff08;stdin&…...

Spring中的Aware接口

Spring中的Aware接口 Aware接口介绍 Aware是Spring中的接口&#xff0c;它的作用是可以让Bean获取到运行环境的相关信息。比如获取到上下文、Bean在容器中的名称等。 Spring中提供了很多Aware接口的子类&#xff0c;具体如下&#xff1a; 常用接口的作用如下&#xff1a; …...

FFmpeg滤镜完整列表

FFmpeg滤镜完整列表 滤镜名称 用途 acompressor 压缩音频信号,当输入信号超过某个预设阈值时&#xff0c;压缩器就会开始工作。该滤镜使音量大的部分变得不那么响亮&#xff0c;而音量小的部分相对变得响亮&#xff0c;这样就可以使整体听起来更加均衡&#xff0c;常用于音乐…...

深入探索Python基础:两个至关重要的函数

新书上架~&#x1f447;全国包邮奥~ python实用小工具开发教程http://pythontoolsteach.com/3 欢迎关注我&#x1f446;&#xff0c;收藏下次不迷路┗|&#xff40;O′|┛ 嗷~~ 目录 一、初学者的基石&#xff1a;print与input函数 二、类型转换&#xff1a;从字符串到浮点数…...

探索集合python(Set)的神秘面纱:它与字典有何不同?

新书上架~&#x1f447;全国包邮奥~ python实用小工具开发教程http://pythontoolsteach.com/3 欢迎关注我&#x1f446;&#xff0c;收藏下次不迷路┗|&#xff40;O′|┛ 嗷~~ 目录 一、集合&#xff08;Set&#xff09;与字典&#xff08;Dictionary&#xff09;的初识 1. …...

火山引擎“奇袭”阿里云

图片&#xff5c;电影《美国队长3》剧照 ©自象限原创 作者丨程心 编辑丨罗辑 大模型价格战&#xff0c;已经不是什么新闻。 从OpenAI发布GPT-4o&#xff0c;将API价格下调50%&#xff0c;并宣布面向普通用户免费开始&#xff0c;就标志着大模型的竞争从性能进入到了成本…...

牛客网刷题 | BC94 反向输出一个四位数

目前主要分为三个专栏&#xff0c;后续还会添加&#xff1a; 专栏如下&#xff1a; C语言刷题解析 C语言系列文章 我的成长经历 感谢阅读&#xff01; 初来乍到&#xff0c;如有错误请指出&#xff0c;感谢&#xff01; 描述 将一个四位数&…...

记一次MySQL执行修改语句超时问题

异常问题 原因分析 这个问题发生在开发环境&#xff0c;怀疑是提交事务时终止项目运行&#xff0c;没有提交该事务&#xff0c;造成死锁 调试该事务时时间太长&#xff0c;为什么说有这个原因呢&#xff0c;因为通过查找日志显示 The client was disconnected by the server …...

linux fork()函数调用原理

在Linux中,fork函数用于创建一个新的进程,该进程是调用进程的子进程。fork函数的实现涉及用户态和内核态之间的交互。下面我将详细说明fork函数在代码流程中的原理和用户态与内核态的交互过程。 用户态调用fork函数: 用户程序调用fork函数,该函数是libc库提供的一个封装函数…...

【电控笔记5.9】编码器脉冲计算速度MT法

总结 编码器的脉冲计算速度可以使用多种方法,其中一种常用的方法是“MT法” (Measuring Time Method),即测量时间法。该方法通过测量编码器脉冲间的时间来计算速度。这种方法在高精度速度测量中非常有效,特别是在速度较低时。 MT法计算速度的基本原理 MT法的基本原理是通过…...

go-zero 实战(4)

中间件 在 userapi 项目中引入中间件。go项目中的中间可以处理请求之前和之后的逻辑。 1. 在 userapi/internal目录先创建 middlewares目录&#xff0c;并创建 user.go文件 package middlewaresimport ("github.com/zeromicro/go-zero/core/logx""net/http&q…...

go语言泛型Generic最佳实践 --- slices包

在go的内置包slices中&#xff0c; 所有的函数函数都使用了泛型&#xff0c; 各种各样的泛型&#xff0c; 可以说这个包绝对是go语言泛型学习的最佳实践之一&#xff01; 来&#xff0c;先来瞄一眼&#xff0c;看看这个slices包里面的函数原型定义&#xff1a; func BinarySe…...

华为云AI开发平台ModelArts

华为云ModelArts&#xff1a;重塑AI开发流程的“智能引擎”与“创新加速器”&#xff01; 在人工智能浪潮席卷全球的2025年&#xff0c;企业拥抱AI的意愿空前高涨&#xff0c;但技术门槛高、流程复杂、资源投入巨大的现实&#xff0c;却让许多创新构想止步于实验室。数据科学家…...

TDengine 快速体验(Docker 镜像方式)

简介 TDengine 可以通过安装包、Docker 镜像 及云服务快速体验 TDengine 的功能&#xff0c;本节首先介绍如何通过 Docker 快速体验 TDengine&#xff0c;然后介绍如何在 Docker 环境下体验 TDengine 的写入和查询功能。如果你不熟悉 Docker&#xff0c;请使用 安装包的方式快…...

java_网络服务相关_gateway_nacos_feign区别联系

1. spring-cloud-starter-gateway 作用&#xff1a;作为微服务架构的网关&#xff0c;统一入口&#xff0c;处理所有外部请求。 核心能力&#xff1a; 路由转发&#xff08;基于路径、服务名等&#xff09;过滤器&#xff08;鉴权、限流、日志、Header 处理&#xff09;支持负…...

【力扣数据库知识手册笔记】索引

索引 索引的优缺点 优点1. 通过创建唯一性索引&#xff0c;可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度&#xff08;创建索引的主要原因&#xff09;。3. 可以加速表和表之间的连接&#xff0c;实现数据的参考完整性。4. 可以在查询过程中&#xff0c;…...

3.3.1_1 检错编码(奇偶校验码)

从这节课开始&#xff0c;我们会探讨数据链路层的差错控制功能&#xff0c;差错控制功能的主要目标是要发现并且解决一个帧内部的位错误&#xff0c;我们需要使用特殊的编码技术去发现帧内部的位错误&#xff0c;当我们发现位错误之后&#xff0c;通常来说有两种解决方案。第一…...

mongodb源码分析session执行handleRequest命令find过程

mongo/transport/service_state_machine.cpp已经分析startSession创建ASIOSession过程&#xff0c;并且验证connection是否超过限制ASIOSession和connection是循环接受客户端命令&#xff0c;把数据流转换成Message&#xff0c;状态转变流程是&#xff1a;State::Created 》 St…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院查看报告小程序

一、开发环境准备 ​​工具安装​​&#xff1a; 下载安装DevEco Studio 4.0&#xff08;支持HarmonyOS 5&#xff09;配置HarmonyOS SDK 5.0确保Node.js版本≥14 ​​项目初始化​​&#xff1a; ohpm init harmony/hospital-report-app 二、核心功能模块实现 1. 报告列表…...

python爬虫:Newspaper3k 的详细使用(好用的新闻网站文章抓取和解析的Python库)

更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 一、Newspaper3k 概述1.1 Newspaper3k 介绍1.2 主要功能1.3 典型应用场景1.4 安装二、基本用法2.2 提取单篇文章的内容2.2 处理多篇文档三、高级选项3.1 自定义配置3.2 分析文章情感四、实战案例4.1 构建新闻摘要聚合器…...

实现弹窗随键盘上移居中

实现弹窗随键盘上移的核心思路 在Android中&#xff0c;可以通过监听键盘的显示和隐藏事件&#xff0c;动态调整弹窗的位置。关键点在于获取键盘高度&#xff0c;并计算剩余屏幕空间以重新定位弹窗。 // 在Activity或Fragment中设置键盘监听 val rootView findViewById<V…...

浅谈不同二分算法的查找情况

二分算法原理比较简单&#xff0c;但是实际的算法模板却有很多&#xff0c;这一切都源于二分查找问题中的复杂情况和二分算法的边界处理&#xff0c;以下是博主对一些二分算法查找的情况分析。 需要说明的是&#xff0c;以下二分算法都是基于有序序列为升序有序的情况&#xf…...