Springboot中mybatis-plus代码生成器配置

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

适用版本:mybatis-plus-generator 3.5.1 以下版本

引入相关jar

   
 
            com.baomidou
            mybatis-plus-boot-starter
            3.4.1
        
 
        
            org.apache.velocity
            velocity-engine-core
            2.0
        
        
            com.baomidou
            mybatis-plus-generator
            3.4.1
        
        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            2.5.6
        
具体工具类(支持自定义模板)
package com;

import com.baomidou.mybatisplus.annotation.DbType;
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 org.junit.Test;

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

/**
 * @author wxw 逆向生成
 * @data 2022/4/18 17 :06
 * @description
 */
public class mpGenerator {
    @Test
   public void generator() {

        AutoGenerator generator = new AutoGenerator();

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai&serverTimezone=UTC");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");

        // 数据源
        generator.setDataSource(dataSourceConfig);

        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        //是否自动配置swagger
        globalConfig.setSwagger2(true);
        //用户备注
        globalConfig.setAuthor("xw");
        //是否覆盖
        globalConfig.setFileOverride(true);
        //是否打开资源管理
        globalConfig.setOpen(false);
        globalConfig.setEntityName("%s");
        globalConfig.setControllerName("%sController");
        globalConfig.setServiceName("I%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setMapperName("%sMapper");
        //自己配置xml默认BaseResultMap
        globalConfig.setBaseResultMap(true);
         //自己配置xml默认BaseColumnList
        globalConfig.setBaseColumnList(true);
        // 全局配置
        generator.setGlobalConfig(globalConfig);


        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.test.demo");
        pc.setController("controller");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setMapper("mapper");
        pc.setEntity("entity");


        generator.setPackageInfo(pc);

         // 如果模板引擎是 freemarker
         String templatePath = "/templates/mapper.xml.vm";
         // 如果模板引擎是 velocity
         // String templatePath = "/templates/mapper.xml.vm";
       // 自定义配置
         InjectionConfig cfg = new InjectionConfig() {
              @Override
              public void initMap() {
                   // to do nothing
              }
         };
         // 自定义输出配置
         List focList = new ArrayList<>();
         // 自定义配置会被优先输出
         focList.add(new FileOutConfig(templatePath) {
              @Override
              public String outputFile(TableInfo tableInfo) {
                   // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                   return System.getProperty("user.dir") + "/src/main/resources/mapping/" + pc.getModuleName()
                           + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
              }
         });


         cfg.setFileOutConfigList(focList);
         generator.setCfg(cfg);

         // 配置模板
//         TemplateConfig templateConfig = new TemplateConfig();
//         templateConfig.setController("templates/controller.java");
//
//         templateConfig.setXml(null);
//         generator.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("t_class");
         //表名下划线转驼峰
        strategy.setNaming(NamingStrategy.underline_to_camel);

        //列名下划线转驼峰
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //自动lombok
        strategy.setEntityLombokModel(true);
        //生成控制器
        strategy.setRestControllerStyle(true);
        //取消前缀
        strategy.setTablePrefix("t_");
        generator.setStrategy(strategy);
        generator.execute();
    }

}

该配置中mapper.xml.vm可以自行去mybaitspuls-generator包里面找Springboot中mybatis-plus代码生成器配置_第1张图片

你可能感兴趣的:(java-spring,mybatis,java,spring,boot)