代码生成器

ゝ一纸荒年。 2024-03-16 21:08 159阅读 0赞

引入依赖

  1. <!-- 引入mp依赖-->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-boot-starter</artifactId>
  5. <version>3.3.2</version>
  6. </dependency>
  7. <!-- 引入mp代码生成器依赖-->
  8. <dependency>
  9. <groupId>com.baomidou</groupId>
  10. <artifactId>mybatis-plus-generator</artifactId>
  11. <version>3.3.2</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.apache.velocity</groupId>
  15. <artifactId>velocity-engine-core</artifactId>
  16. <version>2.3</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.freemarker</groupId>
  20. <artifactId>freemarker</artifactId>
  21. <version>2.3.29</version>
  22. </dependency>

要引入代码生成器

  1. // 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
  2. public class CodeGenerator {
  3. /**
  4. * <p>
  5. * 读取控制台内容
  6. * </p>
  7. */
  8. public static String scanner(String tip) {
  9. Scanner scanner = new Scanner(System.in);
  10. StringBuilder help = new StringBuilder();
  11. help.append("请输入").append(tip).append(":");
  12. System.out.println(help.toString());
  13. if (scanner.hasNext()) {
  14. String ipt = scanner.next();
  15. if (StringUtils.isNotBlank(ipt)) {
  16. return ipt;
  17. }
  18. }
  19. throw new MybatisPlusException("请输入正确的" + tip + "!");
  20. }
  21. public static void main(String[] args) {
  22. // 代码生成器
  23. AutoGenerator mpg = new AutoGenerator();
  24. // 全局配置
  25. GlobalConfig gc = new GlobalConfig();
  26. String projectPath = System.getProperty("user.dir");
  27. gc.setOutputDir(projectPath + "/src/main/java");
  28. gc.setAuthor("jobob");
  29. gc.setOpen(false);
  30. // gc.setSwagger2(true); 实体属性 Swagger2 注解
  31. mpg.setGlobalConfig(gc);
  32. // 数据源配置
  33. DataSourceConfig dsc = new DataSourceConfig();
  34. dsc.setUrl("jdbc:mysql://localhost:3306/ant?useUnicode=true&useSSL=false&characterEncoding=utf8");
  35. // dsc.setSchemaName("public");
  36. dsc.setDriverName("com.mysql.jdbc.Driver");
  37. dsc.setUsername("root");
  38. dsc.setPassword("密码");
  39. mpg.setDataSource(dsc);
  40. // 包配置
  41. PackageConfig pc = new PackageConfig();
  42. pc.setModuleName(scanner("模块名"));
  43. pc.setParent("com.baomidou.ant");
  44. mpg.setPackageInfo(pc);
  45. // 自定义配置
  46. InjectionConfig cfg = new InjectionConfig() {
  47. @Override
  48. public void initMap() {
  49. // to do nothing
  50. }
  51. };
  52. // 如果模板引擎是 freemarker
  53. String templatePath = "/templates/mapper.xml.ftl";
  54. // 如果模板引擎是 velocity
  55. // String templatePath = "/templates/mapper.xml.vm";
  56. // 自定义输出配置
  57. List<FileOutConfig> focList = new ArrayList<>();
  58. // 自定义配置会被优先输出
  59. focList.add(new FileOutConfig(templatePath) {
  60. @Override
  61. public String outputFile(TableInfo tableInfo) {
  62. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  63. return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
  64. + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  65. }
  66. });
  67. /*
  68. cfg.setFileCreate(new IFileCreate() {
  69. @Override
  70. public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
  71. // 判断自定义文件夹是否需要创建
  72. checkDir("调用默认方法创建的目录,自定义目录用");
  73. if (fileType == FileType.MAPPER) {
  74. // 已经生成 mapper 文件判断存在,不想重新生成返回 false
  75. return !new File(filePath).exists();
  76. }
  77. // 允许生成模板文件
  78. return true;
  79. }
  80. });
  81. */
  82. cfg.setFileOutConfigList(focList);
  83. mpg.setCfg(cfg);
  84. // 配置模板
  85. TemplateConfig templateConfig = new TemplateConfig();
  86. // 配置自定义输出模板
  87. //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
  88. // templateConfig.setEntity("templates/entity2.java");
  89. // templateConfig.setService();
  90. // templateConfig.setController();
  91. templateConfig.setXml(null);
  92. mpg.setTemplate(templateConfig);
  93. // 策略配置
  94. StrategyConfig strategy = new StrategyConfig();
  95. strategy.setNaming(NamingStrategy.underline_to_camel);
  96. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  97. strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
  98. strategy.setEntityLombokModel(true);
  99. strategy.setRestControllerStyle(true);
  100. // 公共父类
  101. strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
  102. // 写于父类中的公共字段
  103. strategy.setSuperEntityColumns("id");
  104. strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
  105. strategy.setControllerMappingHyphenStyle(true);
  106. strategy.setTablePrefix(pc.getModuleName() + "_");
  107. mpg.setStrategy(strategy);
  108. mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  109. mpg.execute();
  110. }
  111. }

注释:本文适用于老版本(mybatis-plus-generator 3.5.1 以下版本,3.5.1 及以上的请参考 代码生成器新)

发表评论

表情:
评论列表 (有 0 条评论,159人围观)

还没有评论,来说两句吧...

相关阅读

    相关 代码生成器

    如果你是一名后端开发者,那么大多数的工作一定是重复编写各种 CRUD(增删改查)代码。时间长了你会发现,这些工作不仅无趣,还会浪费你的很多时间,没有机会去做更有创造力和挑战的工