1.引入依赖
2.创建生成器
package generator;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.List;
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
/**
* 个性化代码生成器
* huaxiaozhou.add
*/
public class myGenerator {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");//生成代码输出路径
gc.setAuthor("hua_zhou");//文件作者
gc.setFileOverride(false);// 是否覆盖同名文件,默认是false
gc.setOpen(false);
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setSwagger2(true); //实体属性 Swagger2 注解
gc.setIdType(IdType.AUTO);//主键自增
/* 自定义文件命名,注意 %s 会自动填充表实体属性! */
// gc.setMapperName("%sDao");
// gc.setXmlName("%sDao");
// gc.setServiceName("MP%sService");
// gc.setServiceImplName("%sServiceDiy");
// gc.setControllerName("%sAction");
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/hua?" +
"useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setTypeConvert(
// 自定义数据库表字段类型转换【可选】1.8 新增时间对象
new MySqlTypeConvert() {
@Override
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
if (fieldType.toLowerCase().equals("bit")) {
return DbColumnType.BOOLEAN;
}
if (fieldType.toLowerCase().equals("tinyint")) {
return DbColumnType.BOOLEAN;
}
if (fieldType.toLowerCase().equals("date")) {
return DbColumnType.LOCAL_DATE;
}
if (fieldType.toLowerCase().equals("time")) {
return DbColumnType.LOCAL_TIME;
}
if (fieldType.toLowerCase().equals("datetime")) {
return DbColumnType.LOCAL_DATE_TIME;
}
return super.processTypeConvert(globalConfig, fieldType);
}
}
);
/*
包配置
*/
PackageConfig pc = new PackageConfig();
pc.setModuleName("");
pc.setParent("com.example.hz");
/*
策略配置
*/
StrategyConfig strategy = new StrategyConfig();
//数据库表映射到实体的命名策略
strategy.setNaming(NamingStrategy.underline_to_camel);
// 数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//自定义继承的Entity类全称,带包名
//strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
strategy.setSuperEntityColumns(new String[]{"message", "message1"});
// 是否为lombok模型
strategy.setEntityLombokModel(true);
//生成 @RestController 控制器
strategy.setRestControllerStyle(true);
//strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
//包含的表名
strategy.setInclude();
//strategy.setSuperEntityColumns("id");
// 驼峰转连字符 如 umps_user 变为 upms/user
strategy.setControllerMappingHyphenStyle(true);
// 表前缀
strategy.setTablePrefix(pc.getModuleName() + "_");
/*
自定义输出
*/
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
//自定义xml的存放路径
List focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义Mapper.xml文件存放的路径
return projectPath + "/src/main/resources/mappers/"
+ tableInfo.getEntityName() + "Mapper.xml";
}
});
cfg.setFileOutConfigList(focList);
/* 设置 全局变量,数据源,包,策略配置,自定义输出xml路径*/
mpg.setGlobalConfig(gc);
mpg.setDataSource(dsc);
mpg.setPackageInfo(pc);
mpg.setStrategy(strategy);
mpg.setCfg(cfg);
//默认为velocity模板 如果自定义需要引入模板依赖,设置新模板 例:Freemarker
//mpg.setTemplate(new FreemarkerTemplateEngine());
mpg.execute();
}
}
数据库建表语句
/*
Navicat Premium Data Transfer
Source Server : 本地
Source Server Type : MySQL
Source Server Version : 80016
Source Host : localhost:3306
Source Schema : hua
Target Server Type : MySQL
Target Server Version : 80016
File Encoding : 65001
Date: 22/06/2019 17:26:04
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for hz_menu
-- ----------------------------
DROP TABLE IF EXISTS `hz_menu`;
CREATE TABLE `hz_menu` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) NULL DEFAULT NULL COMMENT '父菜单ID,一级菜单为0',
`menu_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '菜单名称',
`path` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '路径',
`menu_type` smallint(2) NOT NULL COMMENT '类型:0:目录,1:菜单,2:按钮',
`icon` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '菜单图标',
`create_uid` int(11) NOT NULL COMMENT '创建者ID',
`update_uid` int(11) NOT NULL COMMENT '修改者ID',
`create_time` datetime(0) NOT NULL COMMENT '创建时间',
`update_time` datetime(0) NOT NULL COMMENT '修改时间',
`status` smallint(2) NOT NULL COMMENT '状态 0:禁用 1:正常',
`router` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '路由',
`alias` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '别名',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 42 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '菜单表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for hz_menu_resource
-- ----------------------------
DROP TABLE IF EXISTS `hz_menu_resource`;
CREATE TABLE `hz_menu_resource` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`menu_id` int(11) NULL DEFAULT NULL COMMENT '菜单ID',
`resource_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '资源ID',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 101 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '菜单资源关系表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for hz_resource
-- ----------------------------
DROP TABLE IF EXISTS `hz_resource`;
CREATE TABLE `hz_resource` (
`id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`resource_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '资源名称',
`mapping` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '路径映射',
`method` varchar(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '请求方式',
`auth_type` smallint(2) NOT NULL COMMENT '权限认证类型',
`update_time` datetime(0) NULL DEFAULT NULL,
`perm` varchar(68) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '权限标识',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '资源表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for hz_role
-- ----------------------------
DROP TABLE IF EXISTS `hz_role`;
CREATE TABLE `hz_role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '角色名称',
`create_uid` int(11) NOT NULL COMMENT '创建者ID',
`update_uid` int(11) NOT NULL COMMENT '修改者ID',
`create_time` datetime(0) NOT NULL COMMENT '创建时间',
`update_time` datetime(0) NOT NULL COMMENT '修改时间',
`remark` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '角色表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for hz_role_menu
-- ----------------------------
DROP TABLE IF EXISTS `hz_role_menu`;
CREATE TABLE `hz_role_menu` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_id` int(11) NOT NULL COMMENT '角色ID',
`menu_id` int(11) NOT NULL COMMENT '菜单ID',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 277 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '角色菜单关系表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for hz_role_resource
-- ----------------------------
DROP TABLE IF EXISTS `hz_role_resource`;
CREATE TABLE `hz_role_resource` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_id` int(11) NOT NULL COMMENT '角色ID',
`resource_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '菜单ID',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '角色资源关系表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for hz_user
-- ----------------------------
DROP TABLE IF EXISTS `hz_user`;
CREATE TABLE `hz_user` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`nickname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户名',
`email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '邮箱',
`phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '手机',
`status` smallint(2) NOT NULL COMMENT '状态 0:禁用 1:正常',
`create_uid` int(11) NOT NULL COMMENT '创建者ID',
`create_time` datetime(0) NOT NULL COMMENT '创建时间',
`update_time` datetime(0) NOT NULL COMMENT '修改时间',
`login_name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '登陆名',
`password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '密码',
`ip` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'IP地址',
PRIMARY KEY (`uid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 19 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '系统用户表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for hz_user_role
-- ----------------------------
DROP TABLE IF EXISTS `hz_user_role`;
CREATE TABLE `hz_user_role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NULL DEFAULT NULL COMMENT '用户ID',
`role_id` int(11) NULL DEFAULT NULL COMMENT '角色ID',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 50 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '系统用户角色关系表' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;