Search in sources :

Example 31 with InjectionConfig

use of com.baomidou.mybatisplus.generator.InjectionConfig in project seckill by yt-King.

the class CodeGenerator method main.

public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("yt");
    gc.setOpen(false);
    gc.setFileOverride(true);
    // gc.setSwagger2(true); //实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://47.96.23.31:3306/zhgx?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("123");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("com.yt.seckill");
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
        // to do nothing
        }
    };
    // 如果模板引擎是 freemarker
    String templatePath = "/templates/mapper.xml.ftl";
    // 如果模板引擎是 velocity
    // String templatePath = "/templates/mapper.xml.vm";
    // 自定义输出配置
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定义配置会被优先输出
    focList.add(new FileOutConfig(templatePath) {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
            return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
        }
    });
    /*
        cfg.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                // 判断自定义文件夹是否需要创建
                checkDir("调用默认方法创建的目录,自定义目录用");
                if (fileType == FileType.MAPPER) {
                    // 已经生成 mapper 文件判断存在,不想重新生成返回 false
                    return !new File(filePath).exists();
                }
                // 允许生成模板文件
                return true;
            }
        });
        */
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    // 配置自定义输出模板
    // 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
    // templateConfig.setEntity("templates/entity2.java");
    // templateConfig.setService();
    // templateConfig.setController();
    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);
    // 策略配置
    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(scanner("表名,多个英文逗号分割").split(","));
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
}
Also used : ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 32 with InjectionConfig

use of com.baomidou.mybatisplus.generator.InjectionConfig in project tutorials-java by Artister.

the class MysqlGenerator method main.

/**
 * RUN THIS
 */
