mybatis-plus 3.2.0代码生成器

官方教程

引入依赖

    
    
        mysql
        mysql-connector-java
        6.0.6
        runtime
    
    
    
        org.projectlombok
        lombok
        true
    
    
    
        org.springframework.boot
        spring-boot-starter-jdbc
    
    
        com.baomidou
        mybatis-plus-boot-starter
        3.2.0
    
    
        com.baomidou
        mybatis-plus-generator
        3.2.0
    
    
        com.baomidou
        mybatis-plus-core
        3.2.0
    
    
    
        org.springframework.boot
        spring-boot-starter-freemarker
    

编写代码

  /**
 * 代码生成器
 *
 * @Author jack
 * @Since 1.0 2019/11/24 15:08
 */
public class CodeGenerator {

private static final String OUTPUTDIR = "/src/main/java";
// 公共实体
private static final String SUPPER_ENTITY_CLASS = "com.jack006.common.BaseEntity";
// 公共父类
private static final String SUPPER_CONTROLLER_CLASS = "com.jack006.common.BaseController";

private static final String AUTHOR = "jack";
private static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DBURL = "jdbc:mysql://localhost:3306/learn?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
private static final String DBUSERNAME = "jack";
private static final String DBPASSWORD = "jack";


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+OUTPUTDIR);
    gc.setAuthor(AUTHOR);
    gc.setOpen(false);
    // 是否覆盖文件
    gc.setFileOverride(false);
    // 自定义文件名
    gc.setMapperName("%sMapper");
    gc.setServiceName("%sService");
    gc.setServiceImplName("%sServiceImpl");
    mpg.setGlobalConfig(gc);

    //数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setDriverName(DBDRIVER);
    dsc.setUrl(DBURL);
    dsc.setUsername(DBUSERNAME);
    dsc.setPassword(DBPASSWORD);
    mpg.setDataSource(dsc);

    //包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(scanner("模块名"));
    pc.setParent("com.jack006");
    pc.setEntity("entity");
    pc.setXml("mapper");
    pc.setService("service");
    mpg.setPackageInfo(pc);

    // 定义配置
    InjectionConfig cfg = new InjectionConfig() {
        @Override
        public void initMap() {
            // do something
        }
    };

    // 设置模板引擎
    String templatePath = "/templates/mapper.xml.ftl";

    // 自定义输出配置
    List 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);

    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setSuperEntityClass(SUPPER_ENTITY_CLASS);
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 公共父类
    strategy.setSuperControllerClass(SUPPER_CONTROLLER_CLASS);
    // 写于父类中的公共字段
    strategy.setSuperEntityColumns("id");
    strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
  }
}

使用

mybatis-plus 3.2.0代码生成器_第1张图片
image.png

生成效果

mybatis-plus 3.2.0代码生成器_第2张图片
image.png

你可能感兴趣的:(mybatis-plus 3.2.0代码生成器)