• Mybatisplus代码生成器主类CodeGenerator配置


    //代码自动生成
    public class CodeGenerator {
    /**
    * <p>
    * 读取控制台内容
    * </p>
    */
    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.isNotEmpty(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 + "/mybatisplus/src/main/java"); //生成文件输出目录
    gc.setAuthor("侯征");
    gc.setOpen(false);
    // gc.setSwagger2(true); 实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);

    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("123456");
    mpg.setDataSource(dsc);

    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(scanner("模块名"));
    pc.setParent("com.hou.mybatisplus");
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {
    @Override
    public void initMap() {
    // to do nothing
    }
    };
    // 如果模板引擎是 freemarker
    //String templatePath = "/templates/mapper.xml.ftl";
    // 自定义输出配置
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定义配置会被优先输出
    // focList.add(new FileOutConfig(templatePath) {
    // @Override
    // public String outputFile(TableInfo tableInfo) {
    // // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
    // return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
    // + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
    // }
    // });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg); //这个必须要,需要提供一个默认的

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);//表名生成策略
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);//实体字段生成策略
    strategy.setInclude(scanner("表名").split(",")); //需要生成的表
    //strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
    strategy.setEntityLombokModel(true);//使用lombook
    strategy.setRestControllerStyle(true);
    // 公共父类
    //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
    // 写于父类中的公共字段
    // strategy.setSuperEntityColumns("id");
    // strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    // strategy.setControllerMappingHyphenStyle(true);
    // strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
    }
    }

    运行main方法测试:

    生成文件:

  • 相关阅读:
    远程诊断DoIP
    基于linux内核包过滤技术的应用网关
    Boost内存池使用与测试
    C++ 编程规范
    大象——Thinking in UML
    C++ 创建类时常考虑的问题
    SLIP—串行线路上传输数据报的非标准协议
    神秘的程序员——编程的乐趣
    Bad Smell (代码的坏味道)
    模式与软件架构——软件架构的非功能特征
  • 原文地址:https://www.cnblogs.com/houzheng/p/11170714.html
Copyright © 2020-2023  润新知