【Spring Boot】035-Spring Boot 整合 MyBatis Plus
【Spring Boot】010-Spring Boot整合Mybatis
https://blog.csdn.net/qq_29689343/article/details/108621835
文章目录 【Spring Boot】035-Spring Boot 整合 MyBatis Plus一、MyBatis Plus 概述1、简介2、特性3、结构图4、相关资料 二、Spring Boot 整合 MyBatis Plus1、引入依赖核心依赖完整 pom.xml 文件 2、数据源配置3、演示数据建表语句示例数据 4、基础代码和配置实体类Mapper 接口StudentMapper.xml启动类上配置 mapper 扫描路径测试类运行结果代码结构截图 三、MyBatis Plus 核心特性使用1、CRUD基础接口insert 型接口delete 型接口update 型接口select 型接口 2、Wrapper 机制基本使用lambda 式使用投影查询聚合查询 3、主键策略与ID生成器4、逻辑删除application.yaml 中配置在实体类的 deleted 属性上标记 @TableLogic 注解数据表中加上 deleted 字段删除测试逻辑删除结果 5、乐观锁插件第一步:注册乐观锁插件第二步:实体类添加 `@Version` 注解 6、分页插件第一步:添加分页插件第二步:使用分页模型
一、MyBatis Plus 概述 1、简介
MyBatis Plus 为简化开发而生!
MyBatis Plus(简称 MyBatis-Plus 或 MP)是 MyBatis 的增强工具包,它在 MyBatis 的基础上提供了很多便捷的功能,简化了开发过程。
只做增强,不做改变、效率至上,功能丰富。
2、特性CRUD 操作的增强支持: MyBatis Plus 提供了更简单、更便捷的方式进行 CRUD 操作,减少了开发者的工作量。条件构造器: MyBatis Plus 引入了条件构造器,可以通过简单的链式调用来构建复杂的查询条件,避免了手写 SQL 语句的繁琐。代码生成器: MyBatis Plus 提供了代码生成器,可以根据数据库表自动生成对应的实体类、Mapper 接口以及 XML 文件,提高了开发效率。分页插件: MyBatis Plus 集成了分页插件,可以方便地进行分页查询,支持多种数据库的分页方式。性能优化: MyBatis Plus 对 MyBatis 进行了一些性能上的优化,提高了系统的运行效率。通用 Mapper: MyBatis Plus 提供了通用 Mapper 的功能,可以通过继承 BaseMapper 接口来实现通用的 CRUD 操作,减少了编码量。自动填充: MyBatis Plus 支持自动填充功能,可以在插入和更新操作时自动填充指定的字段,如创建时间、更新时间等。 3、结构图
4、相关资料
官网:https://baomidou.com/
github:https://github.com/baomidou/mybatis-plus
二、Spring Boot 整合 MyBatis Plus 1、引入依赖 核心依赖
com.mysql mysql-connector-j runtime
com.baomidou mybatis-plus-boot-starter 3.5.3.2 完整 pom.xml 文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.17
com.zibo study-mp 0.0.1-SNAPSHOT study-mp study-mp 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test
com.mysql mysql-connector-j runtime
com.baomidou mybatis-plus-boot-starter 3.5.3.2
org.projectlombok lombok true org.springframework.boot spring-boot-maven-plugin paketobuildpacks/builder-jammy-base:latest org.projectlombok lombok 2、数据源配置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: 123456 3、演示数据 建表语句 CREATE TABLE `student` ( `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名字', `age` int unsigned NOT NULL COMMENT '年龄', PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='学生'; 示例数据 INSERT INTO `study`.`student`(`id`, `name`, `age`) VALUES (1, '訾博', 27); INSERT INTO `study`.`student`(`id`, `name`, `age`) VALUES (2, 'zibo', 25); INSERT INTO `study`.`student`(`id`, `name`, `age`) VALUES (3, 'zb', 23); 4、基础代码和配置 实体类
MyBatisPlus 拥有非常优秀的单表 CRUD 基础能力,而这个能力需要在实体类上做一些改动。通过标注 @TableName 注解,相当于告诉 MyBatisPlus 当前这个 Student 类要映射到 student 表(默认表名策略是驼峰转下划线);通过给 id 属性标注 @TableId 注解,并声明 ID 类型为 auto ,相当于适配 MySQL 中的自增主键。其他属性与数据库中映射均一致,就不再需要添加新注解了。
package com.zibo.studymp.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("student") public class Student { @TableId(type = IdType.AUTO) private Integer id; private String name; private Integer age; } Mapper 接口
MyBatisPlus 的单表 CRUD 能力来自一个内置的基础接口 BaseMapper ,通过继承 BaseMapper 并注明实体类的泛型类型,即可拥有单表的 CRUD 能力。
package com.zibo.studymp.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.zibo.studymp.entity.Student; public interface StudentMapper extends BaseMapper { Student getByName(String name); } StudentMapper.xml
启动类上配置 mapper 扫描路径
@MapperScan({“com.zibo.**.mapper”})
package com.zibo.studymp; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan({"com.zibo.**.mapper"}) public class StudyMpApplication { public static void main(String[] args) { SpringApplication.run(StudyMpApplication.class, args); } } 测试类 package com.zibo.studymp; import com.zibo.studymp.entity.Student; import com.zibo.studymp.mapper.StudentMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class StudyMpApplicationTests { @Autowired private StudentMapper studentMapper; @Test void contextLoads() { Student student = studentMapper.selectById(1); System.out.println(student); System.out.println("=================================="); student = studentMapper.getByName("zibo"); System.out.println(student); } } 运行结果 Student(id=1, name=訾博, age=27) ================================== Student(id=2, name=zibo, age=25) 代码结构截图
三、MyBatis Plus 核心特性使用 1、CRUD基础接口
MyBatisPlus 提供的重要基础能力,就是替我们开发者实现了基本的单表 CRUD 操作,我们在编写具体的业务模块时,单表的 CRUD 可以完全不需要编写了,仅需要继承 BaseMapper 接口,该 Mapper 接口就可以自动拥有单表 CRUD 的能力。
insert 型接口 // 插入一条记录 int insert(T entity); delete 型接口 // 根据 entity 条件,删除记录 int delete(@Param(Constants.WRAPPER) Wrapper wrapper); // 删除(根据ID 批量删除) int deleteBatchIds(@Param(Constants.COLLECTION) Collection
idList); // 根据 ID 删除 int deleteById(Serializable id); // 根据 columnMap 条件,删除记录 int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap); update 型接口 // 根据 whereWrapper 条件,更新记录 int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper whereWrapper); // 根据 ID 修改 int updateById(@Param(Constants.ENTITY) T entity); select 型接口 // 根据 ID 查询 T selectById(Serializable id); // 根据 entity 条件,查询一条记录 T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 查询(根据ID 批量查询) List selectBatchIds(@Param(Constants.COLLECTION) Collection
idList); // 根据 entity 条件,查询全部记录 List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 查询(根据 columnMap 条件) List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap); // 根据 Wrapper 条件,查询全部记录 List> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值 List