Mybatis Mybatis-plus 代码生成器工具类

依赖包:

mybatis-plus-boot-starter:2.2.0
mybatis-plus-generator:3.1.2
freemarker:2.3.28
lombok:1.18.8
velocity-engine-core:2.0

package com.cj.util;

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.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Mybatis-plus 代码生成器
 * 
 * @author CJ 
 */
public class MybatisPlusCodeGenerator {
    // ================= 自定义配置  =================
    private static final DbType DB_TYPE = DbType.MYSQL;
    private static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/***?characterEncoding=UTF-8&useSSL=false";
    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static final String JDBC_USERNAME = "root";
    private static final String JDBC_PASSWORD = "****";
    /**
     * 生成文件根目录及包名
     */
    private static final String ROOT_DIR = "server/src/main/java";
    private static final String PACKAGE_PARENT_NAME = "com.cj";
    private static final String PACKAGE_CONTROLLER_NAME = "controller";
    private static final String PACKAGE_SERVICE_NAME = "service";
    private static final String PACKAGE_SERVICEIMPL_NAME = "service.impl";
    private static final String PACKAGE_ENTITY_NAME = "entity";
    private static final String PACKAGE_MAPPER_NAME = "dao";
    private static final String RESOURCES_DIR = "server/src/main/resources";
    private static final String MAPPER_XML_PATH = "mapper";
    /**
     * 生成代码的@author
     */
    private static final String AUTHOR = "CJ";
    /**
     * 需要生成代码的数据库表名
     */
    private static final String[] TABLES = new String[]{"crm_customer", "crm_business_opportunity"
            , "crm_customer_contacts", "crm_customer_follow_up_record"};
    /**
     * 数据库表名前缀
     */
    private static final String[] TABLE_PREFIXS = new String[]{"crm_"};


    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        mpg.setDataSource(getDataSourceConfig());
        mpg.setGlobalConfig(getGlobalConfig());
        mpg.setStrategy(getStrategyConfig());
        mpg.setPackageInfo(getPackageConfig());
        mpg.setCfg(getInjectionConfig());
        mpg.setTemplate(getTemplateConfig());
        // 执行生成
        mpg.execute();
    }

    /**
     * 数据源配置
     *
     * @return
     */
    private static DataSourceConfig getDataSourceConfig() {
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DB_TYPE);
        dsc.setDriverName(JDBC_DRIVER);
        dsc.setUrl(JDBC_URL);
        dsc.setUsername(JDBC_USERNAME);
        dsc.setPassword(JDBC_PASSWORD);
        return dsc;
    }

    /**
     * 全局配置
     *
     * @return
     */
    private static GlobalConfig getGlobalConfig() {
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(ROOT_DIR);
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setAuthor(AUTHOR);
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        return gc;
    }

    /**
     * 生成策略配置
     *
     * @return
     */
    private static StrategyConfig getStrategyConfig() {
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix(TABLE_PREFIXS);// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setInclude(TABLES); // 需要生成的表
        strategy.setEntityLombokModel(true);
        return strategy;
    }

    /**
     * 生成包名设置
     *
     * @return
     */
    private static PackageConfig getPackageConfig() {
        // 4.生成文件所在包配置:
        PackageConfig pc = new PackageConfig();
        pc.setParent(PACKAGE_PARENT_NAME);
        pc.setController(PACKAGE_CONTROLLER_NAME);
        pc.setService(PACKAGE_SERVICE_NAME);
        pc.setServiceImpl(PACKAGE_SERVICEIMPL_NAME);
        pc.setEntity(PACKAGE_ENTITY_NAME);
        pc.setMapper(PACKAGE_MAPPER_NAME);
        return pc;
    }

    /**
     * xml文件配置
     *
     * @return
     */
    private static InjectionConfig getInjectionConfig() {
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map map = new HashMap();
                //map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-rb");
                this.setMap(map);
            }
        };
        //xml生成路径
        List focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return RESOURCES_DIR + "/" + MAPPER_XML_PATH + "/" + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        return cfg;
    }

    /**
     * 关闭默认 xml 生成
     *
     * @return
     */
    private static TemplateConfig getTemplateConfig() {
        TemplateConfig tc = new TemplateConfig();
        tc.setXml(null);
        return tc;
    }

}

你可能感兴趣的:(Mybatis Mybatis-plus 代码生成器工具类)