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

SpringBoot第28讲:SpringBoot集成MySQL - MyBatis-Plus方式

SpringBoot第28讲:SpringBoot集成MySQL - MyBatis-Plus方式

本文是SpringBoot第28讲,MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。MyBatis-Plus在国内也有很多的用户,本文主要介绍MyBatis-Plus和SpringBoot的集成。

文章目录

  • SpringBoot第28讲:SpringBoot集成MySQL - MyBatis-Plus方式
    • 1、知识准备
      • 1.1、为什么会诞生MyBatis-Plus?
      • 1.2、支持数据库
      • 1.3、整体架构
    • 2、简单示例
      • 2.1、准备DB和依赖配置
      • 2.2、定义dao
      • 2.3、定义Service接口和实现类
      • 2.4、controller
      • 2.5、分页配置
    • 3、进一步理解
      • 3.1、比较好的实践
      • 3.2、除了分页插件之外还提供了哪些插件?
    • 4、示例源码

1、知识准备

MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

1.1、为什么会诞生MyBatis-Plus?

正如前文所述(SpringBoot第24讲:SpringBoot集成MySQL - MyBatis XML方式),为了更高的效率,出现了MyBatis-Plus这类工具,对MyBatis进行增强。

  1. 考虑到MyBatis是半自动化ORM,MyBatis-Plus 启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作; 并且内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求;总体上让其支持全自动化的使用方式(本质上借鉴了Hibernate思路);
  2. 考虑到Java8 Lambda(函数式编程)开始流行,MyBatis-Plus支持 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错;
  3. 考虑到MyBatis还需要独立引入PageHelper分页插件,MyBatis-Plus支持了内置分页插件,同PageHelper一样基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询;
  4. 考虑到自动化代码生成方式,MyBatis-Plus也支持了内置代码生成器,采用代码或者 Maven 插件可快速生成 Mapper、Model、 Service、Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用;
  5. 考虑到SQL性能优化等问题,MyBatis-Plus内置性能分析插件, 可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询;(能对慢查询做到聚类吗?)
  6. 其它还有解决一些常见开发问题,比如支持主键自动生成,支持4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题;以及内置全局拦截插件,提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

1.2、支持数据库

任何能使用 MyBatis 进行 CRUD, 并且支持标准 SQL 的数据库,具体支持情况如下:

  • MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,ClickHouse,Sybase,OceanBase,Firebird,Cubrid,Goldilocks,csiidb
  • 达梦数据库,虚谷数据库,人大金仓数据库,南大通用(华库)数据库,南大通用数据库,神通数据库,瀚高数据库

1.3、整体架构

  • img

2、简单示例

这里沿用上一篇文章的数据库, 向你展示SpringBoot + MyBatis-Plus的使用等。

2.1、准备DB和依赖配置

创建MySQL的schema test_db, 导入SQL 文件如下

