mybatis-plus代码生成器

Javid 1年前 ⋅ 2176 阅读

package com.zex.generator.util;

import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.ArrayList; import java.util.List;

/**

  • mybatis-plus 代码生成器

  • @author Javid */ 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.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");
     System.out.println(projectPath);
     gc.setOutputDir(projectPath + "/zex-marketing/src/main/java");
     gc.setAuthor("Javid");
     gc.setOpen(false);
     // gc.setSwagger2(true);
     mpg.setGlobalConfig(gc);
    
     // 数据源配置
     DataSourceConfig dsc = new DataSourceConfig();
     dsc.setUrl("jdbc:mysql://rds555x6bqh57m2oi295public.mysql.rds.aliyuncs.com:3306/yyh153?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
     // dsc.setSchemaName("public");
     dsc.setDriverName("com.mysql.cj.jdbc.Driver");
     dsc.setUsername("app_yyh153");
     dsc.setPassword("wDyTEA0tPT3");
     mpg.setDataSource(dsc);
    
     // 包配置
     PackageConfig pc = new PackageConfig();
     pc.setModuleName("coupon");
     pc.setParent("com.zex");
     mpg.setPackageInfo(pc);
    
     // 自定义配置
     InjectionConfig cfg = new InjectionConfig() {
         @Override
         public void initMap() {
             // to do nothing
         }
     };
    
     // 如果模板引擎是 freemarker
     // String templatePath = "/templates/mapper.xml.ftl";
     // 如果模板引擎是 velocity
     String templatePath = "/templates/mapper.xml.vm";
    
     // 自定义输出配置
     List<FileOutConfig> focList = new ArrayList<>();
     // 自定义配置会被优先输出
     focList.add(new FileOutConfig(templatePath) {
         @Override
         public String outputFile(TableInfo tableInfo) {
             // 自定义输出文件名
             return projectPath + "/zex-marketing/src/main/resources/mapper/" + pc.getModuleName()
                     + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
         }
     });
    
     cfg.setFileOutConfigList(focList);
     mpg.setCfg(cfg);
    
     // 配置模板
     TemplateConfig templateConfig = new TemplateConfig();
    
     // 配置自定义输出模板
     //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
     // templateConfig.setEntity("templates/entity2.java");
     // templateConfig.setService();
     // templateConfig.setController();
    
     templateConfig.setXml(null);
     mpg.setTemplate(templateConfig);
    
     // 策略配置
     StrategyConfig strategy = new StrategyConfig();
     strategy.setNaming(NamingStrategy.underline_to_camel);
     strategy.setColumnNaming(NamingStrategy.underline_to_camel);
     strategy.setSuperEntityClass("com.zex.common.base.BaseEntity");
     // 【实体】是否为lombok模型(默认 false), @Getter/@Setter/@ToString
     // strategy.setEntityLombokModel(true);
     // 生成 @RestController 控制器
     strategy.setRestControllerStyle(true);
     strategy.setSuperControllerClass("com.zex.core.web.controller.BaseController");
     strategy.setInclude(new String[]{"yyh_special_topic","yyh_special_topic_coupon"});
     strategy.setSuperEntityColumns(new String[] { "create_by", "create_time" ,"update_by","update_time","remark"});
     // 驼峰转连字符
     strategy.setControllerMappingHyphenStyle(true);
     // 表前缀
     // strategy.setTablePrefix(pc.getModuleName() + "_");
     strategy.setTablePrefix("yyh_");
     mpg.setStrategy(strategy);
     mpg.setTemplateEngine(new VelocityTemplateEngine());
     mpg.execute();
    

    }

}


全部评论: 0

    我有话说: