MybatisPlus逆向生成工具类

package com.fhresource.crb.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;

public class MybatisPlusCodeGenerator {
    // ================= 自定义配置  =================
    private static final DbType DB_TYPE = DbType.MYSQL;
    private static final String JDBC_URL = "jdbc:mysql://ip: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 = "123456";
    /**
     * 生成文件根目录及包名
     */
    private static final String ROOT_DIR = "项目名称/src/main/java";
    private static final String PACKAGE_PARENT_NAME = "com.fhresource.crb";
    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.dto";
    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 = "LZY";
    /**
     * 需要生成代码的数据库表名
     */
    private static final String[] TABLES = new String[]{"t_ebook", "t_ebook_detail"
            , "t_label", "t_resource"};
    /**
     * 数据库表名前缀
     */
    private static final String[] TABLE_PREFIXS = new String[]{"t_"};


    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<String, Object> map = new HashMap<String, Object>();
                //map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-rb");
                this.setMap(map);
            }
        };
        //xml生成路径
        List<FileOutConfig> 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;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值