MybatisGeneratorUtils.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.usky.sas;
  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.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * @Description:
  12. * @Author: fu
  13. * @Date: 2026/03/01 14:44
  14. */
  15. public class MybatisGeneratorUtils {
  16. public static void main(String[] args) {
  17. shell("service-sas", "service-sas-biz");
  18. }
  19. private static void shell(String parentName, String model) {
  20. AutoGenerator mpg = new AutoGenerator();
  21. // 1、全局配置
  22. GlobalConfig gc = new GlobalConfig();
  23. // File file = new File(model);
  24. // String path = file.getAbsolutePath();
  25. String projectPath = System.getProperty("user.dir");
  26. projectPath += "/" + parentName;
  27. projectPath += "/" + model;
  28. gc.setOutputDir(projectPath + "/src/main/java"); // 生成路径(一般都是生成在此项目的src/main/java下面)
  29. // 修改为自己的名字
  30. gc.setAuthor("fu"); // 设置作者
  31. gc.setOpen(false);
  32. gc.setFileOverride(true); // 第二次生成会把第一次生成的覆盖掉
  33. gc.setServiceName("%sService"); // 生成的service接口名字首字母是否为I,这样设置就没有
  34. gc.setBaseResultMap(true); // 生成resultMap
  35. mpg.setGlobalConfig(gc);
  36. // 2、数据源配置
  37. // 修改数据源
  38. DataSourceConfig dsc = new DataSourceConfig();
  39. dsc.setUrl("jdbc:mysql://192.168.10.165:3306/usky-cloud?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
  40. dsc.setDriverName("com.mysql.jdbc.Driver");
  41. dsc.setUsername("root");
  42. dsc.setPassword("yt123456");
  43. mpg.setDataSource(dsc);
  44. // 3、包配置
  45. PackageConfig pc = new PackageConfig();
  46. pc.setParent("com.usky.sas");
  47. pc.setController("controller.web");
  48. pc.setEntity("domain");
  49. pc.setMapper("mapper");
  50. pc.setService("service");
  51. pc.setServiceImpl("service.impl");
  52. // pc.setXml("mapper.demo");
  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.usky.common.mybatis.core.CrudMapper");
  60. strategy.setSuperServiceClass("com.usky.common.mybatis.core.CrudService");
  61. strategy.setSuperServiceImplClass("com.usky.common.mybatis.core.AbstractCrudService");
  62. // strategy.setTablePrefix("t_"); // 表名前缀
  63. strategy.setEntityLombokModel(true); // 使用lombok
  64. // 修改自己想要生成的表
  65. strategy.setInclude("sas_system_type_code"); // 逆向工程使用的表 如果要生成多个,这里可以传入String[]
  66. mpg.setStrategy(strategy);
  67. // 关闭默认 xml 生成,调整生成 至 根目录
  68. // 修改对应的模块名称
  69. TemplateConfig tc = new TemplateConfig();
  70. // 自定义配置
  71. InjectionConfig cfg = new InjectionConfig() {
  72. @Override
  73. public void initMap() {
  74. // to do nothing
  75. }
  76. };
  77. // 如果模板引擎是 velocity
  78. String templatePath = "/templates/mapper.xml.vm";
  79. // 自定义输出配置
  80. List<FileOutConfig> focList = new ArrayList<>();
  81. // 自定义配置会被优先输出
  82. String finalProjectPath = projectPath;
  83. focList.add(new FileOutConfig(templatePath) {
  84. @Override
  85. public String outputFile(TableInfo tableInfo) {
  86. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  87. return finalProjectPath + "/src/main/resources/mapper/pm" + "/"
  88. + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  89. }
  90. });
  91. cfg.setFileOutConfigList(focList);
  92. mpg.setCfg(cfg);
  93. tc.setXml(null);
  94. mpg.setTemplate(tc);
  95. // 5、执行
  96. mpg.execute();
  97. }
  98. }