项目环境:
开发环境:IntelliJ IDEA 2019.3.1 ,JDK1.8 ,MySQL: 5.6.47
采用远程数据库连接:使用ailiyun ESC CentOS 7.0
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
整个项目,我们xml文件都不需要创建,只需要一些依赖和对代码生成器进行稍微的配置就可以生成以上模块的代码。
一、远程数据库 user(自行创建)
二、创建工程:
依赖注入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 增加配置数据库连接池的依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<!-- 加入对应mysql版本的mysql-connector-java版本-->
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 如果你需要整合freemarker模板 需要加入此以来-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
配置模块介绍:
1.全局配置
GlobalConfig gc = new GlobalConfig();
//生成文件的输出目录
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
//设置作者
gc.setAuthor("alvin");
//是否覆盖文件
gc.setOpen(true);
//生成后开打开文件
gc.setOpen(false);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
2.数据源配置
DataSourceConfig dsc = new DataSourceConfig();
//数据库连接url
dsc.setUrl("jdbc:mysql://121.40.83.80:3306/user?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
//数据库类型 默认MySQL
dsc.setDbType(DbType.MYSQL);
//自定义类型转换
dsc.setTypeConvert(new MySqlTypeConvert());
dsc.setDriverName("com.mysql.jdbc.Driver");
//远程数据库登录名
dsc.setUsername("alvin");
//远程数据库密码
dsc.setPassword("123456");
mpg.setDataSource(dsc);
3.包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));
//父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setParent("com.alvin.mybatisplusgenerator");
mpg.setPackageInfo(pc);
4.使用模板
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/*
cfg.setFileCreate(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建的目录");
return false;
}
});
*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
5.配置自定义模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
6.策略配置
StrategyConfig strategy = new StrategyConfig();
//设置命名格式
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//设置自定义继承的Entity类全称,带包名
// strategy.setSuperEntityClass("com.alvin.mybatisplusgenerator.BaseEntity");
//实体是否为lombok模型(默认 false)
strategy.setEntityLombokModel(true);
//生成 @RestController 控制器
strategy.setRestControllerStyle(true);
//设置自定义继承的Entity类全称,带包名
//strategy.setSuperEntityClass("com.alvin.mybatisplusgenerator");
// 设置自定义继承的Controller类全称,带包名
// strategy.setSuperControllerClass("com.alvin.mybatisplusgenerator.BaseController");
// 设置自定义基础的Entity类,公共字段
strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
//驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
//表名前缀
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
三、代码生成器配置:
package com.alvin.mybatisplusgenerator.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
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("alvin");
//是否覆盖文件
gc.setOpen(true);
//生成后开打开文件
gc.setOpen(false);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
/**
* 数据源配置
*/
DataSourceConfig dsc = new DataSourceConfig();
//数据库连接url
dsc.setUrl("jdbc:mysql://121.40.83.80:3306/user?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
//数据库类型 默认MySQL
dsc.setDbType(DbType.MYSQL);
//自定义类型转换
dsc.setTypeConvert(new MySqlTypeConvert());
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("alvin");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
/**
* 包配置
*/
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));
//父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setParent("com.alvin.mybatisplusgenerator");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
/**
* 模板
*/
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/*
cfg.setFileCreate(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建的目录");
return false;
}
});
*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
/**
* 配置模板
*/
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
/**
* 策略配置
*/
StrategyConfig strategy = new StrategyConfig();
//设置命名格式
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//设置自定义继承的Entity类全称,带包名
// strategy.setSuperEntityClass("com.alvin.mybatisplusgenerator.BaseEntity");
//实体是否为lombok模型(默认 false)
strategy.setEntityLombokModel(true);
//生成 @RestController 控制器
strategy.setRestControllerStyle(true);
//设置自定义继承的Entity类全称,带包名
//strategy.setSuperEntityClass("com.alvin.mybatisplusgenerator");
// 设置自定义继承的Controller类全称,带包名
// strategy.setSuperControllerClass("com.alvin.mybatisplusgenerator.BaseController");
// 设置自定义基础的Entity类,公共字段
strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
//驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
//表名前缀
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
四、生成代码
执行配置好的代码生成器main方法
在终端输入模块名和数据库表名,如果有多个表用英文逗号隔开
生成前后对比
参考官网:https://mp.baomidou.com/guide/quick-start.html