mybatisplus使用generator

向右看齐 2022-09-11 10:17 289阅读 0赞

mybatisplus使用generator生成代码

mybatis-plus代码生成器,生成实体,mapper,mapper.xml,service,serviceImpl,controller
演示例子,执行 main 方法控制台输入表名回车自动生成对应项目目录中(目录要需要自行修改)

所需要maven依赖

  1. <!-- 引入mybatis-plus-generator依赖 -->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-generator</artifactId>
  5. <version>3.3.2</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-freemarker</artifactId>
  10. </dependency>

代码 如下
配置数据库连接,修改输出文件的包名,执行main方法,控制台输入表名 回车
根据自己的需求修改包名

  1. package com.ruoyi.generator;
  2. import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  3. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  4. import com.baomidou.mybatisplus.generator.AutoGenerator;
  5. import com.baomidou.mybatisplus.generator.InjectionConfig;
  6. import com.baomidou.mybatisplus.generator.config.*;
  7. import com.baomidou.mybatisplus.generator.config.po.TableInfo;
  8. import com.baomidou.mybatisplus.generator.config.rules.DateType;
  9. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  10. import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
  11. import org.apache.commons.lang3.StringUtils;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Scanner;
  15. /**
  16. * mybatis-plus代码生成器,生成实体,mapper,mapper.xml,service,serviceImpl,controller
  17. * 演示例子,执行 main 方法控制台输入表名回车自动生成对应项目目录中(目录要需要自行修改)
  18. */
  19. public class CodeGenerator {
  20. /**
  21. * <p>
  22. * 读取控制台内容
  23. * </p>
  24. */
  25. public static String scanner(String tip) {
  26. Scanner scanner = new Scanner(System.in);
  27. StringBuilder help = new StringBuilder();
  28. help.append("请输入" + tip + ":");
  29. System.out.println(help.toString());
  30. if (scanner.hasNext()) {
  31. String ipt = scanner.next();
  32. if (StringUtils.isNotEmpty(ipt)) {
  33. return ipt;
  34. }
  35. }
  36. throw new MybatisPlusException("请输入正确的" + tip + "!");
  37. }
  38. public static void main(String[] args) {
  39. // 代码生成器
  40. AutoGenerator mpg = new AutoGenerator();
  41. // 全局配置
  42. GlobalConfig gc = new GlobalConfig();
  43. //获取项目路径 及代码生成器所在的项目路径
  44. String projectPath = System.getProperty("user.dir");
  45. //模块地址
  46. String modules = "/ruoyi-modules/ruoyi-gov";
  47. //设置输出文件路径
  48. gc.setOutputDir(projectPath +modules+"/src/main/java");
  49. //作者
  50. gc.setAuthor("lcc");
  51. //执行完 是否打开输出的目录,默认true
  52. gc.setOpen(false);
  53. //覆盖已有的文件,默认false(第一次生成时放开)
  54. // gc.setFileOverride(true);
  55. gc.setBaseResultMap(true);
  56. gc.setBaseColumnList(true);
  57. // 设置日期类型为Date(若不设置时间类型都会变成LocalDateTime部分连接池例如druid是无法识别的)
  58. gc.setDateType(DateType.ONLY_DATE);
  59. mpg.setGlobalConfig(gc);
  60. // 数据源配置
  61. DataSourceConfig dsc = new DataSourceConfig();
  62. dsc.setUrl("jdbc:mysql://localhost:3306/ry-cloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
  63. dsc.setDriverName("com.mysql.jdbc.Driver");
  64. dsc.setUsername("root");
  65. dsc.setPassword("root");
  66. mpg.setDataSource(dsc);
  67. // 包配置
  68. PackageConfig pc = new PackageConfig();
  69. //模块名称
  70. //pc.setModuleName(scanner("模块名"));
  71. //父包
  72. pc.setParent("com.ruoyi.test");
  73. //自定义实体包名(不同的模块自己手动修改)
  74. pc.setEntity("entity");
  75. //自定义mapper包名(不同的模块自己手动修改)
  76. pc.setMapper("mapper");
  77. //自定义mapper.xml包名(不同的模块自己手动修改)
  78. pc.setXml("mapper.xmls");
  79. //自定义service包名(不同的模块自己手动修改)
  80. pc.setService("service");
  81. //自定义serviceImpl包名(不同的模块自己手动修改)
  82. pc.setServiceImpl("service.impl");
  83. //自定义controller包名(不同的模块自己手动修改)
  84. pc.setController("controller");
  85. mpg.setPackageInfo(pc);
  86. // 自定义配置
  87. InjectionConfig cfg = new InjectionConfig() {
  88. @Override
  89. public void initMap() {
  90. // to do nothing
  91. }
  92. };
  93. // 如果模板引擎是 freemarker
  94. String xmlPath = "/templates/mapper.xml.ftl";
  95. // 自定义输出配置
  96. List<FileOutConfig> focList = new ArrayList<>();
  97. // 自定义配置会被优先输出 例子如下
  98. focList.add(new FileOutConfig(xmlPath) {
  99. @Override
  100. public String outputFile(TableInfo tableInfo) {
  101. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  102. return projectPath +modules+ "/src/main/resources/mapper"
  103. + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  104. }
  105. });
  106. cfg.setFileOutConfigList(focList);
  107. mpg.setCfg(cfg);
  108. cfg.setFileOutConfigList(focList);
  109. mpg.setCfg(cfg);
  110. // 配置模板
  111. TemplateConfig templateConfig = new TemplateConfig();
  112. templateConfig.setXml(null);
  113. mpg.setTemplate(templateConfig);
  114. // 策略配置
  115. StrategyConfig strategy = new StrategyConfig();
  116. strategy.setNaming(NamingStrategy.underline_to_camel);
  117. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  118. //是否为lombok模型,默认为false
  119. strategy.setEntityLombokModel(true);
  120. //前后端分离时可开启
  121. // strategy.setRestControllerStyle(true);
  122. strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
  123. //RequestMapping驼峰转连字符
  124. // strategy.setControllerMappingHyphenStyle(true);
  125. //生成实体时生成生成数据库字段注解
  126. strategy.setEntityTableFieldAnnotationEnable(true);
  127. mpg.setStrategy(strategy);
  128. mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  129. mpg.execute();
  130. }
  131. }

控制台打印如图
在这里插入图片描述
生成文件如图
在这里插入图片描述

发表评论

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

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

相关阅读