mybatis plus generator配置

mybatis plus generator配置

代码生成器AutoGenerator

AutoGenerator autoGenerator = new AutoGenerator();

GlobalConfig

全局配置,定义文件的输出目录,设置文件的@author信息等公共配置

GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");// 获取用户程序当前路径(项目根的路径)
gc.setOutputDir(projectPath + "/src/main/java");// 生成文件的输出目录(默认值:D 盘根目录)
gc.setAuthor("yourName");// 开发人员(默认值:null)
gc.setOpen(false);// 是否打开输出目录(默认值:null)
gc.setFileOverride(true);// 是否覆盖已有文件(默认值:false)
autoGenerator.setGlobalConfig(gc);//把全局配置添加到代码生成器主类

DataSourceConfig

DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://xxx.xxx.x.x:3306/xxx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8");
//dsc.setSchemaName("public");// 数据库方案名
dsc.setDbType(DbType.MYSQL);// 数据库类型
dsc.setDriverName("com.mysql.cj.jdbc.Driver");// 驱动名称
dsc.setUsername("xxx"); // 用户名
dsc.setPassword("xxx"); // 密码

autoGenerator.setDataSource(dsc);//把数据源配置添加到代码生成器主类

PackageConfig

包配置,就是每个类最上面的package,比如:package com.ck.mybatisplus.foundation.service;这里包配置可以分别配置service、entity、controller等等

PackageConfig pc = new PackageConfig();
pc.setModuleName("foundation");// 父包模块名
pc.setParent("com.ck.mybatisplus");// 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setService("service");// Service包名
pc.setEntity("entity");// Entity包名
pc.setServiceImpl("service.impl");// ServiceImpl包名
pc.setMapper("mapper");// Mapper包名
pc.setController("controller");// Contoller包名
// pc.setXml("mapper.xml");// Mapper.xml包名
mpg.setPackageInfo(pc);// 把包配置添加到代码生成器主类

TemplateConfig

方法一

设置自定义模板路径,文件配置信息使用默认的。(如:生成的文件名,生成的文件路径等),一般都是用mybatis plus中自带的模板

//不用带上.ftl/.vm, 会根据使用的模板引擎自动识别
templateConfig.setEntity("templates/entity.java");
templateConfig.setService("templates/service.java");
templateConfig.setController();
方法二

使用自定义输出配置


// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
    @Override
    public String outputFile(TableInfo tableInfo) {
		return projectPath + "/src/main/resources/mapper/mysql/" + pc.getModuleName()
    + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
    }
});

StrategyConfig

一些策略的配置

// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(INCLUDE_TABLES.split(","));
strategy.setControllerMappingHyphenStyle(true);
//  strategy.setTablePrefix(pc.getModuleName() + "_");
autoGenerator.setStrategy(strategy);
autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());

生成器工具类

自己写的一个生成器工具类,可以参考一下,根据自己的需求进行修改

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 com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.*;


/**
 * @author wenyao
 */
public class CodeGenerator {

    private final static AutoGenerator autoGenerator = new AutoGenerator();

    //表名映射(','号分割符)
    private static String INCLUDE_TABLES = "user";
	//private static String INCLUDE_TABLES = "user,home";
    //包名配置
    private static String CONTROLLER = "controller";
    private static String SERVICE = "service";
    private static String SERVICEIMPL = "service.Impl";
    private static String MAPPER = "mapper";
    private static String ENTITY = "entity";
    private static String projectPath = System.getProperty("user.dir");// 获取用户程序当前路径(项目根的路径)
    private static String ParentName = "com.wenyao";
    private static String ModuleName = "";

    //公共全局
    private static String AUTHOR = "wenyao";


    //数据源配置
    private static String DB_USERNAME = "root";
    private static String DB_PASSWORD = "xxx";
    private static String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static String DB_URL = "jdbc:mysql://localhost:3306/mybatis_plus_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";

