content: package com.ld.poetry.utils;
  
  import com.baomidou.mybatisplus.annotation.DbType;
  import com.baomidou.mybatisplus.annotation.IdType;
  import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  import com.baomidou.mybatisplus.generator.AutoGenerator;
  import com.baomidou.mybatisplus.generator.config.*;
  import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  
  import java.util.Scanner;
  
  public class CodeGenerator {
  
      public static String scanner(String tip) {
          Scanner scanner = new Scanner(System.in);
          StringBuilder help = new StringBuilder();
          help.append("请输入" + tip + "：");
          System.out.println(help.toString());
          if (scanner.hasNext()) {
              String ipt = scanner.next();
              if (StringUtils.isNotBlank(ipt)) {
                  return ipt;
              }
          }
          throw new MybatisPlusException("请输入正确的" + tip + "！");
      }
  
      public static void main(String[] args) {
          // 代码生成器
          AutoGenerator mpg = new AutoGenerator();
  
          // 全局配置
          GlobalConfig gc = new GlobalConfig();
          String projectPath = System.getProperty("user.dir");
          gc.setOutputDir(projectPath + "\\src\\main\\java");
          gc.setAuthor("sara");
          gc.setIdType(IdType.AUTO);
          gc.setOpen(false);
          gc.setBaseResultMap(true);
          gc.setBaseColumnList(true);
          gc.setFileOverride(false);
          gc.setServiceName("%sService");
          mpg.setGlobalConfig(gc);
  
          mpg.setDataSource(saraDataSourceConfig());
  
          // 包配置
          PackageConfig pc = new PackageConfig();
          pc.setParent("com.ld.poetry");
          pc.setMapper("dao");
          mpg.setPackageInfo(pc);
  
          // 配置模板
          TemplateConfig templateConfig = new TemplateConfig();
  //        templateConfig.setController(null);
  //        templateConfig.setService(null);
  //        templateConfig.setServiceImpl(null);
  //        templateConfig.setEntity(null);
  //        templateConfig.setMapper(null);
  //        templateConfig.setXml(null);
          mpg.setTemplate(templateConfig);
  
          // 策略配置
          StrategyConfig strategy = new StrategyConfig();
          strategy.setNaming(NamingStrategy.underline_to_camel);
          strategy.setColumnNaming(NamingStrategy.underline_to_camel);
          strategy.setEntityLombokModel(true);
          strategy.setRestControllerStyle(true);
          strategy.setEntityTableFieldAnnotationEnable(true);
  
          strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
          strategy.setLogicDeleteFieldName("deleted");
          mpg.setStrategy(strategy);
  
          mpg.execute();
      }
  
  
      private static DataSourceConfig saraDataSourceConfig() {
          return new DataSourceConfig()
                  .setDbType(DbType.MYSQL)
                  .setUrl("jdbc:mysql://ip:port/poetize?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai")
                  .setDriverName("com.mysql.cj.jdbc.Driver")
                  .setUsername("username")
                  .setPassword("password");
      }
  }
  