要在项目下,新建一个module,把下面代码复制进去,配置好数据库连接字符串和账号密码,然后运行main,输入模块名,输入包路径,输入表名,就自动生产了。

    1. pom.xml配置



    org.springframework.boot
    spring-boot-starter-parent
    2.3.1.RELEASE




    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-test
    test



    org.projectlombok
    lombok
    true



    com.baomidou
    mybatis-plus-boot-starter
    3.3.2



    com.baomidou
    mybatis-plus-generator
    3.3.2



    org.freemarker
    freemarker
    2.3.30

    <!—_swagger文档管理—>

    io.swagger
    swagger-annotations
    1.5.22



    mysql
    mysql-connector-java
    runtime

    _

    com.alibaba
    druid
    1.1.20

    1. applicaiton.xml配置

      spring:
      datasource:
      name: mysql-connect
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/fxq-api?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
      mybatis:
      mapper-locations: classpath:mapping/*Mapper.xml
      type-aliases-package: org.convert.pdfconvert.model.entity


    3. 自动生成代码

    1. import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
    2. import com.baomidou.mybatisplus.core.toolkit.StringPool;
    3. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
    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.NamingStrategy;
    9. import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    10. import java.io.File;
    11. import java.util.ArrayList;
    12. import java.util.List;
    13. import java.util.Scanner;
    14. /**
    15. * @program: codeduck-auth
    16. * @description: mybatis-plus代码生成器
    17. * @author: wty
    18. * @create: 2020-09-27 15:27
    19. **/
    20. public class CodeGenerateUtil {
    21. /**
    22. * 读取控制台内容
    23. */
    24. public static String scanner(String tip) {
    25. Scanner scanner = new Scanner(System.in);
    26. StringBuilder help = new StringBuilder();
    27. help.append("请输入" + tip + ":");
    28. System.out.println(help.toString());
    29. if (scanner.hasNext()) {
    30. String ipt = scanner.next();
    31. if (StringUtils.isNotEmpty(ipt)) {
    32. return ipt;
    33. }
    34. }
    35. throw new MybatisPlusException("请输入正确的" + tip + "!");
    36. }
    37. public static void main(String[] args) {
    38. // 代码生成器
    39. AutoGenerator mpg = new AutoGenerator();
    40. // 数据源配置
    41. DataSourceConfig dsc = new DataSourceConfig();
    42. dsc.setUrl("jdbc:mysql://localhost:3306/fxq-api?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
    43. // dsc.setSchemaName("public");
    44. dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    45. dsc.setUsername("root");
    46. dsc.setPassword("root");
    47. mpg.setDataSource(dsc);
    48. String projectPath = System.getProperty("user.dir");
    49. String module_name = scanner("请输入要生成代码的模块名字:");
    50. File module_url = new File(projectPath + "/" + module_name);
    51. if(!module_url.exists()){
    52. System.out.println("输入的模块不存在!");
    53. return;
    54. }
    55. String page_name = scanner("请输入模块的包全路径,例如:org.wty.mybatis.codercreate,根据自己的包路径正确输入:");
    56. String[] page_name_array = page_name.split("\\.");
    57. System.out.println(page_name_array[0]+page_name_array[1]+page_name_array[2]);
    58. String str = "";
    59. int n = page_name_array.length;
    60. for(int i = 0; i < n; i++){
    61. str += page_name_array[i];
    62. if(i != n -1) str = str + "/";
    63. }
    64. File page_url = new File(projectPath + "/" + module_name + "/src/main/java/" + str);
    65. if(!page_url.exists()){
    66. System.out.println("输入的包名路径不正确,请检查模块名对应的包路径!");
    67. return;
    68. }
    69. // 全局配置
    70. GlobalConfig gc = new GlobalConfig();
    71. gc.setOutputDir(projectPath + "/" + module_name +"/src/main/java");
    72. // gc.setOutputDir("D:\\test");
    73. gc.setAuthor("wty");
    74. gc.setOpen(false);
    75. gc.setSwagger2(true); // 实体属性 Swagger2 注解
    76. gc.setServiceName("%sService");
    77. mpg.setGlobalConfig(gc);
    78. // 包配置
    79. PackageConfig pc = new PackageConfig();
    80. pc.setModuleName(null);
    81. pc.setParent(page_name);
    82. mpg.setPackageInfo(pc);
    83. // 自定义配置
    84. InjectionConfig cfg = new InjectionConfig() {
    85. @Override
    86. public void initMap() {
    87. // to do nothing
    88. }
    89. };
    90. // 如果模板引擎是 freemarker
    91. String templatePath = "/templates/mapper.xml.ftl";
    92. // 如果模板引擎是 velocity
    93. // String templatePath = "/templates/mapper.xml.vm";
    94. // 自定义输出配置
    95. List<FileOutConfig> focList = new ArrayList<>();
    96. // 自定义配置会被优先输出
    97. focList.add(new FileOutConfig(templatePath) {
    98. @Override
    99. public String outputFile(TableInfo tableInfo) {
    100. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
    101. return projectPath + "/" + module_name + "/src/main/resources/mapper/"
    102. + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
    103. }
    104. });
    105. cfg.setFileOutConfigList(focList);
    106. mpg.setCfg(cfg);
    107. // 配置模板
    108. TemplateConfig templateConfig = new TemplateConfig();
    109. templateConfig.setXml(null);
    110. mpg.setTemplate(templateConfig);
    111. // 策略配置
    112. StrategyConfig strategy = new StrategyConfig();
    113. strategy.setNaming(NamingStrategy.underline_to_camel);
    114. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    115. strategy.setEntityLombokModel(true);
    116. strategy.setRestControllerStyle(true);
    117. strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    118. strategy.setControllerMappingHyphenStyle(true);
    119. strategy.setTablePrefix("tb_"); // 设置表前缀
    120. mpg.setStrategy(strategy);
    121. mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    122. mpg.execute();
    123. }
    124. }