DROP TABLE IF EXISTS `tb_role`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tb_role` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) NOT NULL,`role_key` varchar(255) NOT NULL,`description` varchar(255) DEFAULT NULL,`create_time` datetime DEFAULT NULL,`update_time` datetime DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `tb_role`
--LOCK TABLES `tb_role` WRITE;
/*!40000 ALTER TABLE `tb_role` DISABLE KEYS */;
INSERT INTO `tb_role` VALUES (1,'admin','admin','admin','2021-09-08 17:09:15','2021-09-08 17:09:15');
/*!40000 ALTER TABLE `tb_role` ENABLE KEYS */;
UNLOCK TABLES;--
-- Table structure for table `tb_user`
--DROP TABLE IF EXISTS `tb_user`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tb_user` (`id` int(11) NOT NULL AUTO_INCREMENT,`user_name` varchar(45) NOT NULL,`password` varchar(45) NOT NULL,`email` varchar(45) DEFAULT NULL,`phone_number` int(11) DEFAULT NULL,`description` varchar(255) DEFAULT NULL,`create_time` datetime DEFAULT NULL,`update_time` datetime DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `tb_user`
--LOCK TABLES `tb_user` WRITE;
/*!40000 ALTER TABLE `tb_user` DISABLE KEYS */;
INSERT INTO `tb_user` VALUES (1,'qiwenjie','123456','1172814226@qq.com',1212121213,'afsdfsaf','2021-09-08 17:09:15','2021-09-08 17:09:15');
/*!40000 ALTER TABLE `tb_user` ENABLE KEYS */;
UNLOCK TABLES;--
-- Table structure for table `tb_user_role`
--DROP TABLE IF EXISTS `tb_user_role`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tb_user_role` (`user_id` int(11) NOT NULL,`role_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `tb_user_role`
--LOCK TABLES `tb_user_role` WRITE;
/*!40000 ALTER TABLE `tb_user_role` DISABLE KEYS */;
INSERT INTO `tb_user_role` VALUES (1,1);
/*!40000 ALTER TABLE `tb_user_role` ENABLE KEYS */;
UNLOCK TABLES;

引入maven依赖

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version>
</dependency>
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version>
</dependency>

增加yml配置

spring:datasource:url: jdbc:mysql://localhost:3306/db_user?useSSL=false&autoReconnect=true&characterEncoding=utf8driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: qwj930828mybatis-plus:configuration:# 是否开启二级缓存cache-enabled: trueuse-generated-keys: truedefault-executor-type: REUSEuse-actual-param-name: true

2.2、定义dao

RoleDao

package springboot.mysql.mybatisplus.anno.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import springboot.mysql.mybatisplus.anno.entity.Role;/*** @author qiwenjie*/
public interface IRoleDao extends BaseMapper<Role> {
}

UserDao

package springboot.mysql.mybatisplus.anno.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import springboot.mysql.mybatisplus.anno.entity.User;
import springboot.mysql.mybatisplus.anno.entity.query.UserQueryBean;import java.util.List;/*** @author qiwenjie*/
public interface IUserDao extends BaseMapper<User> {List<User> findList(UserQueryBean userQueryBean);
}

这里你也同时可以支持 BaseMapper 方式和自己定义的xml的方法(比较适用于关联查询),比如 findList 是自定义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="springboot.mysql.mybatisplus.anno.dao.IUserDao"><resultMap type="springboot.mysql.mybatisplus.anno.entity.User" id="UserResult"><id     property="id"       	column="id"      		/><result property="userName"     column="user_name"    	/><result property="password"     column="password"    	/><result property="email"        column="email"        	/><result property="phoneNumber"  column="phone_number"  	/><result property="description"  column="description"  	/><result property="createTime"   column="create_time"  	/><result property="updateTime"   column="update_time"  	/><collection property="roles" ofType="springboot.mysql.mybatisplus.anno.entity.Role"><result property="id" column="id"  /><result property="name" column="name"  /><result property="roleKey" column="role_key"  /><result property="description" column="description"  /><result property="createTime"   column="create_time"  	/><result property="updateTime"   column="update_time"  	/></collection></resultMap><sql id="selectUserSql">select u.id, u.password, u.user_name, u.email, u.phone_number, u.description, u.create_time, u.update_time, r.name, r.role_key, r.description, r.create_time, r.update_timefrom tb_user uleft join tb_user_role ur on u.id=ur.user_idinner join tb_role r on ur.role_id=r.id</sql><select id="findList" parameterType="springboot.mysql.mybatisplus.anno.entity.query.UserQueryBean" resultMap="UserResult"><include refid="selectUserSql"/>where u.id != 0<if test="userName != null and userName != ''">AND u.user_name like concat('%', #{user_name}, '%')</if><if test="description != null and description != ''">AND u.description like concat('%', #{description}, '%')</if><if test="phoneNumber != null and phoneNumber != ''">AND u.phone_number like concat('%', #{phoneNumber}, '%')</if><if test="email != null and email != ''">AND u.email like concat('%', #{email}, '%')</if></select>
</mapper> 

2.3、定义Service接口和实现类

UserService接口

package springboot.mysql.mybatisplus.anno.service;import com.baomidou.mybatisplus.extension.service.IService;
import springboot.mysql.mybatisplus.anno.entity.User;
import springboot.mysql.mybatisplus.anno.entity.query.UserQueryBean;
import java.util.List;/*** @author qiwenjie*/
public interface IUserService extends IService<User> {List<User> findList(UserQueryBean userQueryBean);
}

User Service的实现类

package springboot.mysql.mybatisplus.anno.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import springboot.mysql.mybatisplus.anno.dao.IUserDao;
import springboot.mysql.mybatisplus.anno.entity.User;
import springboot.mysql.mybatisplus.anno.entity.query.UserQueryBean;
import springboot.mysql.mybatisplus.anno.service.IUserService;import java.util.List;@Service
public class UserDoServiceImpl extends ServiceImpl<IUserDao, User> implements IUserService {@Overridepublic List<User> findList(UserQueryBean userQueryBean) {return baseMapper.findList(userQueryBean);}
}

Role Service 接口

package springboot.mysql.mybatisplus.anno.service;import com.baomidou.mybatisplus.extension.service.IService;
import springboot.mysql.mybatisplus.anno.entity.Role;
import springboot.mysql.mybatisplus.anno.entity.query.RoleQueryBean;import java.util.List;public interface IRoleService extends IService<Role> {List<Role> findList(RoleQueryBean roleQueryBean);
}

Role Service 实现类

package springboot.mysql.mybatisplus.anno.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import springboot.mysql.mybatisplus.anno.dao.IRoleDao;
import springboot.mysql.mybatisplus.anno.entity.Role;
import springboot.mysql.mybatisplus.anno.entity.query.RoleQueryBean;
import springboot.mysql.mybatisplus.anno.service.IRoleService;
import java.util.List;@Service
public class RoleDoServiceImpl extends ServiceImpl<IRoleDao, Role> implements IRoleService {@Overridepublic List<Role> findList(RoleQueryBean roleQueryBean) {return lambdaQuery().like(StringUtils.isNotEmpty(roleQueryBean.getName()), Role::getName, roleQueryBean.getName()).like(StringUtils.isNotEmpty(roleQueryBean.getDescription()), Role::getDescription, roleQueryBean.getDescription()).like(StringUtils.isNotEmpty(roleQueryBean.getRoleKey()), Role::getRoleKey, roleQueryBean.getRoleKey()).list();}
}

2.4、controller

User Controller

package springboot.mysql.mybatisplus.anno.controller;import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springboot.mysql.mybatisplus.anno.entity.User;
import springboot.mysql.mybatisplus.anno.entity.query.UserQueryBean;
import springboot.mysql.mybatisplus.anno.entity.response.ResponseResult;
import springboot.mysql.mybatisplus.anno.service.IUserService;
import java.time.LocalDateTime;
import java.util.List;/*** @author qiwenjie*/
@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate IUserService userService;/*** @param user user param* @return user*/@ApiOperation("Add/Edit User")@PostMapping("add")public ResponseResult<User> add(User user) {if (user.getId() == null) {user.setCreateTime(LocalDateTime.now());}user.setUpdateTime(LocalDateTime.now());userService.save(user);return ResponseResult.success(userService.getById(user.getId()));}/*** @return user list*/@ApiOperation("Query User One")@GetMapping("edit/{userId}")public ResponseResult<User> edit(@PathVariable("userId") Long userId) {return ResponseResult.success(userService.getById(userId));}/*** @return user list*/@ApiOperation("Query User List")@GetMapping("list")public ResponseResult<List<User>> list(UserQueryBean userQueryBean) {return ResponseResult.success(userService.findList(userQueryBean));}
}

Role Controller

package springboot.mysql.mybatisplus.anno.controller;import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot.mysql.mybatisplus.anno.entity.Role;
import springboot.mysql.mybatisplus.anno.entity.query.RoleQueryBean;
import springboot.mysql.mybatisplus.anno.entity.response.ResponseResult;
import springboot.mysql.mybatisplus.anno.service.IRoleService;
import java.util.List;/*** @author qiwenjie*/
@RestController
@RequestMapping("/role")
public class RoleController {@Autowiredprivate IRoleService roleService;/*** @return role list*/@ApiOperation("Query Role List")@GetMapping("list")public ResponseResult<List<Role>> list(RoleQueryBean roleQueryBean) {return ResponseResult.success(roleService.findList(roleQueryBean));}
}

2.5、分页配置

通过配置内置的MybatisPlusInterceptor拦截器。

package springboot.mysql.mybatisplus.anno.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** MyBatis-plus configuration, add pagination interceptor.** @author qiwenjie*/
@Configuration
public class MyBatisConfig {/*** inject pagination interceptor.** @return pagination*/@Beanpublic PaginationInnerInterceptor paginationInnerInterceptor() {return new PaginationInnerInterceptor();}/*** add pagination interceptor.** @return MybatisPlusInterceptor*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor());return mybatisPlusInterceptor;}
}

3、进一步理解

MyBatis-plus学习梳理

  • 官方文档
  • 官方案例
  • 官方源码仓库
  • Awesome Mybatis-Plus

3.1、比较好的实践

总结下开发的过程中比较好的实践

1、Mapper层:继承BaseMapper

public interface IRoleDao extends BaseMapper<Role> {
}

2、Service层:继承ServiceImpl并实现对应接口

public class RoleDoServiceImpl extends ServiceImpl<IRoleDao, Role> implements IRoleService {}

3、Lambda函数式查询

@Override
public List<Role> findList(RoleQueryBean roleQueryBean) {return lambdaQuery().like(StringUtils.isNotEmpty(roleQueryBean.getName()), Role::getName, roleQueryBean.getName()).like(StringUtils.isNotEmpty(roleQueryBean.getDescription()), Role::getDescription, roleQueryBean.getDescription()).like(StringUtils.isNotEmpty(roleQueryBean.getRoleKey()), Role::getRoleKey, roleQueryBean.getRoleKey()).list();
}

4、分页采用内置MybatisPlusInterceptor

/*** inject pagination interceptor.* @return pagination*/
@Bean
public PaginationInnerInterceptor paginationInnerInterceptor() {return new PaginationInnerInterceptor();
}/*** add pagination interceptor.* @return MybatisPlusInterceptor*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor());return mybatisPlusInterceptor;
}

5、对于复杂的关联查询

可以配置原生xml方式, 在其中自定义ResultMap

<?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="springboot.mysql.mybatisplus.anno.dao.IUserDao"><resultMap type="springboot.mysql.mybatisplus.anno.entity.User" id="UserResult"><id     property="id"       	column="id"      		/><result property="userName"     column="user_name"    	/><result property="password"     column="password"    	/><result property="email"        column="email"        	/><result property="phoneNumber"  column="phone_number"  	/><result property="description"  column="description"  	/><result property="createTime"   column="create_time"  	/><result property="updateTime"   column="update_time"  	/><collection property="roles" ofType="springboot.mysql.mybatisplus.anno.entity.Role"><result property="id" column="id"  /><result property="name" column="name"  /><result property="roleKey" column="role_key"  /><result property="description" column="description"  /><result property="createTime"   column="create_time"  	/><result property="updateTime"   column="update_time"  	/></collection></resultMap><sql id="selectUserSql">select u.id, u.password, u.user_name, u.email, u.phone_number, u.description, u.create_time, u.update_time, r.name, r.role_key, r.description, r.create_time, r.update_timefrom tb_user uleft join tb_user_role ur on u.id=ur.user_idinner join tb_role r on ur.role_id=r.id</sql><select id="findList" parameterType="springboot.mysql.mybatisplus.anno.entity.query.UserQueryBean" resultMap="UserResult"><include refid="selectUserSql"/>where u.id != 0<if test="userName != null and userName != ''">AND u.user_name like concat('%', #{user_name}, '%')</if><if test="description != null and description != ''">AND u.description like concat('%', #{description}, '%')</if><if test="phoneNumber != null and phoneNumber != ''">AND u.phone_number like concat('%', #{phoneNumber}, '%')</if><if test="email != null and email != ''">AND u.email like concat('%', #{email}, '%')</if></select>
</mapper> 

3.2、除了分页插件之外还提供了哪些插件?

插件都是基于拦截器实现的,MyBatis-Plus提供了如下插件:

  • 自动分页: PaginationInnerInterceptor
  • 多租户: TenantLineInnerInterceptor
  • 动态表名: DynamicTableNameInnerInterceptor
  • 乐观锁: OptimisticLockerInnerInterceptor
  • sql 性能规范: IllegalSQLInnerInterceptor
  • 防止全表更新与删除: BlockAttackInnerInterceptor

4、示例源码

todo

相关文章:

SpringBoot第28讲:SpringBoot集成MySQL - MyBatis-Plus方式

SpringBoot第28讲&#xff1a;SpringBoot集成MySQL - MyBatis-Plus方式 本文是SpringBoot第28讲&#xff0c;MyBatis-Plus&#xff08;简称 MP&#xff09;是一个 MyBatis的增强工具&#xff0c;在 MyBatis 的基础上只做增强不做改变&#xff0c;为简化开发、提高效率而生。MyB…...

AI 绘画Stable Diffusion 研究(三)sd模型种类介绍及安装使用详解

本文使用工具&#xff0c;作者:秋葉aaaki 免责声明: 工具免费提供 无任何盈利目的 大家好&#xff0c;我是风雨无阻。 今天为大家带来的是 AI 绘画Stable Diffusion 研究&#xff08;三&#xff09;sd模型种类介绍及安装使用详解。 目前&#xff0c;AI 绘画Stable Diffusion的…...

Docker 命令没有提示信息

问题描述 提示&#xff1a;这里描述项目中遇到的问题&#xff1a; linux安装docker后发现使用docker命令没有提示功能&#xff0c;使用 Tab 键的时候只是提示已有的文件 解决方案&#xff1a; 提示&#xff1a;这里填写该问题的具体解决方案&#xff1a; Bash命令补全 Docke…...

springboot第33集:nacos图

./startup.sh -m standalone Nacos是一个内部微服务组件&#xff0c;需要在可信的内部网络中运行&#xff0c;不可暴露在公网环境&#xff0c;防止带来安全风险。Nacos提供简单的鉴权实现&#xff0c;为防止业务错用的弱鉴权体系&#xff0c;不是防止恶意攻击的强鉴权体系。 鉴…...

学习gRPC(一)

gRPC 简介 根据官网的介绍&#xff0c;gRPC 是开源高性能远程过程调用&#xff08;RPC&#xff09;框架&#xff0c;可以在任何环境中运行。它可以有效地连接数据中心内部和数据中心之间的服务&#xff0c;并为负载平衡、跟踪、运行状况检查和身份验证提供支持。同时由于其建立…...

【二进制安全】堆漏洞:Double Free原理

参考&#xff1a;https://www.anquanke.com/post/id/241598 次要参考&#xff1a;https://xz.aliyun.com/t/6342 malloc_chunk 的源码如下&#xff1a; struct malloc_chunk { INTERNAL_SIZE_T prev_size; /*前一个chunk的大小*/ INTERNAL_SIZE_T size; /*当前chunk的…...

python之open,打开文件时,遇到解码错误处理方式

在Python中&#xff0c;当我们打开一个文件时&#xff0c;我们可以指定文件的编码方式。如果文件的编码方式与我们指定的编码方式不同&#xff0c;那么就会出现解码错误。为了避免这种情况&#xff0c;我们可以使用errors参数来指定如何处理解码错误。 errors参数用于指定解码…...

STM32 CAN通信-CubeMX环境下CAN通信程序的编程与调试经验

文章目录 STM32 CAN通信-CubeMX环境下CAN通信程序的编程 STM32 CAN通信-CubeMX环境下CAN通信程序的编程 STM32F103ZE芯片 CAN通信测试代码&#xff1a; #include "main.h" #include "can.h"CAN_HandleTypeDef hcan1;void SystemClock_Config(void);int ma…...

windows创建不同大小的文件命令

打开命令窗口&#xff08;windowsR输入cmd打开&#xff09; 输入&#xff1a;fsutil file createnew C:\Users\Desktop\fileTran\10M.txt 10240000&#xff0c;创建10M大小的文件。 文件若存在需要先删除。...

Attention Is All You Need

Attention Is All You Need 摘要1. 简介2. Background3. 模型架构3.1 编码器和解码器堆栈3.2 Attention3.2.1 缩放的点积注意力&#xff08;Scaled Dot-Product Attention&#xff09;3.2.2 Multi-Head Attention3.2.3 Attention 在我们模型中的应用 3.3 Position-wise前馈网络…...

手写线程池 - C++版 - 笔记总结

1.线程池原理 创建一个线程&#xff0c;实现很方便。 缺点&#xff1a;若并发的线程数量很多&#xff0c;并且每个线程都是执行一个时间较短的任务就结束了。 由于频繁的创建线程和销毁线程需要时间&#xff0c;这样的频繁创建线程会大大降低 系统的效率。 2.思考 …...

PHP 容器化引发线上 502 错误状态码的修复

最后更新时间 2023-01-24. 背景 笔者所在公司技术栈为 Golang PHP&#xff0c;目前部分项目已经逐步转 Go 语言重构&#xff0c;部分 PHP 业务短时间无法用 Go 重写。 相比 Go 语言&#xff0c;互联网公司常见的 Nginx PHP-FPM 模式&#xff0c;经常会出现性能问题—— 特…...

QT中UDP之UDPsocket通讯

目录 UDP&#xff1a; 举例&#xff1a; 服务器端&#xff1a; 客户端&#xff1a; 使用示例&#xff1a; 错误例子并且改正&#xff1a; UDP&#xff1a; &#xff08;User Datagram Protocol即用户数据报协议&#xff09;是一个轻量级的&#xff0c;不可靠的&#xff0…...

【C语言】10-三大结构之循环结构-1

1. 引言 在日常生活中经常会遇到需要重复处理的问题,例如 统计全班 50 个同学平均成绩的程序求 30 个整数之和检查一个班级的同学程序是否及格要处理以上问题,最原始的方法是分别编写若干个相同或相似的语句或者程序段进行处理 例如:处理 50 个同学的平均成绩可以先计算一个…...

Windows下RocketMQ的启动

下载地址&#xff1a;下载 | RocketMQ 解压后 一、修改runbroker.cmd 修改 bin目录下的runbroker.cmd set "JAVA_OPT%JAVA_OPT% -server -Xms2g -Xmx2g" set "JAVA_OPT%JAVA_OPT% -XX:MaxDirectMemorySize15g" set "JAVA_OPT%JAVA_OPT% -cp %CLASSP…...

linux内核升级 docker+k8s更新显卡驱动

官方驱动 | NVIDIA在此链接下载对应的显卡驱动 # 卸载可能存在的旧版本nvidia驱动(如果没有安装过可跳过&#xff0c;建议执行) sudo apt-get remove --purge nvidia* # 安装驱动需要的依赖 sudo apt-get install dkms build-essential linux-headers-generic sudo vim /etc/mo…...

express学习笔记2 - 三大件概念

中间件 中间件是一个函数&#xff0c;在请求和响应周期中被顺序调用&#xff08;WARNING&#xff1a;提示&#xff1a;中间件需要在响应结束前被调用&#xff09; 路由 应用如何响应请求的一种规则 响应 / 路径的 get 请求&#xff1a; app.get(/, function(req, res) {res…...

Steam搬砖蓝海项目

这个项目早在很久之前就已经存在&#xff0c;并且一直非常稳定。如果你玩过一些游戏&#xff0c;你一定知道Steam是什么平台。Steam平台是全球最大的综合性数字发行平台之一&#xff0c;玩家可以在该平台购买、下载、讨论、上传和分享游戏和软件。 今天我给大家解释一下什么是…...

就业并想要长期发展选数字后端还是ic验证?

“就业并想要长期发展选数字后端还是ic验证&#xff1f;” 这是知乎上的一个热点问题&#xff0c;浏览量达到了13,183。看来有不少同学对这个问题感到疑惑。之前更新了数字后端&数字验证的诸多文章&#xff0c;从学习到职业发展&#xff0c;都写过&#xff0c;唯一没有做过…...

当服务器域名出现解析错误的问题该怎么办?

​  域名解析是互联网用户接收他们正在寻找的域的地址的过程。更准确地说&#xff0c;域名解析是人们在浏览器中输入时使用的域名与网站IP地址之间的转换过程。您需要站点的 IP 地址才能知道它所在的位置并加载它。但&#xff0c;在这个过程中&#xff0c;可能会出现多种因素…...

解锁AMD锐龙隐藏性能:SMUDebugTool深度调校实战指南

解锁AMD锐龙隐藏性能&#xff1a;SMUDebugTool深度调校实战指南 【免费下载链接】SMUDebugTool A dedicated tool to help write/read various parameters of Ryzen-based systems, such as manual overclock, SMU, PCI, CPUID, MSR and Power Table. 项目地址: https://gitc…...

【单片机】内核中断及NVICPending

红色框住的是M3内核中断&#xff0c;青色框住的默认打开&#xff0c;不可关闭中断&#xff08;除NMI外可屏蔽&#xff09;。包括SysTick在内无需NVIC_EnableIRQ&#xff0c;也无需在中断处理函数里清标志位。NVIC_SetPendingIRQ和NVIC_ClearPendingIRQ基本用不到&#xff0c;任…...

零基础快速入门前端DOM核心知识点详解与蓝桥杯Web赛道备考指南(可用于备赛蓝桥杯Web应用开发)

DOM&#xff08;文档对象模型&#xff09;是 HTML/XML 文档的编程接口&#xff0c;通过它可动态操作网页内容、结构与样式。本文将结合示例代码&#xff0c;系统讲解 DOM 核心知识点&#xff08;重点补充事件系统全解&#xff09;&#xff0c;并针对蓝桥杯 Web 应用开发赛道给出…...

ArduPilot电机控制逻辑与PWM输出机制剖析

1. ArduPilot电机控制基础概念 当你第一次接触无人机飞控时&#xff0c;最让人困惑的莫过于电机控制逻辑了。想象一下&#xff0c;你手里拿着遥控器&#xff0c;轻轻推动摇杆&#xff0c;无人机就能平稳地上升、下降或者转向。这背后到底发生了什么&#xff1f;让我用最直白的…...

从‘调不出来’到‘一次过流片’:折叠共源共栅放大器设计中那些没人告诉你的‘坑’与调试技巧

从‘调不出来’到‘一次过流片’&#xff1a;折叠共源共栅放大器设计中那些没人告诉你的‘坑’与调试技巧 在模拟电路设计的江湖里&#xff0c;折叠共源共栅&#xff08;Folded Cascode&#xff09;放大器就像一位身怀绝技却性格古怪的武林高手——性能强悍但极难驯服。许多工…...

基于MATLAB的平移线扫激光三维重建完整方案与代码实现

现整理了一套完整的&#xff0c;平移线扫重建 matlab代码和方案&#xff0c;包含相机标定、光平面标定与方案、移动装置标定与方案、激光线条中心线自适应提取、畸变矫正、三维重建、点云滤波等部分&#xff0c;代码按模块编写&#xff0c;注释完整&#xff0c;附带一份完整苹果…...

KMS_VL_ALL_AIO激活工具完全指南:从问题诊断到长效管理

KMS_VL_ALL_AIO激活工具完全指南&#xff1a;从问题诊断到长效管理 【免费下载链接】KMS_VL_ALL_AIO Smart Activation Script 项目地址: https://gitcode.com/gh_mirrors/km/KMS_VL_ALL_AIO 如何诊断Windows/Office激活失败的核心原因&#xff1f; 1.1 激活失败的三大…...

7大核心优势!Windows环境PM2服务化终极解决方案:从痛点到实战的完整指南

7大核心优势&#xff01;Windows环境PM2服务化终极解决方案&#xff1a;从痛点到实战的完整指南 【免费下载链接】pm2-installer Install PM2 offline as a service on Windows or Linux. Mostly designed for Windows. 项目地址: https://gitcode.com/gh_mirrors/pm/pm2-ins…...

语义分割竞赛必备:5种Loss函数组合效果对比(含Dice+Focal Loss调参指南)

语义分割竞赛进阶&#xff1a;5种损失函数组合实战评测与调参策略 在Kaggle等数据竞赛中&#xff0c;语义分割任务的性能提升往往取决于损失函数的巧妙选择与组合。不同于常规分类任务&#xff0c;多类别像素级预测需要处理极端类别不平衡、边界模糊等独特挑战。本文将深入剖析…...

如何用stressapptest进行高效内存和磁盘压力测试?实战案例分享

如何用stressapptest进行高效内存和磁盘压力测试&#xff1f;实战案例分享 在服务器运维和硬件性能评估中&#xff0c;内存和磁盘的稳定性直接关系到系统的可靠性。想象一下&#xff0c;当你的服务器在凌晨三点突然因为内存错误崩溃&#xff0c;或者磁盘在高峰期出现读写异常&a…...