MyBatis Plus Generator

  1. <!-- pom -->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-generator</artifactId>
  5. <version>3.4.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.velocity</groupId>
  9. <artifactId>velocity</artifactId>
  10. <version>1.7</version>
  11. </dependency>
  1. import com.baomidou.mybatisplus.annotation.DbType;
  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. * @author fengyuhao
  12. * @Description
  13. */
  14. public class MpGenerator {
  15. final static String dirPath = System.getProperty("user.dir") + "/src/main/java";
  16. private static String[] tables = new String[]{"userdemo"}; // 配置生成指定的表
  17. private static DbType dbType = DbType.POSTGRE_SQL;
  18. private static String url = "jdbc:postgresql://localhost:5432/mybatisplus?useUnicode=true&" +
  19. "characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&" +
  20. "transformedBitIsBoolean=true&serverTimezone=GMT%2B8";
  21. private static String UserName = "postgres";
  22. private static String PassWord = "953598751";
  23. private static String DriverName = "org.postgresql.Driver";
  24. // private static DbType dbType = DbType.MYSQL;
  25. // private static String UserName = "root";
  26. // private static String PassWord = "123456";
  27. // private static String DriverName = "com.mysql.cj.jdbc.Driver";
  28. // private static String url ="jdbc:mysql://localhost:3306/map3d?characterEncoding=utf8&serverTimezone=UTC";
  29. public static void main(String[] args) {
  30. AutoGenerator mpg = new AutoGenerator();
  31. // 全局配置
  32. GlobalConfig gc = new GlobalConfig();
  33. gc.setOutputDir(dirPath);
  34. gc.setAuthor("fyh");
  35. gc.setOpen(false); // 不打开文件夹
  36. gc.setFileOverride(true); //是否覆盖
  37. gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
  38. gc.setEnableCache(false);// XML 二级缓存
  39. gc.setBaseResultMap(true);// XML ResultMap
  40. gc.setBaseColumnList(true);// XML columList
  41. // 自定义文件命名,注意 %s 会自动填充表实体属性!
  42. gc.setMapperName("%sDao");
  43. // gc.setXmlName("%sMapper");
  44. gc.setServiceName("%sService");
  45. gc.setServiceImplName("%sServiceImpl");
  46. gc.setControllerName("%sController");
  47. mpg.setGlobalConfig(gc);
  48. // 数据源配置
  49. DataSourceConfig dsc = new DataSourceConfig();
  50. dsc.setDbType(dbType);
  51. dsc.setDriverName(DriverName);
  52. dsc.setUsername(UserName);//?serverTimezone=GMT%2B8
  53. dsc.setPassword(PassWord);
  54. dsc.setUrl(url);
  55. mpg.setDataSource(dsc);
  56. // 策略配置
  57. StrategyConfig strategy = new StrategyConfig();
  58. // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
  59. //strategy.setTablePrefix(new String[] { "tb_", "tsys_" });// 此处可以修改表前缀
  60. strategy.setEntityLombokModel(true);// 自动添加 lombok 注解
  61. strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
  62. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  63. strategy.setInclude(tables); // 需要生成的表
  64. // strategy.setExclude(new String[]{"test"}); // 排除生成的表
  65. // 自定义实体父类
  66. // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
  67. // 自定义实体,公共字段
  68. // strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
  69. // 自定义 mapper 父类
  70. // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
  71. // 自定义 service 父类
  72. strategy.setSuperServiceClass("com.baomidou.mybatisplus.extension.service.IService");
  73. // 自定义 service 实现类父类
  74. strategy.setSuperServiceImplClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl");
  75. // 自定义 controller 父类
  76. // strategy.setSuperControllerClass("com.baomidou.demo.TestController");
  77. // 【实体】是否生成字段常量(默认 false)
  78. // public static final String ID = "test_id";
  79. // strategy.setEntityColumnConstant(true);
  80. // 【实体】是否为构建者模型(默认 false)
  81. // public User setName(String name) {this.name = name; return this;}
  82. // strategy.setEntityBuilderModel(true);
  83. mpg.setStrategy(strategy);
  84. // 自定义配置
  85. InjectionConfig cfg = new InjectionConfig() {
  86. @Override
  87. public void initMap() {
  88. // to do nothing
  89. }
  90. };
  91. // 自定义输出配置
  92. List<FileOutConfig> focList = new ArrayList<>();
  93. // 自定义配置优先于默认配置生效
  94. String mapperXmlPath = "/templates/mapper.xml.vm";
  95. focList.add(new FileOutConfig(mapperXmlPath) {
  96. @Override
  97. public String outputFile(TableInfo tableInfo) {
  98. // 自定义xml 文件名和生成路径
  99. return System.getProperty("user.dir") + "/src/main/resources/mapper/" + tableInfo.getEntityName()+"Mapper" + StringPool.DOT_XML;
  100. }
  101. });
  102. cfg.setFileOutConfigList(focList);
  103. mpg.setCfg(cfg);
  104. // 包配置
  105. PackageConfig pc = new PackageConfig();
  106. pc.setParent("com.cug.seckill")
  107. // .setModuleName("module")
  108. .setController("controller")
  109. .setEntity("entity")
  110. .setMapper("mapper")
  111. .setService("service")
  112. .setServiceImpl("service.impl");
  113. // pc.setXml("mapper");
  114. mpg.setPackageInfo(pc);
  115. // 执行生成
  116. mpg.execute();
  117. }
  118. }