当前位置:   article > 正文

Mybatis-Plus-Generator代码生成工具

mybatis-plus-generator

前提

      最近一直在开发新的项目,对于公司使用的代码生成器着实不爽,只生成model以及dao和xml,而且生成dao功能都是基于id操作的,但是实际业务都是基于biz_id,xml中如果发生表结构变动,改动起来额外容易发生问题。本着不将就的态度,结合自己上家公司的经验,想做一个基于模板的代码生成器,而且将生成xml和实际开发的xml分离开,以后表结构在发生变化的时候直接替换整个xml即可,最大程度做到开闭原则。

       之前打算采用上家公司的代码生成器,但是之前公司电脑配置都是windows,代码生成器是exe的,拿到了源码,里面一些路径问题也都是针对windows系统,但是当前大家都使用的mac,适配改造起来比较耗时,打算采用其他开源的替代。

        了解到mybatis-plus(MP)作为mybatis的加强版,在业界广受好评,而且MP其中很重要的一个功能是代码生成器是针对模板做,MyBatis-Plus 支持 Velocity(默认)、Freemarker、Beetl,用户可以选择自己熟悉的模板引擎,如果都不满足要求,可以采用自定义模板引擎。除此之外AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

MP在码云和github均有简单实用demo 具体地址https://gitee.com/baomidou/mybatis-plus-samples

下面小编展示一下搭建过程

1、创建一个springboot项目

2、引入MP相关jar,具体内容如下仅供参考

  1. <dependency>
  2. <groupId>org.apache.velocity</groupId>
  3. <artifactId>velocity-engine-core</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-jdbc</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-test</artifactId>
  12. <scope>test</scope>
  13. </dependency>
  14. <dependency>
  15. <groupId>com.baomidou</groupId>
  16. <artifactId>mybatis-plus-generator</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>mysql</groupId>
  20. <artifactId>mysql-connector-java</artifactId>
  21. <scope>runtime</scope>
  22. </dependency>

3、配置AutoGenerator代码生成相关配置

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

4、采用默认模板的代码,启动main即可生成

关于更多配置内容请参考https://mp.baomidou.com/guide/generator.html

我研究它主要是希望利用MP的代码生成器模板可以根据自定义的模板生成内容,后续做出了一定模样再来分享。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/231086
推荐阅读
  

闽ICP备14008679号