    //xml模板文件路径
    private static String TEMPLATEPATH = "/templates/mapper.xml.ftl";

    //配置
    private static GlobalConfig gc = new GlobalConfig();
    private static DataSourceConfig dsc = new DataSourceConfig();
    private static PackageConfig pc = new PackageConfig();
    private static InjectionConfig cfg = new InjectionConfig() {
        @Override
        public void initMap() {
            // to do nothing
        }
    };
    private static TemplateConfig templateConfig = new TemplateConfig();
    private static StrategyConfig strategy = new StrategyConfig();



    /**
     * 初始化全局配置
     */
    private static void initGlobalConfig(){
        // 全局配置
        gc.setOutputDir(projectPath + "/src/main/java");// 生成文件的输出目录(默认值:D 盘根目录)
        gc.setAuthor(AUTHOR);// 开发人员(默认值:null)
        gc.setOpen(false);// 是否打开输出目录(默认值:null)
        gc.setFileOverride(true);// 是否覆盖已有文件(默认值:false)
        gc.setSwagger2(true);// 实体属性 Swagger2 注解
        gc.setBaseResultMap(true); //在xml中生成BaseResultMap
        gc.setBaseColumnList(true); //
        autoGenerator.setGlobalConfig(gc);//把全局配置添加到代码生成器主类
    }

    /**
     * 初始化数据源配置
     */
    private static void initDataSourceConfig(){
        // 数据源配置

        dsc.setUrl(DB_URL);
        //dsc.setSchemaName("public");// 数据库方案名
        dsc.setDbType(DbType.MYSQL);// 数据库类型
        dsc.setDriverName(DB_DRIVER);// 驱动名称
        dsc.setUsername(DB_USERNAME); // 用户名
        dsc.setPassword(DB_PASSWORD); // 密码
        autoGenerator.setDataSource(dsc);//把数据源配置添加到代码生成器主类
    }

    /**
     * 初始化包配置
     */
    private static void initPackageConfig(){
        // 包配置
        pc.setModuleName(ModuleName);// 父包模块名
        pc.setParent(ParentName);// 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
        pc.setService(SERVICE);// Service包名
        pc.setEntity(ENTITY);// Entity包名
        pc.setServiceImpl(SERVICEIMPL);// ServiceImpl包名
        pc.setMapper(MAPPER);// Mapper包名
        pc.setController(CONTROLLER);// Contoller包名
        // pc.setXml("mapper.xml");// Mapper.xml包名(自定义配置到resource下,所以这里不使用默认路径)
        autoGenerator.setPackageInfo(pc);// 把包配置添加到代码生成器主类
    }

    /**
     * 初始化自定义配置
     * 这里配置生成的mapper.xml文件的自定义输出路径(默认不是输出到resources文件夹下,所以需要自定义)
     */
    private static void initInjectionConfig(){
        // 自定义配置
        // 自定义输出配置,自定义配置会被优先输出
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig(TEMPLATEPATH) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectPath + "/src/main/resources/mapper/mysql/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        autoGenerator.setCfg(cfg);
    }


    /**
     * 初始化模板配置
     */
    private static void initTemplateConfig(){
        // 配置模板
        templateConfig.setXml(null);
        autoGenerator.setTemplate(templateConfig);
    }

    /**
     * 生成策略初始化
     */
    private static void initStrategyConfig(){
        // 策略配置

        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
        // strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        // 写于父类中的公共字段
        // strategy.setSuperEntityColumns("id");
        strategy.setInclude(INCLUDE_TABLES.split(","));
        strategy.setControllerMappingHyphenStyle(true);
        //  strategy.setTablePrefix(pc.getModuleName() + "_");
        autoGenerator.setStrategy(strategy);
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
    }

    public static void init(){
        initGlobalConfig();
        initDataSourceConfig();
        initPackageConfig();
        initInjectionConfig();
        initTemplateConfig();
        initStrategyConfig();
    }

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        init();
        autoGenerator.execute();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值