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

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