MybatisGenerator.java 4.4 KB

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