AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

效果如下图:

Generator官方文档 : https://mybatis.plus/guide/generator.html

使用教程

1.使用idea创建SpringBoot项目

然后一直next.

2. 添加依赖pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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> <dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<!-- MySQL连接驱动,注意版本问题 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--MyBatis-Plus 支持 Velocity(默认)、Freemarker、Beetl,用户可以选择自己熟悉的模板引擎,如果都不满足您的要求,可以采用自定义模板引擎。-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.0.13.RELEASE</version>
</dependency>
</dependencies>

3. 创建Java项目CodeGenerator.java

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") +"/"+ scanner("项目名称");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("YHL");
gc.setOpen(false);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc); // 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc); // 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(scanner("包名"));
pc.setModuleName(scanner("模块名"));
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 的名称会跟着发生变化!!
System.out.println(projectPath+"================================");
return projectPath + "/src/main/resources/mapper/" + 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);
strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
// 写于父类中的公共字段
strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}

4. 运行CodeGenerator,根据提示输入.

最新文章

  1. [原] KVM 虚拟化原理探究(6)— 块设备IO虚拟化
  2. 高性能Javascript--高效的数据访问
  3. sql注入的基本防范手段
  4. [outlook]打开以后就自动进入安全模式的解决方法。Outlook start in safe mode.
  5. This tag and its children can be replaced by one &lt;TextView/&gt; and a compound drawable
  6. NABCD模型需求分析
  7. ajax讲解:“创建用户”和“用户登录”练习
  8. PHP中超全局变量$GLOBALS和global的区别
  9. JS 日期工具类-基于yDate
  10. ERROR 1114 (HY000): The table &#39;adv_date_tmp&#39; is full(Mysql临时表应用)
  11. Node.js权威指南 (5) - 使用Buffer类处理二进制数据
  12. 剑指offer替换空格
  13. java继承(一)
  14. Java高级篇(四)——反射
  15. 【python 3】 集合方法操作汇总
  16. collections和collection 还有集合
  17. Flume配置Multiplexing Channel Selector
  18. bootstrap-table方法之:合并单元格
  19. (转)C#连接Oracle数据库(直接引用dll使用)
  20. jmeter对需要登录的接口进行性能测测试

热门文章

  1. CentOS 8 安装 VirtualBox 增强功能
  2. IDEA搭建多模块maven项目
  3. spark源码分析, 任务提交及序列化
  4. Spring学习(五)--Spring的IOC
  5. mysql 空值(null)和空字符(&#39;&#39;)的区别
  6. Hbuilder MUI 页面刷新及页面传值问题
  7. 01 How does C Programming work ? C语言如何工作?
  8. JDBC Java 程序从 MySQL 数据库中读取数据,并备份到 xml 文档中
  9. 高性能HTTP加速器Varnish--基础知识
  10. ubuntu20 使用命令安装 mongodb