mybatis-plus逆向工程生成代碼示例

 

pom.xml

<velocity-engine-core.version>2.1</velocity-engine-core.version>
<mybatis-plus.version>2.2.0</mybatis-plus.version>


<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>${velocity-engine-core.version}</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>${mybatis-plus.version}</version>
</dependency>

 

Coding

MybatisPlusGenerator

package com.finance.www.config;

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.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author 954L
 * @create 2020/3/6 13:21
 */
public class MybatisPlusGenerator {
    
    /**配置文件信息*/
    private static final ResourceBundle RB_MYBATIS_PLUS = ResourceBundle.getBundle("mybatis-plus");

    /**java文件目錄*/
    private static final String JAVA_SOURCE_PATH = System.getProperty("user.dir") + "/src/main/java";

    /**xml映射文件目錄*/
    private static final String XML_SOURCE_PATH = System.getProperty("user.dir") + "/src/main/resources/mapper/";

    public static void main(String[] args) throws InterruptedException {
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        mpg.setGlobalConfig(getGlobalConfig(JAVA_SOURCE_PATH));

        // 數據源配置
        mpg.setDataSource(getDataSourceConfig());

        // 數據庫表配置
        mpg.setStrategy(getStrategyConfig(RB_MYBATIS_PLUS.getString("tableNames").split(",")));

        // 配置包結構
        mpg.setPackageInfo(getPackageConfig(RB_MYBATIS_PLUS.getString("controller.package"),
                RB_MYBATIS_PLUS.getString("service.package"), RB_MYBATIS_PLUS.getString("service.impl.package"),
                RB_MYBATIS_PLUS.getString("entity.package"), RB_MYBATIS_PLUS.getString("mapper.package")));

        // mapper.xml配置
        mpg.setCfg(getInjectionConfig());

        // 刪除默認的xml生成
        TemplateConfig tc = new TemplateConfig();
        tc.setXml(null);
        mpg.setTemplate(tc);

        // 執行
        mpg.execute();
    }

    private static InjectionConfig getInjectionConfig(){
        // 注入自定義配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<String, Object>(2);
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-RB_MYBATIS_PLUS");
                this.setMap(map);
            }
        };

        // 修改xml生成目錄
        List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return XML_SOURCE_PATH + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        return cfg;
    }

    private static PackageConfig getPackageConfig(String controllerPackage,
                                                  String servicPackage,
                                                  String serviceImplPackage,
                                                  String entityPackage,
                                                  String mapperPackage){
        PackageConfig pc = new PackageConfig();
        pc.setParent(RB_MYBATIS_PLUS.getString("parent.package"));
        if (StringUtils.isNotBlank(controllerPackage))
            pc.setController(controllerPackage);
        if (StringUtils.isNotBlank(servicPackage))
            pc.setService(servicPackage);
        if (StringUtils.isNotBlank(serviceImplPackage))
            pc.setServiceImpl(serviceImplPackage);
        if (StringUtils.isNotBlank(entityPackage))
            pc.setEntity(entityPackage);
        if (StringUtils.isNotBlank(mapperPackage))
            pc.setMapper(mapperPackage);
        return pc;
    }

    private static StrategyConfig getStrategyConfig(String... tableNames){
        StrategyConfig strategy = new StrategyConfig();
        // 駝峯
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setInclude(tableNames);
        return strategy;
    }

    private static DataSourceConfig getDataSourceConfig(){
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName(RB_MYBATIS_PLUS.getString("mysql.driver"));
        dsc.setUrl(RB_MYBATIS_PLUS.getString("mysql.url"));
        dsc.setUsername(RB_MYBATIS_PLUS.getString("mysql.user"));
        dsc.setPassword(RB_MYBATIS_PLUS.getString("mysql.pwd"));
        return dsc;
    }

    private static GlobalConfig getGlobalConfig(String outPutDir){
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(outPutDir);
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setAuthor(RB_MYBATIS_PLUS.getString("author"));
        // 自定義文件命名,%s:文件名
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("I%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        return gc;
    }

}

 

mybatis-plus.properties


author=954L

#需要逆向生成的表名:多個用“,”號分割
tableNames=table_table

#父包名
parent.package=com.finance.www
#代碼分層包名     留空:不生成代碼
controller.package=app.web
service.package=app.service
service.impl.package=app.service.impl
entity.package=data.orm
mapper.package=data.mappers

#數據庫連接信息
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://127.0.0.1:3306/financedata
mysql.user=root
mysql.pwd=password

 

 

對你有幫助的話,右上角給個讚唄~

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章