文章目录
- 1. 基础数据准备
- 2. 整合MybatisPlus
- 3. MybatisPlus 配置
- 3.1 数据源配置
- 3.2 mybatis-plus 分页插件配置
- 3.3 mybatis-plus 自动填充
- 3.4 代码生成器
1. 基础数据准备
直接拿前辈做的表结构使用。
/*
Navicat Premium Data Transfer
Source Server : localhost_mysql
Source Server Type : MySQL
Source Server Version : 80200
Source Host : localhost:3306
Source Schema : admin_system
Target Server Type : MySQL
Target Server Version : 80200
File Encoding : 65001
Date: 24/07/2024 21:14:02
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for sys_menu
-- ----------------------------
DROP TABLE IF EXISTS `sys_menu`;
CREATE TABLE `sys_menu` (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '主键',
`parent_id` bigint(0) NOT NULL DEFAULT 0 COMMENT '父id',
`menu_name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '菜单名',
`sort` int(0) NULL DEFAULT 0 COMMENT '排序',
`menu_type` int(0) NULL DEFAULT NULL COMMENT '类型:0,目录,1菜单,2:按钮',
`path` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '路由路径 如:umsUser',
`component_path` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '组件路径 如:ums/user/index',
`perms` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '权限标识',
`icon` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '图标',
`deleted` int(0) NULL DEFAULT NULL COMMENT '是否删除 删除(1) 未删除(0)',
`status` int(0) NULL DEFAULT NULL COMMENT '状态:0:可用,1:不可用',
`creator` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '创建者',
`updater` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '更新者',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 124 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '系统菜单表' ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_menu
-- ----------------------------
INSERT INTO `sys_menu` VALUES (1, 0, '首页', 0, 1, 'index', 'index', 'index', 'home', 0, 0, 'admin', 'admin', '2023-12-11 18:57:19', '2023-12-11 18:57:21');
INSERT INTO `sys_menu` VALUES (2, 0, '系统管理', 7, 0, 'system', '', 'system', 'system', 0, 0, 'admin', 'admin', '2023-12-11 18:58:23', '2023-12-11 18:58:25');
INSERT INTO `sys_menu` VALUES (3, 0, '系统工具', 8, 0, 'tools', 'tools', 'tools', 'tool', 0, 0, 'admin', 'admin', '2023-12-11 18:58:53', '2023-12-11 18:58:55');
INSERT INTO `sys_menu` VALUES (4, 0, '支付管理', 3, 0, 'pay', 'pay', 'pay', '', 1, 0, 'admin', 'admin', '2023-12-11 18:59:30', '2023-12-11 18:59:32');
INSERT INTO `sys_menu` VALUES (5, 2, '用户管理', 0, 1, 'sysUser', 'system/user/index', 'system:user:list', '', 0, 0, 'admin', 'admin', '2023-12-11 19:01:16', '2023-12-11 19:01:18');
INSERT INTO `sys_menu` VALUES (6, 2, '角色管理', 1, 1, 'Role', 'system/role/index', 'system:role:list', '', 0, 0, 'admin', 'admin', '2023-12-11 19:02:05', '2023-12-11 19:02:08');
INSERT INTO `sys_menu` VALUES (7, 2, '菜单管理', 2, 1, 'sysMenu', 'system/menu/index', 'system:menu:list', '', 0, 0, 'admin', 'admin', '2023-12-11 19:02:52', '2023-12-11 19:02:55');
INSERT INTO `sys_menu` VALUES (8, 3, '代码生成', 7, 1, 'toolsGen', 'generator/index', 'tools:gen:list', '', 0, 0, 'admin', 'admin', '2023-12-11 19:03:57', '2023-12-11 19:04:00');
INSERT INTO `sys_menu` VALUES (120, 2, '系统日志', 4, 1, 'syslog', 'system/syslog/index', NULL, '', 0, 0, 'admin', 'admin', '2024-07-19 18:31:40', '2024-07-22 10:17:16');
-- ----------------------------
-- Table structure for sys_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
`role_id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '角色id',
`role_label` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '角色标识',
`role_name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '角色名字',
`sort` int(0) NULL DEFAULT NULL COMMENT '排序',
`status` int(0) NULL DEFAULT NULL COMMENT '状态:0:可用,1:不可用',
`deleted` int(0) NULL DEFAULT NULL COMMENT '是否删除:0: 未删除,1:已删除',
`remark` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '备注',
`creator` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '创建者',
`updater` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '更新者',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`role_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '系统角色表' ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_role
-- ----------------------------
INSERT INTO `sys_role` VALUES (1, 'admin', '系统管理员', 0, 0, 0, NULL, 'admin', 'admin', '2024-07-24 11:31:23', '2024-07-24 11:31:23');
INSERT INTO `sys_role` VALUES (2, 'developer', '开发者', 1, 0, 0, NULL, 'admin', 'admin', '2024-07-24 11:31:23', '2024-07-24 11:31:23');
INSERT INTO `sys_role` VALUES (3, 'test', '测试人员', 2, 0, 0, NULL, 'admin', 'admin', '2024-02-05 18:00:00', '2024-02-05 18:00:00');
INSERT INTO `sys_role` VALUES (4, '11', '11', 3, 0, 1, '111', 'admin', 'admin', '2024-06-25 14:36:48', NULL);
INSERT INTO `sys_role` VALUES (6, '112233', '112233', 3, 0, 1, '1112233', 'admin', 'admin', '2024-06-25 14:51:15', '2024-06-25 15:28:36');
INSERT INTO `sys_role` VALUES (7, '33', '22', 4, 0, 1, '333', 'admin', 'admin', '2024-06-25 15:41:49', NULL);
INSERT INTO `sys_role` VALUES (8, 'fafda', 'fasfa', NULL, 0, 0, 'fdf', 'admin', 'admin', '2024-06-25 20:57:32', '2024-06-26 00:10:05');
-- ----------------------------
-- Table structure for sys_role_menu
-- ----------------------------
DROP TABLE IF EXISTS `sys_role_menu`;
CREATE TABLE `sys_role_menu` (
`id` bigint(0) NOT NULL AUTO_INCREMENT,
`role_id` bigint(0) NOT NULL,
`menu_id` bigint(0) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 617 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '角色菜单表' ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_role_menu
-- ----------------------------
INSERT INTO `sys_role_menu` VALUES (68, 2, 1);
INSERT INTO `sys_role_menu` VALUES (69, 2, 5);
INSERT INTO `sys_role_menu` VALUES (70, 2, 6);
INSERT INTO `sys_role_menu` VALUES (71, 2, 7);
INSERT INTO `sys_role_menu` VALUES (72, 2, 8);
INSERT INTO `sys_role_menu` VALUES (288, 3, 1);
INSERT INTO `sys_role_menu` VALUES (289, 3, 2);
INSERT INTO `sys_role_menu` VALUES (290, 3, 5);
INSERT INTO `sys_role_menu` VALUES (291, 3, 6);
INSERT INTO `sys_role_menu` VALUES (292, 3, 7);
INSERT INTO `sys_role_menu` VALUES (606, 1, 1);
INSERT INTO `sys_role_menu` VALUES (607, 1, 2);
INSERT INTO `sys_role_menu` VALUES (608, 1, 5);
INSERT INTO `sys_role_menu` VALUES (609, 1, 6);
INSERT INTO `sys_role_menu` VALUES (610, 1, 7);
INSERT INTO `sys_role_menu` VALUES (611, 1, 120);
INSERT INTO `sys_role_menu` VALUES (612, 1, 3);
INSERT INTO `sys_role_menu` VALUES (613, 1, 8);
INSERT INTO `sys_role_menu` VALUES (614, 1, 121);
INSERT INTO `sys_role_menu` VALUES (615, 1, 122);
INSERT INTO `sys_role_menu` VALUES (616, 1, 123);
-- ----------------------------
-- Table structure for sys_user
-- ----------------------------
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`username` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '用户账号',
`nickname` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '用户昵称',
`email` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT '' COMMENT '用户邮箱',
`mobile` varchar(11) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT '' COMMENT '手机号码',
`sex` int(0) NULL DEFAULT 0 COMMENT '用户性别(0男 1女 2未知)',
`avatar` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT '' COMMENT '头像地址',
`password` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT '' COMMENT '密码',
`status` int(0) NULL DEFAULT 0 COMMENT '帐号状态(0正常 1停用)',
`creator` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '创建者',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`updater` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '更新者',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '备注',
`deleted` tinyint(0) NULL DEFAULT 0 COMMENT '逻辑删除, 删除(1) 未删除(0)',
`name` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '用户姓名(实名)',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '系统用户表' ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES (1, 'admin', 'admin', 'cc11@163.com', '123456789', 0, 'avatarurl', '$2a$10$wYbVIP3JbCj8TZcl8JIRku0iC.1wYZ0MeZxp/fU/eFHgldgXIcR3.', 0, 'admin', '2024-12-07 22:29:21', 'admin', '2024-03-07 22:29:21', '备注', 0, NULL);
INSERT INTO `sys_user` VALUES (2, 'zs', '张三', 'zhangsan@123.com', '16632156458', 0, '', '$2a$10$wYbVIP3JbCj8TZcl8JIRku0iC.1wYZ0MeZxp/fU/eFHgldgXIcR3.', 0, 'admin', '2024-03-11 19:06:41', 'admin', '2024-12-11 19:06:46', NULL, 0, NULL);
INSERT INTO `sys_user` VALUES (3, '123', '123', 'cc123@123.com', '1663215645', 0, 'imgurl', '$2a$10$nuDPT0ZJFFgAE3w2iFzjHu3DC/STjaRM1D8LOsoJEEeaLKLCDB1gi', 0, 'admin', '2024-12-11 19:06:41', 'admin', '2024-03-11 19:06:46', NULL, 0, NULL);
INSERT INTO `sys_user` VALUES (4, '11', '11', '11@11.com', '13112341234', 1, '', '$2a$10$IVyirW7r9LVUc72vJUIqEuoFmJ5y1U3Nh9x45y5r3jh2wtUSfxmfm', 0, 'admin', '2024-06-24 18:38:58', 'admin', NULL, '11', 1, '11');
-- ----------------------------
-- Table structure for sys_user_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_user_role`;
CREATE TABLE `sys_user_role` (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`user_id` bigint(0) NOT NULL COMMENT '用户id',
`role_id` bigint(0) NOT NULL COMMENT '角色id',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '用户角色表' ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_user_role
-- ----------------------------
INSERT INTO `sys_user_role` VALUES (1, 1, 1);
INSERT INTO `sys_user_role` VALUES (2, 2, 2);
INSERT INTO `sys_user_role` VALUES (4, 5, 1);
INSERT INTO `sys_user_role` VALUES (5, 8, 3);
SET FOREIGN_KEY_CHECKS = 1;
2. 整合MybatisPlus
在support 模块引入相关依赖,包括数据源相关,连接池相关,代码生成器相关等依赖,父工程已进行版本管理, 这里只需要引入。
- 在父pom.xml中对mybatisplus版本进行管理
注: druid 使用1.2.20之后版本,低版本和高版本mp存在LocalDateTime和数据库datetime转换问题
- 在support 模块引入相关依赖,在父版本中引入了 spring-boot-dependencies, 所以jdbc, mysql驱动, mybatis-plus代码生成器模板引擎版本跟随springboot整合包中的
support pom.xml
<dependency>
<groupId>com.ylp</groupId>
<artifactId>admin_common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
</dependency>
<!-- mybatis-plus代码生成器模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- mybatis-plus 代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</dependency>
3. MybatisPlus 配置
3.1 数据源配置
在start模块 applicationl.yml 中添加数据源相关配置( 项目后续配置都写在此模块,也可分文件),
server:
port: 9999
spring:
# 数据源
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 使用druid连接池
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/admin_system?serverTimezone=UTC
username: root
password: root
mybatis-plus:
global-config:
db-config:
logic-delete-field: deleted # 全局逻辑删除字段名
logic-delete-value: 1 # 逻辑已删除值
logic-not-delete-value: 0 # 逻辑未删除值
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # sql日志,开发环境开启
mapper-locations: classpath*:com/ylp/*/mapper/*.xml
运行测试:
3.2 mybatis-plus 分页插件配置
配置放到support下
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.ylp.*.mapper")
public class MybatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
// interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
return interceptor;
}
}
3.3 mybatis-plus 自动填充
也放到support mubatisplus config下
package com.ylp.support.config.mybatisplus;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/**
* 字段自动填充
*/
@Component
public class AutoFillMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
//log.info("开始插入填充...");
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
//log.info("开始更新填充...");
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
}
}
使用: @TableField(fill = FieldFill.INSERT) 或 @TableField(fill = FieldFill.INSERT_UPDATE)
3.4 代码生成器
暂时放到support模块, 后续集成到系统底层支持中,在系统前端界面可配置生成代码
package com.ylp.support.generator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.io.File;
import java.sql.Types;
import java.util.Collections;
public class MybatisPlusGenerator {
static final String url = "jdbc:mysql://127.0.0.1:3306/admin_system?serverTimezone=UTC"; // 数据库地址
static final String username = "root"; // 数据库用户名
static final String password = "root"; // 数据库密码
static final String authorName = "cc"; // 作者名
static final String moduleName = "admin_sys"; // 模块名
static final String parentPackageNameResource = "com/ylp/sys"; // mapper.xml路径
static final String parentPackageNameJava = "com.ylp.sys"; // java 文件父包名
// 要生成代码对应的数据表名
static final String tableName = "sys_role,sys_role_menu,sys_user_role,sys_menu,sys_user";
public static void main(String[] args) {
//System.out.println(System.getProperty("user.dir"));
FastAutoGenerator.create(url, username, password)
// 1.全局配置
.globalConfig(builder -> {
builder.author(authorName) // 设置作者
.enableSpringdoc() // 开启 swagger3 模式, 生成swagger注解
// 获取当前工程路径并定位到项目java目录下
.outputDir(System.getProperty("user.dir") + "/" + moduleName + "/src/main/java"); // 指定输出目录
})
// 2.数据源配置
.dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
if (typeCode == Types.SMALLINT) {
// 自定义类型转换
return DbColumnType.INTEGER;
}
return typeRegistry.getColumnType(metaInfo);
}))
// 3.包名策略配置
.packageConfig(builder -> {
builder.parent(parentPackageNameJava) // 设置父包名
.entity("domain.entity")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.controller("controller")
//.moduleName("system") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/" + moduleName + "/src/main/resources/" + parentPackageNameResource + "/mapper")); // 设置mapperXml生成路径
})
// 策略配置
.strategyConfig(builder -> {
builder.addInclude(tableName) // 设置需要生成的表名
// 覆盖已生成文件
// .entityBuilder().enableFileOverride()
// .mapperBuilder().enableFileOverride()
.serviceBuilder().enableFileOverride().formatServiceFileName("%sService");
//.addTablePrefix("t_", "c_"); // 设置过滤表前缀
})
// 配置模板
.templateConfig(builder -> {
//builder.controller(""); // 不生成controller
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}