代码自动生成器非常好用,我们只需要提供我们数据库的表名,然后就可以让生成器自动帮我们完成各种代码的创建,整个过程非常清爽,可以说是加班人的福利!
xml<!--        mybatisplus代码生成依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.1</version>
        </dependency>
java
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.List;
/**
 * 描述:MybatisPlus代码生成器
 * @author lihaowei
 * @since: 2023/3/17 13:58
 * @return 
 */
public class CodeGenerator {
  public static void main(String[] args) {
    // 代码生成器的输出目录
    String outputDir = "D:\\yh\\spring-web\\api\\src\\main\\java";
    // 代码注释上的开发人员
    String author = "LIHAOWEI";
    // 数据库配置
    String url = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "123456";
    // mapper.xml所在物理路径
    String xmlPath = "D:\\yh\\spring-web\\provider\\src\\main\\resources\\mapper";
    // entity所在物理路径
    String entityPath = "D:\\yh\\spring-web\\api\\src\\main\\java\\com\\lhw\\entity";
    // 要生成代码的表名
    String[] tableNames = {"user"};
    // 1、创建代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 2、全局配置
    GlobalConfig gc = new GlobalConfig();
    // 生成文件的输出目录
    gc.setOutputDir(outputDir);
    // 是否覆盖已有文件,默认false
    gc.setFileOverride(false);
    // 是否打开输出目录,默认true
    gc.setOpen(false);
    // 是否在xml中添加二级缓存配置,默认false
    gc.setEnableCache(false);
    // 开发人员
    gc.setAuthor(author);
    // 是否开启swagger2模式,默认false
    gc.setSwagger2(true);
    // 是否开启ActiveRecord模式,默认false
    gc.setActiveRecord(false);
    // XML中是否开启BaseResultMap,默认false
    gc.setBaseResultMap(true);
    // XML中是否开启baseColumnList,默认false
    gc.setBaseColumnList(false);
    gc.setEntityName("%sEntity");
    // 去掉Service接口的首字母I
    //gc.setServiceName("%sService");
    // 配置时间类型对应策略,只使用java.util.Date包下的时间类型
    gc.setDateType(DateType.ONLY_DATE);
    mpg.setGlobalConfig(gc);
    // 3、数据源配置
    //oracle则调整dbtype和covert以及驱动
    //DataSourceConfig dsc = new DataSourceConfig();
    //dsc.setDbType(DbType.ORACLE);
    //dsc.setTypeConvert(new OracleTypeConvert());
    //dsc.setDriverName("oracle.jdbc.OracleDriver");
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setDbType(DbType.MYSQL);
    dsc.setTypeConvert(new MySqlTypeConvert());
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUrl(url);
    dsc.setUsername(username);
    dsc.setPassword(password);
 //   dsc.setSchemaName("MEDDOC"); //取oracle别的用户下的表
    mpg.setDataSource(dsc);
    // 4、包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent("com.lhw");
    pc.setEntity("entity");
    pc.setService("service");
    pc.setServiceImpl("service.impl");
    pc.setMapper("mapper");
    pc.setController("controller");
    mpg.setPackageInfo(pc);
    // 5、自定义配置:指定xml文件和entity文件所在目录
    InjectionConfig cfg = new InjectionConfig() {
      @Override
      public void initMap() {
      }
    };
    // 自定义输出配置
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定义配置会被优先输出
    focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
      @Override
      public String outputFile(TableInfo tableInfo) {
        return xmlPath + "\\" + tableInfo.getMapperName() + ".xml";
      }
    });
    focList.add(new FileOutConfig("/templates/entity.java.vm") {
      @Override
      public String outputFile(TableInfo tableInfo) {
        return entityPath + "\\" + tableInfo.getEntityName() + ".java";
      }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    // 停止默认的xml文件生成
    templateConfig.setXml(null);
    // 停止默认的entity文件生成
    templateConfig.setEntity(null);
    mpg.setTemplate(templateConfig);
    // 6、策略配置
    StrategyConfig strategy = new StrategyConfig();
    // Oracle默认大写,需设置为大写命名
    strategy.setCapitalMode(true);
    // 数据库表映射到实体的命名策略
    strategy.setNaming(NamingStrategy.underline_to_camel);
    // 数据库表字段映射到实体的命名策略
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    // 代码对应的表名,可同时生成多个表的代码
    strategy.setInclude(tableNames);
    // 生成 @RestController 控制器
    strategy.setRestControllerStyle(true);
    // 是否为lombok模型(默认 false)
    strategy.setEntityLombokModel(true);
    mpg.setStrategy(strategy);
    // 7、执行生成
    mpg.execute();
  }
}
本文作者:Weee
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!