mybatis-plus 代碼生成


之前采用maven-generator 生成代碼,在配置文件上因為從單模塊到多模塊時出現問題,
改用mybatis-plus自動生成代碼。

碼雲地址:https://gitee.com/baomidou/mybatis-plus

github地址:https://github.com/baomidou/mybatis-plus

依賴

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>3.0.6</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
</dependency>

單模塊生成

public class Generator {

    public static void main(String[] args) {
        String [] tableNames = new String[]{"表名1","表名2"};

        String location = "代碼生成路徑位置";//例如: com/cn/jzedy
        generator(location,tableNames);
    }

    private static void generator(String location,String [] tableNames){

        GlobalConfig globalConfig = new GlobalConfig();// 全局配置
                globalConfig.setOpen(false)//是否打開輸出目錄 默認true
                        .setOutputDir(location)//生成文件的輸出目錄
                        .setFileOverride(true)//是否覆蓋已有文件 默認false
                        .setBaseResultMap(true)//開啟 BaseResultMap 默認false
                        .setBaseColumnList(true)//開啟 baseColumnList 默認false
                        .setActiveRecord(false)//開啟 ActiveRecord 模式 默認false
                        .setAuthor("Jzedy")//開發人員
                        .setServiceName("%sService");//service 命名方式 例如:%sBusiness 生成 UserBusiness

        
        DataSourceConfig dataSourceConfig = new DataSourceConfig();// 數據源配置
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setDriverName(Driver.class.getName())
                .setUsername("數據庫連接名稱")
                .setPassword("數據庫連接密碼")
                .setUrl("url地址");

        PackageConfig packageConfig = new PackageConfig();// 包配置
        packageConfig.setParent(location)
                .setEntity("entity")//Entity包名
                .setMapper("mapper")//mapper包名
                .setService("service")
                .setController("controller");

        StrategyConfig strategyConfig = new StrategyConfig();// 策略配置
                strategyConfig
                        .setCapitalMode(true)//駝峰命名
                        .setEntityLombokModel(false)//【實體】是否為lombok模型(默認 false)
                        .setRestControllerStyle(false)//生成 @RestController 控制器
                        .setNaming(NamingStrategy.underline_to_camel)//數據庫表映射到實體的命名策略,該處下划線轉駝峰命名
                        .setInclude(tableNames);//需要包含的表名,允許正則表達式(與exclude二選一配置)


        new AutoGenerator()//// 代碼生成器
                .setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig)
                .setPackageInfo(packageConfig)
                .setStrategy(strategyConfig)
                .execute();

    }


}

具體配置查閱https://mp.baomidou.com/

多模塊配置

因為項目分模塊時候 想代碼自動生成在對應位置,例如下面代碼 將entity mapper service代碼生成在
service模塊中,controller生成在web模塊中。可以自行模塊進一步細分,只是對代碼生成路徑在但模塊上
基礎上調整而已。同時下述代碼的代碼生成模板是采用mybatis-plus自己的模板,若需自定義模板不再此論述,

public class Generator {

    public static void main(String[] args) {
        String [] tableNames = new String[]{"users","roles"};

        String [] modules = new String[]{"service","web"};//項目模塊名,需自定義
        for (String module : modules) {
            moduleGenerator(module,tableNames);
        }
    }

    private static void moduleGenerator(String module,String [] tableNames){

        GlobalConfig globalConfig = getGlobalConfig(module);// 全局配置

        DataSourceConfig dataSourceConfig = getDataSourceConfig();// 數據源配置

        PackageConfig packageConfig = getPackageConfig(module);// 包配置

        StrategyConfig strategyConfig = getStrategyConfig(tableNames);// 策略配置

        TemplateConfig templateConfig = getTemplateConfig(module);// 配置模板

        new AutoGenerator()
                .setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig)
                .setPackageInfo(packageConfig)
                .setStrategy(strategyConfig)
                .setTemplate(templateConfig)
                .execute();

    }

    private static TemplateConfig getTemplateConfig(String module) {
        TemplateConfig templateConfig = new TemplateConfig();
        if ("service".equals(module)){
            templateConfig.setEntity(new TemplateConfig().getEntity(false))
                    .setMapper(new TemplateConfig().getMapper())//mapper模板采用mybatis-plus自己模板
                    .setXml(new TemplateConfig().getXml())
                    .setService(new TemplateConfig().getService())
                    .setServiceImpl(new TemplateConfig().getServiceImpl())
                    .setController(null);//service模塊不生成controller代碼
        }else if ("web".equals(module)){//web模塊只生成controller代碼
            templateConfig.setEntity(null)
                    .setMapper(null)
                    .setXml(null)
                    .setService(null)
                    .setServiceImpl(null)
                    .setController(new TemplateConfig().getController());
        }else throw new IllegalArgumentException("參數匹配錯誤,請檢查");
        return templateConfig;
    }

    private static StrategyConfig getStrategyConfig(String[] tableNames) {
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)//駝峰命名
                .setEntityLombokModel(false)
                .setRestControllerStyle(false)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(tableNames);
        return strategyConfig;
    }

    private static PackageConfig getPackageConfig(String module) {
        PackageConfig packageConfig = new PackageConfig();
        String packageName = "com.cn.jzedy";//不同模塊 代碼生成具體路徑自定義指定
        if ("service".equals(module)){
            packageName+=".web";
        }else if ("web".equals(module)){

        }
        packageConfig.setParent(packageName)
                .setEntity("entity")
                .setMapper("mapper")
                .setService("service")
                .setController("controller");
        return packageConfig;
    }

    private static DataSourceConfig getDataSourceConfig() {
        String dbUrl = "jdbc:mysql://localhost:3306/z-blogs";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setDriverName(Driver.class.getName())
                .setUsername("root")
                .setPassword("root")
                .setUrl(dbUrl);
        return dataSourceConfig;
    }

    private static GlobalConfig getGlobalConfig(String module) {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOpen(false)//new File(module).getAbsolutePath()得到模塊根目錄路徑,因事Maven項目,代碼指定路徑自定義調整
                .setOutputDir(new File(module).getAbsolutePath()+"/src/main/java")//生成文件的輸出目錄
                .setFileOverride(true)//是否覆蓋已有文件
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setActiveRecord(false)
                .setAuthor("Jzedy")
                .setServiceName("%sService");
        return globalConfig;
    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM