springboot-如何快速集成mybatisplus
wptr33 2024-12-15 17:12 25 浏览
一、步骤概览
二、步骤说明
1.引入依赖
在 pom.xml 文件中引入 mybatis-plus 依赖包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.5</version>
</dependency>
2.定义配置项
在application.yml 配置文件中定义 mybatis-plus 配置项
# MyBatis Plus配置
mybatis-plus:
# 搜索指定包别名
typeAliasesPackage: com.shawn.**.model
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath*:mapper/**/*Mapper.xml
# 加载全局的配置文件
configLocation: classpath:mybatis/mybatis-config.xml
① typeAliasesPackage
指定包的别名,用于将包下的实体类注册为 MyBatis 的别名。在这个例子中,com.shawn.**.model 表示将 com.shawn 包及其子包下的所有 model 类都注册为别名。
② mapperLocations
配置 Mapper 的扫描路径,用于指定 MyBatis Plus 找到所有的 Mapper.xml 文件。classpath*:mapper/**/*Mapper.xml 表示在 classpath 下的 mapper 目录及其子目录中查找以 Mapper.xml 结尾的文件。
③ mapperLocations
加载全局的配置文件。classpath:mybatis/mybatis-config.xml 表示在 classpath 下的 mybatis 目录中查找 mybatis-config.xml 文件。mybatis-config.xml 详情如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 全局参数 -->
<settings>
<!-- 使全局的映射器启用或禁用缓存 -->
<setting name="cacheEnabled" value="true" />
<!-- 允许JDBC 支持自动生成主键 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 配置默认的执行器.SIMPLE就是普通执行器;REUSE执行器会重用预处理语句(prepared statements);BATCH执行器将重用语句并执行批量更新 -->
<setting name="defaultExecutorType" value="SIMPLE" />
<!-- 指定 MyBatis 所用日志的具体实现 -->
<setting name="logImpl" value="SLF4J" />
<!-- 使用驼峰命名法转换字段 -->
<!-- <setting name="mapUnderscoreToCamelCase" value="true"/> -->
</settings>
</configuration>
3.配置 mybatis-plus
定义MyBatis Plus 的配置类,用于配置 MyBatis Plus 的插件和拦截器。代码概览如图所示:
- MybatisPlusConfig#mybatisPlusInterceptor:创建 MybatisPlusInterceptor 对象,并添加多个内部拦截器。
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 分页插件
interceptor.addInnerInterceptor(paginationInnerInterceptor());
// 乐观锁插件
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
// 阻断插件
interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
return interceptor;
}
- MybatisPlusConfig#paginationInnerInterceptor:创建 PaginationInnerInterceptor 对象,用于分页查询。可以根据数据库类型设置不同的分页方言,默认设置为 MySQL
public PaginationInnerInterceptor paginationInnerInterceptor() {
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 设置数据库类型为mysql
paginationInnerInterceptor.setDbType(DbType.MYSQL);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInnerInterceptor.setMaxLimit(-1L);
return paginationInnerInterceptor;
}
- MybatisPlusConfig#optimisticLockerInnerInterceptor:创建 OptimisticLockerInnerInterceptor 对象,用于实现乐观锁功能。
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
return new OptimisticLockerInnerInterceptor();
}
- MybatisPlusConfig#blockAttackInnerInterceptor:创建BlockAttackInnerInterceptor 对象,用于阻止对全表的删除或更新操作。
public BlockAttackInnerInterceptor blockAttackInnerInterceptor() {
return new BlockAttackInnerInterceptor();
}
4.设置扫描路径
在 springboot 启动类上添加 @MapperScan ,这样就可以自动扫描指定的 Mapper 接口,并生成相应的 Mapper Bean。设置示例如图所示:
5.封装分页参数
①. 封装请求
客户端发起分页查询请求,如果分页请求肯定包含页码和分页大小。我们单独封装基类,用于接收分页请求参数,接收请求参数的类(DTO) 后续可以直接集成分页请求基类。
- BasePageReq:分页请求基类
@Data
public class BasePageReq {
private Long pageNum;
private Long pageSize;
}
②. 封装返回
响应给客户端的数据,需要包含数据列表和分页详情,我们将其封装成统一的数据格式,便于客户端解析。
- BasePageRes:分页返回信息
@Data
@AllArgsConstructor
public class BasePageRes<T> {
private int total;
private int size;
private int pages;
private int current;
private List<T> records;
public BasePageRes(List<T> records, int total, int size, int current) {
this.records = getCurrentList(records, size, current);
this.total = total;
this.size = size;
this.current = current;
this.pages = (int) Math.ceil((double) total / size);
}
public List<T> getCurrentList(List<T> records, int size, int current) {
int toIndex = current * size > records.size() ? records.size() : current * size;
return records.subList((current - 1) * size, toIndex);
}
public BasePageRes(IPage<T> page) {
this.records = page.getRecords();
this.total = (int) page.getTotal();
this.size = (int) page.getSize();
this.current = (int) page.getCurrent();
this.pages = (int) page.getPages();
}
public static <E> BasePageRes<E> newInstance(IPage<E> page) {
return new BasePageRes(page);
}
}
三、代码测试
1.测试代码
①. mapper
②. service
③. controller
④. dto
2.测试结果
相关推荐
- 「网络安全」JAVA代码审计——XXE外部实体注入
-
一、WEB安全部分想要了解XXE,在那之前需要了解XML的相关基础二、XML基础...
- Web前端面试题目及答案汇总(web前端面试题最新)
-
Web前端面试题目及答案汇总来源:极客头条以下是收集一些面试中经常会遇到的经典面试题以及自己面试过程中无法解决的问题,通过对知识的整理以及经验的总结,重新巩固自身的前端基础知识,如有错误或更好的答案,...
- 什么是脚本文件?与可执行文件有什么不同?
-
今天的内容是脚本文件和可执行文件是两种不同类型的计算机文件,它们在结构和执行方式上有显著区别。脚本文件:定义与特性...
- 20个实用Python运维脚本(收藏级)(python 运维工具)
-
系统环境:支持Linux(Ubuntu/CentOS/Debian)和Windows...
- 2026年前每个开发者都应该学习的技能
-
优秀开发者...
- Linux 如何每 5、10、15 或 30 分钟运行一次 Cron 作业?
-
在Linux系统中,Cron是一个强大的工具,用于自动化重复性任务。通过合理配置...
- Shell脚本编程进阶实战:从入门到高效自动化
-
Shell脚本编程进阶实战:从入门到高效自动化一、参数处理进阶:打造专业级CLI工具1.高级参数解析示例...
- 在Bash中按分隔符拆分字符串的方法
-
技术背景在Bash脚本编程中,经常会遇到需要按特定分隔符拆分字符串的需求,例如处理CSV文件、解析日志等。掌握字符串拆分的方法对于数据处理和脚本自动化非常重要。...
- 程序员用5分钟,把一个400多MB的苹果安装包削掉了187MB
-
丰色发自凹非寺量子位|公众号QbitAI前些日子,一个...
- 如何在 Windows 上编写批处理脚本
-
你知道如何使用命令提示符吗?如果这样做,您可以编写一个批处理文件。在最简单的形式中,批处理文件(或批处理脚本)是双击文件时执行的几个命令的列表。批处理文件一直回到DOS,但仍然适用于现代版本的Win...
- 一文搞懂shell脚本(shell脚本应用实战)
-
一文搞懂shell脚本1、shell脚本介绍什么是shell脚本...
- 一文讲清ShellScript脚本编程知识
-
摘要:本文详尽地讲述了ShellScript的基础内容,还有它在Linux系统里的运用情况,涵盖了它的基本语法、常用的命令以及高级的功能。ShellScript可是一种简单又非常实用的编...
- 在Bash脚本中获取自身所在目录的方法
-
技术背景在使用Bash脚本时,有时需要获取脚本自身所在的目录。比如,当脚本作为另一个应用程序的启动器时,需要将工作目录更改为脚本所在的目录,以便对该目录中的文件进行操作。然而,由于脚本的调用方式多样(...
- shell中如何确定脚本的位置?这篇文章告诉你
-
我想从同一个位置读取一些配置文件,如何确定脚本的位置?。这个问题的出现主要是由两个原因引发的:一是您希望将脚本的数据或配置进行外部化,因此需要一种方式来寻找这些外部资源;二是您的脚本需要对某些捆绑资源...
- bash shell 语法(bash命令用法)
-
下面是**Shell(Bash)语法的常用知识点总结**,适合初学者和日常脚本编写参考。内容涵盖变量、判断、循环、函数、重定向、正则、数组等常见用法。---#Shell(Bash)语法速查总结...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)