MybatisGeneratorUtils.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.bizmatics.controller.web;
  2. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  3. import com.baomidou.mybatisplus.generator.AutoGenerator;
  4. import com.baomidou.mybatisplus.generator.InjectionConfig;
  5. import com.baomidou.mybatisplus.generator.config.*;
  6. import com.baomidou.mybatisplus.generator.config.po.TableInfo;
  7. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  8. import java.io.File;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. /**
  12. * @author yq
  13. * @date 2021/7/6 11:42
  14. */
  15. public class TestUtils {
  16. public static void main(String[] args) {
  17. //修改成自己的模块名称
  18. String[] models = {"test-controller", "test-service", "test-model", "test-persistence"};
  19. for (String model : models) {
  20. shell(model);
  21. }
  22. }
  23. private static void shell(String model) {
  24. AutoGenerator mpg = new AutoGenerator();
  25. //1、全局配置
  26. GlobalConfig gc = new GlobalConfig();
  27. File file = new File(model);
  28. String path = file.getAbsolutePath();
  29. gc.setOutputDir(path + "/src/main/java"); //生成路径(一般都是生成在此项目的src/main/java下面)
  30. //修改为自己的名字
  31. gc.setAuthor("ya"); //设置作者
  32. gc.setOpen(false);
  33. gc.setFileOverride(true); //第二次生成会把第一次生成的覆盖掉
  34. gc.setServiceName("%sService"); //生成的service接口名字首字母是否为I,这样设置就没有
  35. gc.setBaseResultMap(true); //生成resultMap
  36. mpg.setGlobalConfig(gc);
  37. //2、数据源配置
  38. //修改数据源
  39. DataSourceConfig dsc = new DataSourceConfig();
  40. dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
  41. dsc.setDriverName("com.mysql.jdbc.Driver");
  42. dsc.setUsername("root");
  43. dsc.setPassword(null);
  44. mpg.setDataSource(dsc);
  45. // 3、包配置
  46. PackageConfig pc = new PackageConfig();
  47. pc.setParent("com.bizmatics");
  48. pc.setController("controller.web");
  49. pc.setEntity("model");
  50. pc.setMapper("persistence.mapper");
  51. pc.setService("service");
  52. pc.setServiceImpl("service.impl");
  53. //pc.setModuleName("test");
  54. mpg.setPackageInfo(pc);
  55. // 4、策略配置
  56. StrategyConfig strategy = new StrategyConfig();
  57. strategy.setNaming(NamingStrategy.underline_to_camel);
  58. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  59. strategy.setSuperMapperClass("com.bizmatics.common.mvc.base.CrudMapper");
  60. strategy.setSuperServiceClass("com.bizmatics.common.mvc.base.CrudService");
  61. strategy.setSuperServiceImplClass("com.bizmatics.common.mvc.base.AbstractCrudService");
  62. // strategy.setTablePrefix("t_"); // 表名前缀
  63. strategy.setEntityLombokModel(true); //使用lombok
  64. //修改自己想要生成的表
  65. strategy.setInclude("test"); // 逆向工程使用的表 如果要生成多个,这里可以传入String[]
  66. mpg.setStrategy(strategy);
  67. // 关闭默认 xml 生成,调整生成 至 根目录
  68. //修改对应的模块名称
  69. TemplateConfig tc = new TemplateConfig();
  70. if ("test-persistence".equals(model)) {
  71. tc.setController(null);
  72. tc.setEntity(null);
  73. tc.setService(null);
  74. tc.setServiceImpl(null);
  75. // 自定义配置
  76. InjectionConfig cfg = new InjectionConfig() {
  77. @Override
  78. public void initMap() {
  79. // to do nothing
  80. }
  81. };
  82. //如果模板引擎是 velocity
  83. String templatePath = "/templates/mapper.xml.vm";
  84. // 自定义输出配置
  85. List<FileOutConfig> focList = new ArrayList<>();
  86. // 自定义配置会被优先输出
  87. focList.add(new FileOutConfig(templatePath) {
  88. @Override
  89. public String outputFile(TableInfo tableInfo) {
  90. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  91. return path + "/src/main/resources/mapper/mysql/" + "/"
  92. + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  93. }
  94. });
  95. cfg.setFileOutConfigList(focList);
  96. mpg.setCfg(cfg);
  97. tc.setXml(null);
  98. } else if ("test-model".equals(model)) {
  99. tc.setController(null);
  100. tc.setService(null);
  101. tc.setServiceImpl(null);
  102. tc.setMapper(null);
  103. tc.setXml(null);
  104. } else if ("test-service".equals(model)) {
  105. tc.setController(null);
  106. tc.setMapper(null);
  107. tc.setXml(null);
  108. tc.setEntity(null);
  109. } else if ("test-controller".equals(model)) {
  110. tc.setMapper(null);
  111. tc.setXml(null);
  112. tc.setService(null);
  113. tc.setServiceImpl(null);
  114. tc.setEntity(null);
  115. }
  116. mpg.setTemplate(tc);
  117. //5、执行
  118. mpg.execute();
  119. }
  120. }