文章目录
- 1. 前言
- 2. 与其它逆向工程工具相比的优势
- 3. 下载插件
- 4. 准备工作
- 5. 修改生成代码的配置
- 6. 选择需要逆向工程的表,点击 code generator 按钮
- 7. 逆向工程生成的代码的目录结构
- 8. 使用IDEA的在文件中批量替换文本功能更改Service接口的名称——结合正则表达式(可选)
1. 前言
在开发过程中,你是否厌倦了以下机械重复、枯燥乏味的工作
- 创建实体类
- 创建Controller
- 创建Service
- 创建ServiceImpl
- 创建Mapper
- 创建Mapper.xml
特别是在创建一个新项目的时候,我们需要根据表的结构大量地创建这些文件,创建这些文件没有什么技术含量,但是没有这些文件我们的项目就无法持续推进
没错,今天推荐给大家的MybatisPlus逆向工程插件正是为了解决这一痛点!!!
2. 与其它逆向工程工具相比的优势
- 当下也有很多逆向工程工具,要么就是需要编写及其复杂的配置文件,要么就是需要付费
- 比如Mybatis官方提供的逆向工程插件,不仅需要在Maven中导入逆向工程插件的坐标,还要编写一个及其复杂的配置文件,令人头晕眼花
- 本来用逆向工程就是为了不想做机械重复的工作,如今却要让我看一个晦涩难懂的配置文件,还要我手动编写配置,这不扯淡吗
今天推荐给大家的MybatisPlus逆向工程插件,不仅完全免费,而且十分简单易用(有图形化界面
)
使用该插件无需编写复杂的配置文件,只需配置数据库信息,动动鼠标,就能一键生成实体类、Controller、Service、ServiceImpl、Mapper、Mapper.xml
本次演示使用的IDEA版本为最新版(2023.2),MySQL版本为8.0.32,SpringBoot版本为3.0.2,JDK版本为17.0.7
3. 下载插件
在IEDA的插件市场搜索MybatisPlus
安装插件后可能需要重启IDEA
4. 准备工作
4.1 创建数据库和表(可跳过)
先创建一个名为element
的数据库,运行以下SQL文件
/*
Navicat Premium Data Transfer
Source Server : rootLink
Source Server Type : MySQL
Source Server Version : 80034 (8.0.34)
Source Host : localhost:3306
Source Schema : element
Target Server Type : MySQL
Target Server Version : 80034 (8.0.34)
File Encoding : 65001
Date: 18/06/2024 15:08:24
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for business
-- ----------------------------
DROP TABLE IF EXISTS `business`;
CREATE TABLE `business` (
`business_id` int NOT NULL AUTO_INCREMENT,
`business_address` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NULL DEFAULT NULL,
`business_explain` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NULL DEFAULT NULL,
`business_img` mediumtext CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NULL,
`business_name` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NOT NULL,
`delivery_price` decimal(5, 2) NULL DEFAULT NULL,
`order_type_id` int NOT NULL,
`remarks` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NULL DEFAULT NULL,
`start_price` decimal(5, 2) NULL DEFAULT NULL,
PRIMARY KEY (`business_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 22 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_danish_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Table structure for cart
-- ----------------------------
DROP TABLE IF EXISTS `cart`;
CREATE TABLE `cart` (
`cart_id` int NOT NULL AUTO_INCREMENT,
`food_id` int NOT NULL,
`business_id` int NOT NULL,
`user_id` int NOT NULL,
`quantity` int NOT NULL,
PRIMARY KEY (`cart_id`) USING BTREE,
INDEX `foodId`(`food_id` ASC) USING BTREE,
INDEX `businessId`(`business_id` ASC) USING BTREE,
INDEX `userId`(`user_id` ASC) USING BTREE,
CONSTRAINT `cart_ibfk_1` FOREIGN KEY (`food_id`) REFERENCES `food` (`food_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `cart_ibfk_2` FOREIGN KEY (`business_id`) REFERENCES `business` (`business_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `cart_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 25 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Table structure for delivery_address
-- ----------------------------
DROP TABLE IF EXISTS `delivery_address`;
CREATE TABLE `delivery_address` (
`delivery_address_id` int NOT NULL AUTO_INCREMENT,
`contact_sex` int NOT NULL,
`contact_tel` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NOT NULL,
`address` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NOT NULL,
`user_id` int NOT NULL,
`contact_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
PRIMARY KEY (`delivery_address_id`) USING BTREE,
INDEX `deliveryaddress_FK_user`(`user_id` ASC) USING BTREE,
CONSTRAINT `delivery_address_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_danish_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Table structure for food
-- ----------------------------
DROP TABLE IF EXISTS `food`;
CREATE TABLE `food` (
`food_id` int NOT NULL AUTO_INCREMENT,
`food_name` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NOT NULL,
`food_explain` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NOT NULL,
`food_img` mediumtext CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NULL,
`food_price` decimal(5, 2) NOT NULL,
`business_id` int NOT NULL,
`remarks` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NULL DEFAULT NULL,
PRIMARY KEY (`food_id`) USING BTREE,
INDEX `food_FK_business`(`business_id` ASC) USING BTREE,
CONSTRAINT `food_ibfk_1` FOREIGN KEY (`business_id`) REFERENCES `business` (`business_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 40 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_danish_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Table structure for order_detail
-- ----------------------------
DROP TABLE IF EXISTS `order_detail`;
CREATE TABLE `order_detail` (
`order_detail_id` int NOT NULL AUTO_INCREMENT,
`order_id` int NULL DEFAULT NULL,
`food_id` int NULL DEFAULT NULL,
`quantity` int NULL DEFAULT NULL,
PRIMARY KEY (`order_detail_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 20 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Table structure for orders
-- ----------------------------
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`order_id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`business_id` int NOT NULL,
`order_date` datetime NOT NULL,
`order_total` decimal(7, 2) NOT NULL,
`delivery_address_id` int NOT NULL,
`order_state` int NOT NULL,
PRIMARY KEY (`order_id`) USING BTREE,
INDEX `orders_FK_user`(`user_id` ASC) USING BTREE,
INDEX `orders_FK_business`(`business_id` ASC) USING BTREE,
INDEX `orders_FK_deliveryaddress`(`delivery_address_id` ASC) USING BTREE,
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`business_id`) REFERENCES `business` (`business_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `orders_ibfk_3` FOREIGN KEY (`delivery_address_id`) REFERENCES `delivery_address` (`delivery_address_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 21 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_danish_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`role_id` int NOT NULL AUTO_INCREMENT,
`role_name` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`role_id`) USING BTREE,
INDEX `roleId`(`role_id` ASC, `role_name` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Table structure for role_user
-- ----------------------------
DROP TABLE IF EXISTS `role_user`;
CREATE TABLE `role_user` (
`role_user_id` int NOT NULL AUTO_INCREMENT,
`user_id` int NULL DEFAULT NULL,
`role_id` int NULL DEFAULT NULL,
PRIMARY KEY (`role_user_id`) USING BTREE,
INDEX `userId`(`user_id` ASC) USING BTREE,
INDEX `roleId`(`role_id` ASC) USING BTREE,
CONSTRAINT `role_user_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `role_user_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `role` (`role_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 142 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`user_id` int NOT NULL AUTO_INCREMENT,
`password` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`username` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NOT NULL,
`user_sex` int NOT NULL DEFAULT 1,
`user_avatar` mediumtext CHARACTER SET utf8mb3 COLLATE utf8mb3_danish_ci NULL,
`deleted` int(1) UNSIGNED ZEROFILL NULL DEFAULT 0,
PRIMARY KEY (`user_id`) USING BTREE,
UNIQUE INDEX `userName`(`username` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2917 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_danish_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Table structure for user_base_info
-- ----------------------------
DROP TABLE IF EXISTS `user_base_info`;
CREATE TABLE `user_base_info` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户ID',
`name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户名',
`chinese_name` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '中文名',
`sex` char(3) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '性别',
`alias` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '别名',
`wechat` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '微信号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
4.2 配置数据库信息
在IDEA的菜单栏中找到工具->Config Database
4.2.1 打开IDEA的菜单栏
4.2.2 找到工具,点击Config Database
4.2.3 填写连接数据库所需要的信息
jdbc:mysql://localhost:3306/element?useSSL=false&serverTimezone=UTC
填写完连接数据库所需要的信息后,点击test connect
按钮
显示test successful!
弹窗就表示连接数据库成功了
4.3 导入MybatisPlus的Maven依赖和Spring Web的Maven依赖(SpringBoot项目)
在项目的pom.xml文件中引入MybatisPlus的依赖
SpringBoot 2
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
SpringBoot 3
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
MybatisPlus的官网:安装 | MyBatis-Plus (baomidou.com)
在项目的pom.xml文件中引入Spring Web的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
5. 修改生成代码的配置
5.1 在菜单栏的工具项找到Code Generator
5.2 修改生成代码的配置
根据需求修改配置
尽量不要勾选over file(如果目录下有同名文件,会覆盖掉原文件)
6. 选择需要逆向工程的表,点击 code generator 按钮
可以使用SHIFT
键和CTRL
键配合鼠标左键实现多选,被选中的表会变成粉色
显示Generator successful
窗口就代表生成成功了
7. 逆向工程生成的代码的目录结构
生成代码的目录符合经典的三层MVC架构的(Mapper.xml
文件会放在resources/mapper
目录下)
8. 使用IDEA的在文件中批量替换文本功能更改Service接口的名称——结合正则表达式(可选)
MybatisPlus逆向工程插件生成的Service接口前面有一个I,可能不符合我们的使用习惯,我们可以使用IDEA的在文件中批量批量替换文本
功能更改Service接口的名称
右击service目录,点击在文件中替换
以下是两个关键的正则表达式
(I)([a-zA-Z0-9_]{1,}Service)
$2
用于搜索的正则表达式的解释:
(I)
:I
表示匹配字母"I"- 圆括号
()
表示这是一个捕获组,可以将匹配这部分的内容单独提取出来。在这个表达式中,第一个捕获组匹配的是字母"I"
([a-zA-Z0-9_]{1,}Service)
:[a-zA-Z0-9_]
表示匹配任意字母(大写或小写)、数字或下划线{1,}
表示前面的字符类(字母、数字、下划线)至少出现一次Service
表示匹配单词"Service"- 整条式子也是一个捕获组,它匹配的是"I"后面的至少一个字母、数字或下划线,以及"Service"这个单词
解释一下替换文本($2)的意思,用于搜索的正则表达式有2个小括号括起来的表达式,每一个都代表着一组匹配结果集,第一个匹配结果集对应$1,第二个匹配结果集对应$2,第三个匹配结果集对应$3,以此类推
填写完两个正则表达式后,就可以实时看到替换的预览效果
点击替换按钮,就能将Service接口前面的I去掉了
唯一不足的是,Service接口类的文件名还是需要手动修改,可以结合IDEA的智能提示,提高修改文件名的效率
标签:utf8mb3,Mapper,插件,SET,配置文件,ci,user,NULL,id From: https://blog.csdn.net/m0_62128476/article/details/139781301