Mybatis-Plus-AutoGenerator 最详细使用方法_Java_软件编程 - 编程客栈

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。可以通过模版等一系列的方式来生成代码,⚠️这个比Mybatis-Generator的更加强大,纯java代码。。官方地址:https://mp.baomidou.com/guide/generator.html

  1. package com.cikers.ps;
  2. import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  3. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  4. import com.baomidou.mybatisplus.generator.AutoGenerator;
  5. import com.baomidou.mybatisplus.generator.InjectionConfig;
  6. import com.baomidou.mybatisplus.generator.config.*;
  7. import com.baomidou.mybatisplus.generator.config.po.TableInfo;
  8. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  9. import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
  10. import org.apache.commons.lang3.StringUtils;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Scanner;
  14. public class MysqlGenerator {
  15. public static String scanner(String tip) {
  16. Scanner scanner = new Scanner(System.in);
  17. StringBuilder help = new StringBuilder();
  18. help.append("请输入" + tip + ":");
  19. System.out.println(help.toString());
  20. if (scanner.hasNext()) {
  21. String ipt = scanner.next();
  22. if (StringUtils.isNotEmpty(ipt)) {
  23. return ipt;
  24. }
  25. }
  26. throw new MybatisPlusException("请输入正确的" + tip + "!");
  27. }
  28. public static void main(String[] args) {
  29. // 代码生成器
  30. AutoGenerator mpg = new AutoGenerator();
  31. // 全局配置
  32. GlobalConfig gc = new GlobalConfig();
  33. String projectPath = "/Users/syk/Documents///";
  34. gc.setOutputDir(projectPath + "/src/main/java");
  35. gc.setAuthor("syk");
  36. gc.setOpen(false);
  37. gc.setBaseResultMap(true);
  38. gc.setBaseColumnList(true);
  39. //gc.setControllerName("SSSSScontroller");
  40. // 是否覆盖已有文件
  41. gc.setFileOverride(false);
  42. mpg.setGlobalConfig(gc);
  43. // 数据源配置
  44. DataSourceConfig dsc = new DataSourceConfig();
  45. dsc.setUrl("jdbc:mysql:///newstack_db?useUnicode=true&characterEncoding=UTF-8");
  46. // dsc.setSchemaName("public");
  47. dsc.setDriverName("com.mysql.jdbc.Driver");
  48. dsc.setUsername("root");
  49. dsc.setPassword("password");
  50. mpg.setDataSource(dsc);
  51. // 包配置
  52. PackageConfig pc = new PackageConfig();
  53. //pc.setModuleName(scanner("模块名"));
  54. pc.setParent(null);
  55. // 这个地址是生成的配置文件的包路径
  56. pc.setEntity("com.cikers.ps.model.entity");
  57. //pc.setController("com.cikers.ps.controller");
  58. pc.setMapper("com.cikers.ps.mapper");
  59. mpg.setPackageInfo(pc);
  60. // 自定义配置
  61. InjectionConfig cfg = new InjectionConfig() {
  62. @Override
  63. public void initMap() {
  64. // to do nothing
  65. }
  66. };
  67. // 如果模板引擎是 freemarker
  68. String templatePath = "/templates/mapper.xml.ftl";
  69. // 如果模板引擎是 velocity
  70. //String templatePath = "/templates/mapper.xml.vm";
  71. // 自定义输出配置
  72. List<FileOutConfig> focList = new ArrayList<>();
  73. // 自定义配置会被优先输出
  74. focList.add(new FileOutConfig(templatePath) {
  75. @Override
  76. public String outputFile(TableInfo tableInfo) {
  77. // 自定义输出文件名
  78. return projectPath + "/src/main/resources/mapper/entity"
    • "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  79. }
  80. });
  81. cfg.setFileOutConfigList(focList);
  82. mpg.setCfg(cfg);
  83. // 配置模板
  84. TemplateConfig templateConfig = new TemplateConfig();
  85. // //配置自定义输出模板
  86. // 不需要其他的类型时,直接设置为null就不会成对应的模版了
  87. //templateConfig.setEntity("...");
  88. templateConfig.setService(null);
  89. templateConfig.setController(null);
  90. templateConfig.setServiceImpl(null);
  91. // 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改,
  92. // 放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也
  93. // 可以自定义模板名称 只要放到目录下,名字不变 就会采用这个模版 下面这句有没有无所谓
  94. // 模版去github上看地址:
  95. /**https://github.com/baomidou/mybatis-plus/tree/3.0/mybatis-plus-generator/src/main/resources/templates*/
  96. //templateConfig.setEntity("/templates/entity.java");
  97. templateConfig.setXml(null);
  98. mpg.setTemplate(templateConfig);
  99. // 策略配置
  100. StrategyConfig strategy = new StrategyConfig();
  101. strategy.setNaming(NamingStrategy.underline_to_camel);
  102. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  103. strategy.setSuperEntityClass("com.cikers.ps.model.BaseEntity");
  104. strategy.setSuperMapperClass("com.cikers.ps.util.IMapper");
  105. strategy.setEntityLombokModel(false);
  106. //strategy.setRestControllerStyle(false);
  107. //strategy.setSuperControllerClass("com.cikers.ps.controller.MysqlController");
  108. strategy.setInclude(scanner("表名"));
  109. // 设置继承的父类字段
  110. strategy.setSuperEntityColumns("id","modifiedBy","modifiedOn","createdBy","createdOn");
  111. //strategy.setControllerMappingHyphenStyle(true);
  112. //strategy.setTablePrefix(pc.getModuleName() + "_");
  113. mpg.setStrategy(strategy);
  114. mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  115. mpg.execute();
  116. }
  117. }

其中需要的maven依赖

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.0-RELEASE</version>
  5. </dependency>
  6. <!-- mp自动代码生成-->
  7. <dependency>
  8. <groupId>com.baomidou</groupId>
  9. <artifactId>mybatis-plus-generator</artifactId>
  10. <version>3.0.7.1</version>
  11. </dependency>
  12. <!-- velocity 模板引擎, 默认 -->
  13. <dependency>
  14. <groupId>org.apache.velocity</groupId>
  15. <artifactId>velocity-engine-core</artifactId>
  16. <version>2.0</version>
  17. </dependency>
  18. <!-- freemarker 模板引擎 -->
  19. <dependency>
  20. <groupId>org.freemarker</groupId>
  21. <artifactId>freemarker</artifactId>
  22. <version>2.3.23</version>
  23. </dependency>
  24. <!-- beetl 模板引擎 -->
  25. <dependency>
  26. <groupId>com.ibeetl</groupId>
  27. <artifactId>beetl</artifactId>
  28. <version>2.2.5</version>
  29. </dependency>

Mybatis-Plus-AutoGenerator 最详细使用方法

 运行输入表面就可以了!!!!

到此这篇关于Mybatis-Plus-AutoGenerator 最详细使用方法的文章就介绍到这了,更多相关Mybatis Plus AutoGenerator内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

本文标题: Mybatis-Plus-AutoGenerator 最详细使用方法

本文地址: http://www.cppcns.com/ruanjian/java/303820.html


Original url: Access
Created at: 2020-09-16 15:22:27
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论