个人主页:花棉袄
本章内容:【MyBatis-Plus代码生成器】
✍ 版权: 本文由【花棉袄】原创在 CSDN 首发 需要转载请联系博主
Gitee仓库地址:2022最新版MyBatisplus代码生成器
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<exclusions>
<exclusion>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>${commons.io.version}version>
dependency>
<dependency>
<groupId>commons-configurationgroupId>
<artifactId>commons-configurationartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson.version}version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocityartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<exclusions>
<exclusion>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
exclusion>
<exclusion>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>io.swaggergroupId>
<artifactId>swagger-annotationsartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<repositories>
<repository>
<id>publicid>
<name>aliyun nexusname>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<releases>
<enabled>trueenabled>
releases>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>publicid>
<name>aliyun nexusname>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>falseenabled>
snapshots>
pluginRepository>
pluginRepositories>
# 设置输出目录
path: E://JAVA//TemplateAdmin//management//src//main//java
# 设置mapperXml生成路径
pathMapperXml: E://JAVA//TemplateAdmin//management//src//main//resources//mapper
# 作者名
author: 天才小狐狸
# 注释时间
commentDate: yyyy年MM月dd日
# 数据库地址
databaseIp: 49.235.*******
# 数据库端口后
spot: 3306
# 数据库名字
databaseName: community-management
# 数据库账号名
mysqlUser: test
# 数据库密码
mysqlPassword: 123456
# 设置父包名
parent: com.michale
# 设置父包模块名
moduleName: management
# 设置实体层
entity: entity
# 设置服务层
service: service
# 设置实现层
serviceImpl: service.impl
# 设置mapper层
mapper: mapper
# 设置controller层
controller: controller
# 其他层设置
other: common
# 设置需要生成的表名
tableName: tb_community
# 设置是否生成全部表名
allTableName: false
# 设置过滤表前缀
tablePrefix: tb_
# 设置自动填充的字段
create_time: create_time
update_time: update_time
/**
* Author: 花棉袄
* Date: 2022年08月20日
* Describe: 获取配置信息
*/
public class GetProperties {
public static Configuration getConfig() {
try {
return new PropertiesConfiguration("application-v1-pack.yml");
} catch (ConfigurationException e) {
throw new RRException("获取配置文件失败,", e);
}
}
}
package com.michale.generator.v1.utils;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static com.michale.generator.v1.utils.GetProperties.getConfig;
/**
* Author: 花棉袄
* Date: 2022年08月20日
* Describe: 生成代码
*/
public class AutoGenerator implements Serializable {
public static void Generator() throws IOException {
Configuration config = getConfig();
//数据库IP地址
String databaseIp = config.getString("databaseIp");
//数据库端口号
String spot = config.getString("spot");
//数据库名
String databaseName = config.getString("databaseName");
//数据库账号名
String mysqlUser = config.getString("mysqlUser");
//数据库密码
String mysqlPassword = config.getString("mysqlPassword");
//Mysql url配置 只需选择 String mysql = mysql5;或者 String mysql = mysql8; 其他无需修改
String mysqlBase = "jdbc:mysql://" + databaseIp + ":" + spot + "/" + databaseName;
String mysql5Base = "?characterEncoding=utf-8&useSSL=false";
String mysql8Base = "?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false";
String mysql5 = mysqlBase + mysql5Base;
String mysql8 = mysqlBase + mysql8Base;
//选择mysql5或者mysql8 String mysql = mysql5;或者 String mysql = mysql8;
String mysql = mysql5;
FastAutoGenerator.create(mysql, mysqlUser, mysqlPassword)
.globalConfig(builder -> {
builder.author(config.getString("author")) // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.disableOpenDir()//关闭资源管理器
.outputDir(config.getString("path")) // 指定输出目录
.commentDate(config.getString("commentDate"))//注释时间
;
})
.packageConfig(builder -> {
builder.parent(config.getString("parent")) // 设置父包名
.moduleName(config.getString("moduleName")) // 设置父包模块名
.entity(config.getString("entity")) //设置实体层
.service(config.getString("service")) //设置服务层
.serviceImpl(config.getString("serviceImpl")) //设置实现层
.mapper(config.getString("mapper")) //设置mapper层
.controller(config.getString("controller")) //设置controller层
.other(config.getString("other")) //其他层设置
.pathInfo(Collections.singletonMap(OutputFile.xml, config.getString("pathMapperXml"))); // 设置mapperXml生成路径
})
.strategyConfig((scanner, builder) -> {
builder.addInclude(check(config.getString("tableName"))) // 设置需要生成的表名
.addTablePrefix(config.getString("tablePrefix"))
.serviceBuilder() //开启service策略配置
.formatServiceFileName("%sService") //取消Service前的I
.controllerBuilder() //开启controller策略配置
.enableRestStyle() //配置restful风格
.enableHyphenStyle() //url中驼峰转连字符
.entityBuilder() //开启实体类配置
.addTableFills(new Column(config.getString("create_time"), FieldFill.INSERT)) //配置添加自动填充字段
.addTableFills(new Column(config.getString("update_time"), FieldFill.INSERT_UPDATE)) //添加和更新配置自动填充字段
.enableLombok() //开启lombok
.controllerBuilder().enableHyphenStyle().enableRestStyle()
//.enableChainModel()//开启lombok链式操作@Accessors(chain = true)
;
}).templateConfig(builder -> builder
.entity("/templates/v1/entity.java.vm")
.service("/templates/v1/service.java.vm")
.serviceImpl("/templates/v1/serviceImpl.java.vm")
.mapper("/templates/v1/mapper.java.vm")
.xml("/templates/v1/mapper.xml.vm")
.controller("/templates/v1/controller.java.vm")
.build())// 使用Freemarker 引擎模板,默认的是Velocity引擎模板
.execute();
}
//输入处理多个表的情况
public static List<String> check(String allTableName) {
if (StringUtils.equalsIgnoreCase("true", allTableName)) {
return getTables("all");
} else {
List<String> strings = new LinkedList<>();
strings.add(getConfig().getString("tableName"));
return strings;
}
}
protected static List<String> getTables(String tables) {
return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
}
}
/**
* Author: 花棉袄
* Date: 2022年08月18日
* Describe:
*/
@RestController
@RequestMapping("/v1")
public class GeneratorController {
@GetMapping("/generator")
@ApiOperation(value = "", notes = "")
public Result generator() throws IOException {
System.out.println(getConfig().getString("author"));
AutoGenerator.Generator();
return new Result(true, 200, "创建完成");
}
}
自定义代码模板:点我下载
通用常量信息:点我下载