manven多模块+mybatisplus自动生成代码 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.bizmatics.fiveep.controller;
  2. import com.baomidou.mybatisplus.generator.AutoGenerator;
  3. import com.baomidou.mybatisplus.generator.config.*;
  4. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  5. import java.io.File;
  6. /**
  7. * @author yq
  8. * @date 2021/7/5 16:38
  9. */
  10. public class TestUtils {
  11. public static void main(String[] args) {
  12. String[] models = {"fiveep-controller", "fiveep-service", "fiveep-model", "fiveep-persistence"};
  13. for (String model : models) {
  14. shell(model);
  15. }
  16. }
  17. private static void shell(String model) {
  18. AutoGenerator mpg = new AutoGenerator();
  19. //1、全局配置
  20. GlobalConfig gc = new GlobalConfig();
  21. File file = new File(model);
  22. String path = file.getAbsolutePath();
  23. gc.setOutputDir(path + "/src/main/java"); //生成路径(一般都是生成在此项目的src/main/java下面)
  24. gc.setAuthor("ya"); //设置作者
  25. gc.setOpen(false);
  26. gc.setFileOverride(true); //第二次生成会把第一次生成的覆盖掉
  27. gc.setServiceName("%sService"); //生成的service接口名字首字母是否为I,这样设置就没有
  28. gc.setBaseResultMap(true); //生成resultMap
  29. mpg.setGlobalConfig(gc);
  30. //2、数据源配置
  31. DataSourceConfig dsc = new DataSourceConfig();
  32. dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
  33. dsc.setDriverName("com.mysql.jdbc.Driver");
  34. dsc.setUsername("root");
  35. dsc.setPassword(null);
  36. mpg.setDataSource(dsc);
  37. // 3、包配置
  38. PackageConfig pc = new PackageConfig();
  39. pc.setParent("com.bizmatics");
  40. pc.setController("fiveep.controller.web");
  41. pc.setEntity("model");
  42. pc.setMapper("persistence.mapper");
  43. pc.setService("service");
  44. pc.setServiceImpl("service.impl");
  45. //pc.setModuleName("test");
  46. mpg.setPackageInfo(pc);
  47. // 4、策略配置
  48. StrategyConfig strategy = new StrategyConfig();
  49. strategy.setNaming(NamingStrategy.underline_to_camel);
  50. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  51. // strategy.setSuperControllerClass("com.lcy.demo.sys.controller.BaseController");
  52. // strategy.setSuperEntityClass("com.lcy.demo.sys.entity.BaseEntity");
  53. // strategy.setTablePrefix("t_"); // 表名前缀
  54. strategy.setEntityLombokModel(true); //使用lombok
  55. strategy.setInclude("test"); // 逆向工程使用的表 如果要生成多个,这里可以传入String[]
  56. mpg.setStrategy(strategy);
  57. // 关闭默认 xml 生成,调整生成 至 根目录
  58. TemplateConfig tc = new TemplateConfig();
  59. if ("fiveep-persistence".equals(model)) {
  60. tc.setController(null);
  61. tc.setEntity(null);
  62. tc.setService(null);
  63. tc.setServiceImpl(null);
  64. } else if ("fiveep-model".equals(model)) {
  65. tc.setController(null);
  66. tc.setService(null);
  67. tc.setServiceImpl(null);
  68. tc.setMapper(null);
  69. tc.setXml(null);
  70. } else if ("fiveep-service".equals(model)) {
  71. tc.setController(null);
  72. tc.setMapper(null);
  73. tc.setXml(null);
  74. tc.setEntity(null);
  75. } else if ("fiveep-controller".equals(model)) {
  76. tc.setMapper(null);
  77. tc.setXml(null);
  78. tc.setService(null);
  79. tc.setServiceImpl(null);
  80. tc.setEntity(null);
  81. }
  82. mpg.setTemplate(tc);
  83. //5、执行
  84. mpg.execute();
  85. }
  86. }
  87. 参考链接
  88. https://blog.csdn.net/u010559460/article/details/103945985/
  89. https://blog.csdn.net/qq_32331997/article/details/74908570