MyBatis Plus中代码生成器使用详解

按照官网上实例尝试了一下,感觉MyBatis plus中代码生成器还是很强大的,以下是测试的总结:

使用MybatisPlus的主要依赖

引入plus依赖(苞米豆)


  com.baomidou
  mybatis-plus-boot-starter
  3.1.1
  

生成器依赖


  com.baomidou
  mybatis-plus-generator
  3.1.1
 

模板依赖


  org.freemarker
  freemarker
  2.3.28
 

测试的pom依赖(也有我测试别东西的依赖,多余的请忽略)



 4.0.0
 
 org.springframework.boot
 spring-boot-starter-parent
 2.1.5.RELEASE
  
 
 com.wy
 testpuls
 0.0.1-SNAPSHOT
 testpuls
 Demo project for Spring Boot

 
 1.8
 

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

 
  mysql
  mysql-connector-java
  runtime
 
 
  org.projectlombok
  lombok
  true
 
 
  com.baomidou
  mybatis-plus-boot-starter
  3.1.1
 
 
  com.baomidou
  mybatis-plus-generator
  3.1.1
 
 
  org.freemarker
  freemarker
  2.3.28
 
 
  org.springframework.boot
  spring-boot-starter-test
  test
 
 
  junit
  junit
  4.12
  test
 
 
  org.springframework
  spring-test
  5.1.7.RELEASE
  compile
 
 
  junit
  junit
 
 
  org.springframework.boot
  spring-boot-test
 
 
  com.capgemini.mrchecker
  mrchecker-core-module
  4.12.1.1
 
 
  org.springframework.cloud
  spring-cloud-spring-service-connector
  2.0.4.RELEASE
 
 
  com.capgemini.mrchecker
  mrchecker-core-module
  4.12.1.1
 
 

 
 
  
  org.springframework.boot
  spring-boot-maven-plugin
  
 
 



代码生成器类

package com.wy.testpuls.util;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MyGenerator {
  public static String scanner(String someThing) {
    Scanner scanner = new Scanner(System.in);
    StringBuilder help = new StringBuilder();
    help.append("请输入" + someThing + ":");
    System.out.println(help.toString());
    if (scanner.hasNext()) {
      String sc = scanner.next();
      if (StringUtils.isNotEmpty(sc)) {
        return sc;
      }
    }
    throw new MybatisPlusException("请输入正确的" + someThing + "!");
  }

  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("山石");
    gc.setOpen(false);
    mpg.setGlobalConfig(gc);

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

    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(scanner("请输入你的包名"));
    pc.setParent("com.wy");//你哪个父目录下创建包
    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 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();

    // 配置自定义输出模板
    //指定自定义模板路径,注意不要带上.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);
    //自定义继承entity类,添加这一个会在生成实体类的时候继承entity
    //strategy.setSuperEntityClass("com.wy.testCodeGenerator.entity");
    //实体是否为lombok模型
    strategy.setEntityLombokModel(true);
    //生成@RestController控制器
    strategy.setRestControllerStyle(true);
    //是否继承controller
    // strategy.setSuperControllerClass("com.wy.testCodeGenerator.controller");
    strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setSuperEntityColumns("id");
    //驼峰转连字符串
    strategy.setControllerMappingHyphenStyle(true);
    //表前缀
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
  }
}

注意:测试时输入的表名必须和数据库中一致,区分大小写。

疑问:生成的实体类当中没有id。求解

到此这篇关于MyBatis Plus中代码生成器使用详解的文章就介绍到这了,更多相关MyBatis Plus代码生成器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(MyBatis Plus中代码生成器使用详解)