GeneratorCodeConfig.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.huimv.common.utils;
  2. import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  3. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  4. import com.baomidou.mybatisplus.generator.AutoGenerator;
  5. import com.baomidou.mybatisplus.generator.config.*;
  6. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  7. import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
  8. import java.util.Scanner;
  9. /**
  10. * 自动生成mybatisplus的相关代码
  11. */
  12. public class GeneratorCodeConfig {
  13. public static String scanner(String tip) {
  14. Scanner scanner = new Scanner(System.in);
  15. StringBuilder help = new StringBuilder();
  16. help.append("请输入" + tip + ":");
  17. System.out.println(help.toString());
  18. if (scanner.hasNext()) {
  19. String ipt = scanner.next();
  20. if (StringUtils.isNotBlank(ipt)) {
  21. return ipt;
  22. }
  23. }
  24. throw new MybatisPlusException("请输入正确的" + tip + "!");
  25. }
  26. public static void main(String[] args) {
  27. // 代码生成器
  28. AutoGenerator mpg = new AutoGenerator();
  29. // 全局配置
  30. GlobalConfig gc = new GlobalConfig();
  31. String projectPath = System.getProperty("user.dir");
  32. gc.setOutputDir(projectPath + "/huimv-common/src/main/java");
  33. gc.setAuthor("astupidcoder");
  34. gc.setOpen(false);
  35. //实体属性 Swagger2 注解
  36. gc.setSwagger2(false);
  37. mpg.setGlobalConfig(gc);
  38. // 数据源配置
  39. DataSourceConfig dsc = new DataSourceConfig();
  40. dsc.setUrl("jdbc:mysql://192.168.1.7:3306/huimv_farm_v2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");
  41. dsc.setDriverName("com.mysql.cj.jdbc.Driver");
  42. dsc.setUsername("root");
  43. dsc.setPassword("hm123465");
  44. mpg.setDataSource(dsc);
  45. // 包配置
  46. PackageConfig pc = new PackageConfig();
  47. // pc.setModuleName(scanner("模块名"));
  48. pc.setParent("com.huimv.common.template");
  49. pc.setEntity("entity");
  50. pc.setMapper("mapper");
  51. pc.setService("service");
  52. pc.setServiceImpl("service.impl");
  53. mpg.setPackageInfo(pc);
  54. // 自定义配置
  55. // InjectionConfig cfg = new InjectionConfig() {
  56. // @Override
  57. // public void initMap() {
  58. // // to do nothing
  59. // }
  60. // };
  61. // 如果模板引擎是 freemarker
  62. // String templatePath = "/templates/mapper.xml.ftl";
  63. // 如果模板引擎是 velocity
  64. // String templatePath = "/templates/mapper.xml.vm";
  65. // 自定义输出配置
  66. // List<FileOutConfig> focList = new ArrayList<>();
  67. // 自定义配置会被优先输出
  68. // focList.add(new FileOutConfig(templatePath) {
  69. // @Override
  70. // public String outputFile(TableInfo tableInfo) {
  71. // // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  72. // return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
  73. // + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  74. // }
  75. // });
  76. /*
  77. cfg.setFileCreate(new IFileCreate() {
  78. @Override
  79. public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
  80. // 判断自定义文件夹是否需要创建
  81. checkDir("调用默认方法创建的目录");
  82. return false;
  83. }
  84. });
  85. */
  86. // cfg.setFileOutConfigList(focList);
  87. // mpg.setCfg(cfg);
  88. // 配置模板
  89. TemplateConfig templateConfig = new TemplateConfig();
  90. // 配置自定义输出模板
  91. //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
  92. // templateConfig.setEntity("templates/entity2.java");
  93. // templateConfig.setService();
  94. // templateConfig.setController();
  95. templateConfig.setXml(null);
  96. mpg.setTemplate(templateConfig);
  97. // 策略配置
  98. StrategyConfig strategy = new StrategyConfig();
  99. strategy.setNaming(NamingStrategy.underline_to_camel);
  100. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  101. strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model");
  102. strategy.setEntityLombokModel(true);
  103. strategy.setRestControllerStyle(true);
  104. strategy.setEntityLombokModel(true);
  105. // 公共父类
  106. // strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
  107. // 写于父类中的公共字段
  108. // strategy.setSuperEntityColumns("id");
  109. strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
  110. strategy.setControllerMappingHyphenStyle(true);
  111. strategy.setTablePrefix(pc.getModuleName() + "_");
  112. // strategy.setTablePrefix("base_");
  113. mpg.setStrategy(strategy);
  114. mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  115. mpg.execute();
  116. }
  117. }