1. 想详细了解MyBatis-Plus代码生成器可以先去官网了解
2. 集成
先新建spring boot工程mp-generator ,并添加依赖(如果是聚合项目,可以新建模块集成)
org.springframework
spring-web
mysql
mysql-connector-java
com.baomidou
mybatis-plus-boot-starter
3.4.0
com.baomidou
mybatis-plus-generator
3.4.0
org.freemarker
freemarker
2.3.31
org.projectlombok
lombok
true
io.springfox
springfox-swagger2
2.9.2
org.apache.commons
commons-lang3
3. 添加配置
新建entity包,添加一个实体类的父类
package com.mp.generator.entity;
import com.baomidou.mybatisplus.annotation.*;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public abstract class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "主键")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty(value = "是否删除,0 未删除, 1 删除")
@TableLogic
@TableField(value = "deleted", fill = FieldFill.INSERT)
private Integer deleted;
@ApiModelProperty(value = "是否可用,1 可用,0 不可用")
@TableField(value = "available", fill = FieldFill.INSERT)
private Integer available;
@ApiModelProperty(value = "创建时间")
@TableField(value = "create_time", fill = FieldFill.INSERT)
private Date createTime;
@ApiModelProperty(value = "修改时间")
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
新建config包,在包下添加Config类,配置全局信息
package com.mp.generator.config;
public class Config {
public static String URL = "jdbc:mysql://127.0.0.1:3306/dynamic1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true";
public static String USERNAME = "root";
public static String PASSWORD = "123456";
public static String DRIVER = "com.mysql.cj.jdbc.Driver";
/**
* 包名
*/
public static final String PACKAGE_PARENT = "com.mp.generator";
public static final String PACKAGE_NAME_CONTROLLER = "controller";
public static final String PACKAGE_NAME_MODEL = "entity";
public static final String PACKAGE_NAME_DAO = "mapper";
public static final String PACKAGE_NAME_SERVICE = "service";
public static final String PACKAGE_NAME_SERVICE_IMPL = "service.impl";
/**
* 文件名后缀:Dao
*/
public static final String FILE_NAME_DAO = "%sMapper";
/**
* 文件名后缀:MapperXml
*/
public static final String FILE_NAME_XML = "%sMapper";
/**
* MP开头,Service结尾
*/
public static final String FILE_NAME_SERVICE = "%sService";
/**
* 文件名后缀:ServiceImpl
*/
public static final String FILE_NAME_SERVICE_IMPL = "%sServiceImpl";
/**
* 文件名后缀:Controller
*/
public static final String FILE_NAME_CONTROLLER = "%sController";
/**
* 逻辑删除字段
*/
public static final String FIELD_LOGIC_DELETE_NAME = "deleted";
/**
* 作者
*/
public static final String AUTHOR = "generator";
/**
* 生成文件的输出目录
*/
// public static String projectPath = System.getProperty("user.dir") + "/mp-generator";
public static String projectPath = System.getProperty("user.dir");
/**
* 输出目录
*/
public static final String outputDir = projectPath + "/src/main/java";
/**
* 模板引擎。velocity / freemarker / beetl
*/
public static final String TEMPLATE_ENGINE = "freemarker";
/**
* 是否支持Swagger,默认不支持
*/
public static final Boolean SWAGGER_SUPPORT = true;
}
新建utils包,添加CommonUtils类,配置生成信息
package com.mp.generator.utils;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.mp.generator.config.Config;
import com.mp.generator.entity.BaseEntity;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CommonUtils {
//读取控制台内容
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入" + tip + ":");
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
//数据连接信息
private static DataSourceConfig dataSourceConfig() {
return new DataSourceConfig()
.setDbType(DbType.MYSQL)
.setUrl(Config.URL)
.setUsername(Config.USERNAME)
.setPassword(Config.PASSWORD)
.setDriverName(Config.DRIVER)
;
}
// 配置
private static GlobalConfig globalConfig() {
return new GlobalConfig()
.setAuthor(scanner("作者"))
.setOutputDir(Config.outputDir)
.setFileOverride(true) // 是否覆盖已有文件
.setOpen(false) // 是否打开输出目录
.setDateType(DateType.ONLY_DATE) // 时间采用java 8,(操作工具类:JavaLib => DateTimeUtils)
.setEnableCache(false)// XML 二级缓存
.setBaseResultMap(false)// XML ResultMap
.setBaseColumnList(false)// XML columList
.setKotlin(false) //是否生成 kotlin 代码
.setMapperName(Config.FILE_NAME_DAO) //自定义文件命名,注意 %s 会自动填充表实体属性!
.setXmlName(Config.FILE_NAME_XML)
.setServiceName(Config.FILE_NAME_SERVICE)
.setServiceImplName(Config.FILE_NAME_SERVICE_IMPL)
.setControllerName(Config.FILE_NAME_CONTROLLER)
.setSwagger2(Config.SWAGGER_SUPPORT) // model swagger2
;
}
//实体类配置
private static StrategyConfig strategyConfig() {
return new StrategyConfig()
.setChainModel(true) // 【实体】是否为构建者模型(默认 false)
.setNaming(NamingStrategy.underline_to_camel)
.setColumnNaming(NamingStrategy.underline_to_camel)
.setSuperEntityClass(BaseEntity.class)
.setEntityLombokModel(true)
.setRestControllerStyle(true)
.setInclude(scanner("表名,多个英文逗号分割").split(","))
.setControllerMappingHyphenStyle(false)
.setEntityTableFieldAnnotationEnable(true) //是否生成实体时,生成字段注解,包括@TableName("")
;
}
// 包信息配置
private static PackageConfig packageConfig() {
return new PackageConfig()
// .setModuleName(scanner("模块名"))
.setParent(Config.PACKAGE_PARENT)
// 如果是单体项目请注释掉 .setModuleName(scanner("模块名"))即可,把下面注释取消
.setController(Config.PACKAGE_NAME_CONTROLLER)
.setEntity(Config.PACKAGE_NAME_MODEL)
.setMapper(Config.PACKAGE_NAME_DAO)
.setService(Config.PACKAGE_NAME_SERVICE)
.setServiceImpl(Config.PACKAGE_NAME_SERVICE_IMPL)
;
}
private static InjectionConfig injectionConfig() {
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return Config.projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
injectionConfig.setFileOutConfigList(focList);
return injectionConfig;
}
private static TemplateConfig templateConfig() {
TemplateConfig templateConfig = new TemplateConfig();
//配置自定义输出模板,指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
templateConfig.setController("templates/controllerGenerator.java");
templateConfig.setServiceImpl("templates/serviceImplGenerator.java");
templateConfig.setMapper("templates/mapperGenerator.java");
templateConfig.setXml(null);
return templateConfig;
}
// 获取模板引擎
private static AbstractTemplateEngine getTemplateEngine() {
String templateEngine = Config.TEMPLATE_ENGINE;
switch (templateEngine) {
case "velocity":
return new VelocityTemplateEngine();
case "freemarker":
return new FreemarkerTemplateEngine();
case "beetl":
return new BeetlTemplateEngine();
default:
return new VelocityTemplateEngine();
}
}
// 执行器
public static void execute() {
GlobalConfig globalConfig = globalConfig();
DataSourceConfig dataSourceConfig = dataSourceConfig();
PackageConfig packageConfig = packageConfig();
StrategyConfig strategyConfig = strategyConfig();
InjectionConfig injectionConfig = injectionConfig();
AbstractTemplateEngine templateEngine = getTemplateEngine();
new AutoGenerator()
.setGlobalConfig(globalConfig)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(packageConfig)
.setTemplateEngine(templateEngine)
.setCfg(injectionConfig)
.setTemplate(templateConfig())
.execute();
}
}
修改自己想要的模板,在resource目录下新建templates目录,然后存放所有的模板
mapperGenerator.java.ftl
package ${package.Mapper};
import ${package.Entity}.${entity};
import ${superMapperClassPackage};
import org.springframework.stereotype.Repository;
/**
*
* ${table.comment!} Mapper 接口
*
*
* @author ${author}
* @since ${date}
*/
@Repository
<#if kotlin>
interface ${table.mapperName} : ${superMapperClass}<${entity}>
<#else>
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {
}
#if>
serviceImplGenerator.java.ftl
package ${package.ServiceImpl};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
*
* ${table.comment!} 服务实现类
*
*
* @author ${author}
* @since ${date}
*/
@Slf4j
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {
}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {
}
#if>
controllerGenerator.java.ftl
package ${package.Controller};
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
#if>
/**
*
* ${table.comment!} 前端控制器
*
*
* @author ${author}
* @since ${date}
*/
<#if restControllerStyle>
@RestController
<#else>
@Controller
#if>
@RequestMapping("/api/mp-generator<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}#if>")
@Api(value = "/api/mp-generator<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}#if>", tags = {"${table.comment!}接口"})
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
#if>
}
#if>
4. 添加一个启动类运行
package com.mp.generator;
import com.mp.generator.utils.CommonUtils;
public class MySQLCodeGenerator {
public static void main(String[] args) {
CommonUtils.execute();
}
}
sql脚本
/*
Navicat Premium Data Transfer
Source Server : 本地
Source Server Type : MySQL
Source Server Version : 50729
Source Host : 127.0.0.1:3306
Source Schema : dynamic1
Target Server Type : MySQL
Target Server Version : 50729
File Encoding : 65001
Date: 18/03/2021 19:02:02
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user_info
-- ----------------------------
DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`available` int(1) NULL DEFAULT 1 COMMENT '是否可用,1 可用,0 不可用',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`deleted` int(1) NULL DEFAULT 0 COMMENT '是否删除,0 未删除, 1 删除',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
`avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '头像',
`email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '邮箱',
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码',
`phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '手机号',
`salt` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '盐',
`sex` int(1) NULL DEFAULT NULL COMMENT '性别 0未知 1女 2男',
`user_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '姓名',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `user_info_user_name`(`user_name`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 31 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户信息表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user_info
-- ----------------------------
INSERT INTO `user_info` VALUES (1, 1, '2021-01-13 13:30:32', 0, '2021-01-15 15:19:15', NULL, '[email protected]', '55555', '1234567890', NULL, 0, 'testd57fdf65-6dea-44ae-8493-59bfb93dfec611111');
INSERT INTO `user_info` VALUES (2, 1, '2021-01-13 13:30:32', 0, '2021-01-15 15:19:15', NULL, '[email protected]', '55555', '1234567890', NULL, 0, 'test3ef83e0c-d20d-498d-852d-052bab06199922222');
SET FOREIGN_KEY_CHECKS = 1;