SpringBoot整合MybatisPlus基本的增删改查,保姆级教程

MybatisPlus是国产的第三方插件,它封装了许多常用的CURDapi,免去了我们写mApper.xml的重复劳动,这里介绍了基本的整合SpringBoot和基础用法 。
2|0引入依赖在项目中pom文件引入mybatisplus和MySQL驱动依赖,如下图
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-JAVA</artifactId><version>8.0.26</version></dependency>3|0配置连接信息在项目中application.yaml文件中配置数据库的连接信息
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/didiadmin?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=trueusername: rootpassword: 1234564|0新建两个表本案例通过一个完整的业务流程来介绍如何使用mybatispuls,首先在数据库中新建两个表,一个是字典类型表sys_dict_type和一个字典数据表sys_dict_data
CREATE TABLE `sys_dict_type` (`id` CHAR (19) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '标识',`type_name` VARCHAR (255) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典类型名称', `type_code` VARCHAR (255) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典类型标识', `description` VARCHAR (255) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典类型描述', `enable` CHAR (1) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '是否启用', `create_by` CHAR (19) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人ID',`create_name` CHAR (19) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人名称', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_by` CHAR (19) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人ID', `update_name` CHAR (19) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人名称', `update_time` datetime DEFAULT NULL COMMENT '修改时间', `remark` VARCHAR (255) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`) USING BTREE) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMICCREATE TABLE `sys_dict_data` (`data_id` CHAR (19) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '标识',`data_label` CHAR (19) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典标签', `data_value` CHAR (20) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典值', `type_code` CHAR (20) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '所属类型', `is_default` CHAR (1) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '是否默认', `update_by` CHAR (19) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人ID',`update_name` CHAR (19) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人名称', `update_time` datetime DEFAULT NULL COMMENT '修改时间', `create_by` VARCHAR (255) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人ID',`create_name` CHAR (19) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人名称', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `remark` VARCHAR (255) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注', `enable` CHAR (1) CHARACTERSET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '是否启用', PRIMARY KEY (`data_id`) USING BTREE) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC5|0在项目中创建相应的实体类通过观察SQL语句发现两个表中有很多相同的字段,所有我们把相同的字段抽离出来,放到一个基础的实体类中,其他实体类通过集成方式获取公共的字段 。
5|1创建基础实体类package com.didiplus.common.base;import com.baomidou.mybatisplus.annotation.FieldFill;import com.baomidou.mybatisplus.annotation.TableField;import lombok.Data;import java.io.Serializable;import java.time.LocalDateTime;/*** Author: didiplus* Email: 972479352@qq.com* CreateTime: 2022/4/29* Desc: 基 础 实 体 类*/@Datapublic class BaseDomain implements Serializable {/*** 创建时间*/@TableField(value = https://www.isolves.com/it/cxkf/jiagou/2022-05-05/"create_time", fill = FieldFill.INSERT)private LocalDateTime createTime;/*** 创建人*/@TableField(value = "create_by", fill = FieldFill.INSERT)private String createBy;/*** 创建人名称*/@TableField(value = "create_name", fill = FieldFill.INSERT)private String createName;/*** 修改时间*/@TableField(value = "update_time", fill = FieldFill.UPDATE)private LocalDateTime updateTime;/*** 修改人*/@TableField(value = "update_by", fill = FieldFill.UPDATE)private String updateBy;/*** 修改人名称*/@TableField(value = "update_name", fill = FieldFill.UPDATE)private String updateName;/*** 备注*/private String remark;}


推荐阅读