public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/mybatis-plus-sample-generator/src/main/java");
    gc.setAuthor("jobob");
    gc.setOpen(false);
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/ant?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("1q2w3e4r");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(scanner("模块名"));
    pc.setParent("com.baomidou.mybatisplus.samples.generator");
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
        // to do nothing
        }
    };
    List<FileOutConfig> focList = new ArrayList<>();
    focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输入文件名称
            return projectPath + "/mybatis-plus-sample-generator/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
        }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    mpg.setTemplate(new TemplateConfig().setXml(null));
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setSuperEntityClass("com.baomidou.mybatisplus.samples.generator.common.BaseEntity");
    strategy.setEntityLombokModel(true);
    strategy.setSuperControllerClass("com.baomidou.mybatisplus.samples.generator.common.BaseController");
    strategy.setInclude(scanner("表名"));
    strategy.setSuperEntityColumns("id");
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
}
Also used : DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) ArrayList(java.util.ArrayList) TemplateConfig(com.baomidou.mybatisplus.generator.config.TemplateConfig) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) PackageConfig(com.baomidou.mybatisplus.generator.config.PackageConfig) FileOutConfig(com.baomidou.mybatisplus.generator.config.FileOutConfig) StrategyConfig(com.baomidou.mybatisplus.generator.config.StrategyConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 33 with InjectionConfig

use of com.baomidou.mybatisplus.generator.InjectionConfig in project blade-tool by chillzhuang.

the class BladeCodeGenerator method getInjectionConfig.

private InjectionConfig getInjectionConfig() {
    String servicePackage = serviceName.split("-").length > 1 ? serviceName.split("-")[1] : serviceName;
    // 自定义配置
    Map<String, Object> map = new HashMap<>(16);
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
            map.put("codeName", codeName);
            map.put("serviceName", serviceName);
            map.put("servicePackage", servicePackage);
            map.put("servicePackageLowerCase", servicePackage.toLowerCase());
            map.put("tenantColumn", tenantColumn);
            map.put("hasWrapper", hasWrapper);
            this.setMap(map);
        }
    };
    List<FileOutConfig> focList = new ArrayList<>();
    focList.add(new FileOutConfig("/templates/sql/menu.sql.vm") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            map.put("entityKey", (tableInfo.getEntityName().toLowerCase()));
            map.put("menuId", IdWorker.getId());
            map.put("addMenuId", IdWorker.getId());
            map.put("editMenuId", IdWorker.getId());
            map.put("removeMenuId", IdWorker.getId());
            map.put("viewMenuId", IdWorker.getId());
            return getOutputDir() + "/" + "/sql/" + tableInfo.getEntityName().toLowerCase() + ".menu.mysql";
        }
    });
    focList.add(new FileOutConfig("/templates/entityVO.java.vm") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + "vo" + "/" + tableInfo.getEntityName() + "VO" + StringPool.DOT_JAVA;
        }
    });
    focList.add(new FileOutConfig("/templates/entityDTO.java.vm") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + "dto" + "/" + tableInfo.getEntityName() + "DTO" + StringPool.DOT_JAVA;
        }
    });
    if (hasWrapper) {
        focList.add(new FileOutConfig("/templates/wrapper.java.vm") {

            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + "wrapper" + "/" + tableInfo.getEntityName() + "Wrapper" + StringPool.DOT_JAVA;
            }
        });
    }
    if (Func.isNotBlank(packageWebDir)) {
        if (Func.equals(systemName, DevelopConstant.SWORD_NAME)) {
            focList.add(new FileOutConfig("/templates/sword/action.js.vm") {

                @Override
                public String outputFile(TableInfo tableInfo) {
                    return getOutputWebDir() + "/actions" + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
                }
            });
            focList.add(new FileOutConfig("/templates/sword/model.js.vm") {

                @Override
                public String outputFile(TableInfo tableInfo) {
                    return getOutputWebDir() + "/models" + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
                }
            });
            focList.add(new FileOutConfig("/templates/sword/service.js.vm") {

                @Override
                public String outputFile(TableInfo tableInfo) {
                    return getOutputWebDir() + "/services" + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
                }
            });
            focList.add(new FileOutConfig("/templates/sword/list.js.vm") {

                @Override
                public String outputFile(TableInfo tableInfo) {
                    return getOutputWebDir() + "/pages" + "/" + StringUtil.upperFirst(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + ".js";
                }
            });
            focList.add(new FileOutConfig("/templates/sword/add.js.vm") {

                @Override
                public String outputFile(TableInfo tableInfo) {
                    return getOutputWebDir() + "/pages" + "/" + StringUtil.upperFirst(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "Add.js";
                }
            });
            focList.add(new FileOutConfig("/templates/sword/edit.js.vm") {

                @Override
                public String outputFile(TableInfo tableInfo) {
                    return getOutputWebDir() + "/pages" + "/" + StringUtil.upperFirst(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "Edit.js";
                }
            });
            focList.add(new FileOutConfig("/templates/sword/view.js.vm") {

                @Override
                public String outputFile(TableInfo tableInfo) {
                    return getOutputWebDir() + "/pages" + "/" + StringUtil.upperFirst(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "View.js";
                }
            });
        } else if (Func.equals(systemName, DevelopConstant.SABER_NAME)) {
            focList.add(new FileOutConfig("/templates/saber/api.js.vm") {

                @Override
                public String outputFile(TableInfo tableInfo) {
                    return getOutputWebDir() + "/api" + "/" + servicePackage.toLowerCase() + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
                }
            });
            focList.add(new FileOutConfig("/templates/saber/crud.vue.vm") {

                @Override
                public String outputFile(TableInfo tableInfo) {
                    return getOutputWebDir() + "/views" + "/" + servicePackage.toLowerCase() + "/" + tableInfo.getEntityName().toLowerCase() + ".vue";
                }
            });
        }
    }
    cfg.setFileOutConfigList(focList);
    return cfg;
}
Also used : InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo)

Aggregations

InjectionConfig (com.baomidou.mybatisplus.generator.InjectionConfig)33 AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)22 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)22 ArrayList (java.util.ArrayList)20 FreemarkerTemplateEngine (com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)13 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)6 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)4 GlobalConfig (com.baomidou.mybatisplus.generator.config.GlobalConfig)4 PackageConfig (com.baomidou.mybatisplus.generator.config.PackageConfig)4 StrategyConfig (com.baomidou.mybatisplus.generator.config.StrategyConfig)4 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)4 VelocityTemplateEngine (com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine)4 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 MySqlTypeConvert (com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert)2 TableFill (com.baomidou.mybatisplus.generator.config.po.TableFill)2 DbColumnType (com.baomidou.mybatisplus.generator.config.rules.DbColumnType)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 TableField (com.baomidou.mybatisplus.generator.config.po.TableField)1 MyFreemarkerTemplateEngine (com.baomidou.plugin.idea.mybatisx.codegenerator.MyFreemarkerTemplateEngine)1