Search in sources :

Example 1 with FileOutConfig

use of com.baomidou.mybatisplus.generator.config.FileOutConfig in project ddf-common by dongfangding.

the class CodeGenerator method generate.

public static void generate() {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir") + "/ddf-common-mybatis-generator";
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("mybatis-plus-generator");
    gc.setFileOverride(true);
    gc.setActiveRecord(false);
    gc.setBaseResultMap(true);
    gc.setBaseColumnList(true);
    gc.setEnableCache(false);
    gc.setOpen(false);
    // gc.setSwagger2(true); 实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/better-together?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&tinyInt1isBit=false");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("123");
    dsc.setTypeConvert(new MySqlTypeConvertCustom());
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("com.ddf.better.together");
    pc.setEntity("model.entity");
    /*        pc.setService("com.ddf.boot.service");
        pc.setServiceImpl("com.ddf.boot.service.impl");
        pc.setController("com.ddf.boot.controller");
        pc.setMapper("com.ddf.boot.mapper");*/
    pc.setXml(null);
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
        // to do nothing
        }
    };
    // 如果模板引擎是 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;
        }
    });
    /*        // 自定义vo
        focList.add(new FileOutConfig("myTemplates/vo.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return gc.getOutputDir() + "/com/code/generator/domain/vo/"
                        + tableInfo.getEntityName()+"Vo" + StringPool.DOT_JAVA;
            }
        });*/
    // 应用自定义文件输出
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);
    mpg.setTemplateEngine(new VelocityTemplateEngine());
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    // 过滤表名前缀
    strategy.setTablePrefix("");
    // 驼峰命名
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    // 设置父类,如果存在的话
    // strategy.setSuperEntityClass("com.ddf.boot.common.core.model.BaseDomain");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 写于父类中的公共字段
    // strategy.setSuperEntityColumns("id", "create_by", "create_time", "modify_by", "modify_time", "is_del", "version");
    // 其它带父类的
    // strategy.setSuperServiceClass("");
    strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.execute();
}
Also used : DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) VelocityTemplateEngine(com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine) 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) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 2 with FileOutConfig

use of com.baomidou.mybatisplus.generator.config.FileOutConfig in project heifer by galaxy-sea.

the class GeneratorCode method injectionConfig.

private static InjectionConfig injectionConfig(PackageConfig packageInfo, DataSourceConfig dataSourceConfig) {
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
            this.setMap(map);
        // to do nothing
        }

        @Override
        public void initTableMap(TableInfo tableInfo) {
            super.initTableMap(tableInfo);
            tableInfo.setComment(pattern.matcher(tableInfo.getComment()).replaceAll(""));
            for (TableField field : tableInfo.getFields()) {
                if (StringUtils.isNotEmpty(field.getComment())) {
                    field.setComment(pattern.matcher(field.getComment()).replaceAll(""));
                }
            }
        }
    };
    String projectPath = System.getProperty("user.dir");
    // 如果模板引擎是 freemarker
    String templatePath = "generator/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 的名称会跟着发生变化!!
            if (moduleName == null) {
                return projectPath + "/src/main/resources/mapper/" + "" + tableInfo.getXmlName() + "Mapper" + ConstVal.XML_SUFFIX;
            }
            moduleName = moduleName.replace(".", "/");
            return projectPath + "/src/main/resources/mapper/" + moduleName + "/" + tableInfo.getXmlName() + ConstVal.XML_SUFFIX;
        }
    });
    cfg.setFileOutConfigList(focList);
    return cfg;
}
Also used : FileOutConfig(com.baomidou.mybatisplus.generator.config.FileOutConfig) ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) TableField(com.baomidou.mybatisplus.generator.config.po.TableField)

Example 3 with FileOutConfig

use of com.baomidou.mybatisplus.generator.config.FileOutConfig in project chao-cloud by chaojunzi.

the class MybatisGeneratorConfig method InjectionConfig.

@Bean
public InjectionConfig InjectionConfig() {
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
        }
    };
    // 自定义输出配置
    List<FileOutConfig> focList = new ArrayList<>();
    cfg.setFileOutConfigList(focList);
    return cfg;
}
Also used : FileOutConfig(com.baomidou.mybatisplus.generator.config.FileOutConfig) ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) Bean(org.springframework.context.annotation.Bean)

Example 4 with FileOutConfig

use of com.baomidou.mybatisplus.generator.config.FileOutConfig in project demo-parent by yindanqing925.

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") + "/mybatisplus-demo";
    gc.setOutputDir(projectPath + "/src/main/java");
    // TODO 设置用户名
    gc.setAuthor("yindanqing");
    gc.setOpen(false);
    // service 命名方式
    gc.setServiceName("%sService");
    // service impl 命名方式
    gc.setServiceImplName("%sServiceImpl");
    // 自定义文件命名,注意 %s 会自动填充表实体属性!
    gc.setMapperName("%sMapper");
    gc.setXmlName("%sMapper");
    gc.setFileOverride(true);
    gc.setActiveRecord(true);
    // XML 二级缓存
    gc.setEnableCache(false);
    // XML ResultMap
    gc.setBaseResultMap(true);
    // XML columList
    gc.setBaseColumnList(false);
    mpg.setGlobalConfig(gc);
    // TODO 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://cdb-1v2wt3os.bj.tencentcdb.com:10243/nh?useUnicode=true&characterEncoding=UTF8");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("user_dev");
    dsc.setPassword("user_dev_nh");
    mpg.setDataSource(dsc);
    // TODO 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("org.nh.mybatisplus.dict");
    pc.setEntity("domain");
    pc.setService("service");
    pc.setServiceImpl("service.impl");
    mpg.setPackageInfo(pc);
    // 自定义需要填充的字段
    List<TableFill> tableFillList = new ArrayList<>();
    // 如 每张表都有一个创建时间、修改时间
    // 而且这基本上就是通用的了,新增时,创建时间和修改时间同时修改
    // 修改时,修改时间会修改,
    // 虽然像Mysql数据库有自动更新几只,但像ORACLE的数据库就没有了,
    // 使用公共字段填充功能,就可以实现,自动按场景更新了。
    // 如下是配置
    // TableFill createField = new TableFill("gmt_create", FieldFill.INSERT);
    // TableFill modifiedField = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
    // tableFillList.add(createField);
    // tableFillList.add(modifiedField);
    // 自定义配置
    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 + "/src/main/java/org/nh/mybatisplus/dict/mapper/" + tableInfo.getEntityName() + "Mapper" + ".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.setEntityLombokModel(true);
    // 设置逻辑删除键
    strategy.setLogicDeleteFieldName("deleted");
    // TODO 指定生成的bean的数据库表名
    strategy.setInclude("transfer_dict");
    // strategy.setSuperEntityColumns("id");
    // 驼峰转连字符
    strategy.setControllerMappingHyphenStyle(true);
    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) TableFill(com.baomidou.mybatisplus.generator.config.po.TableFill) 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 5 with FileOutConfig

use of com.baomidou.mybatisplus.generator.config.FileOutConfig in project chao-cloud by chaojunzi.

the class ZipVelocityTemplateEngine method batchOutput.

/**
 * 输出到文件流
 * @param zip zip输出流
 * @return {@link AbstractTemplateEngine}
 * @throws Exception 生成文件模板时抛出的异常
 */
public AbstractTemplateEngine batchOutput(ZipOutputStream zip) throws Exception {
    try {
        List<TableInfo> tableInfoList = getConfigBuilder().getTableInfoList();
        for (TableInfo tableInfo : tableInfoList) {
            Map<String, Object> objectMap = super.getObjectMap(tableInfo);
            Map<String, String> pathInfo = getConfigBuilder().getPathInfo();
            TemplateConfig template = getConfigBuilder().getTemplate();
            // 自定义内容
            InjectionConfig injectionConfig = getConfigBuilder().getInjectionConfig();
            if (null != injectionConfig) {
                injectionConfig.initMap();
                objectMap.put("cfg", injectionConfig.getMap());
                List<FileOutConfig> focList = injectionConfig.getFileOutConfigList();
                if (CollectionUtils.isNotEmpty(focList)) {
                    for (FileOutConfig foc : focList) {
                        if (isCreate(FileType.OTHER, foc.outputFile(tableInfo))) {
                            writer(objectMap, foc.getTemplatePath(), foc.outputFile(tableInfo));
                        }
                    }
                }
            }
            // appendTableInfo
            this.appendTableInfo(tableInfo, objectMap);
            VelocityContext context = new VelocityContext(objectMap);
            // MpEntity.java
            String entityName = tableInfo.getEntityName();
            if (null != entityName && null != pathInfo.get(ConstVal.ENTITY_PATH)) {
                String entityFile = String.format((pathInfo.get(ConstVal.ENTITY_PATH) + File.separator + "%s" + suffixJavaOrKt()), entityName);
                if (isCreate(FileType.ENTITY, entityFile)) {
                    this.writer(context, templateFilePath(template.getEntity(getConfigBuilder().getGlobalConfig().isKotlin())), zip, entityFile);
                }
            }
            // MpMapper.java
            if (null != tableInfo.getMapperName() && null != pathInfo.get(ConstVal.MAPPER_PATH)) {
                String mapperFile = String.format((pathInfo.get(ConstVal.MAPPER_PATH) + File.separator + tableInfo.getMapperName() + suffixJavaOrKt()), entityName);
                if (isCreate(FileType.MAPPER, mapperFile)) {
                    this.writer(context, templateFilePath(template.getMapper()), zip, mapperFile);
                }
            }
            // MpMapper.xml
            if (null != tableInfo.getXmlName() && null != pathInfo.get(ConstVal.XML_PATH)) {
                String xmlFile = String.format((pathInfo.get(ConstVal.XML_PATH) + File.separator + tableInfo.getXmlName() + ConstVal.XML_SUFFIX), entityName);
                if (isCreate(FileType.XML, xmlFile)) {
                    this.writer(context, templateFilePath(template.getXml()), zip, xmlFile);
                }
            }
            // IMpService.java
            if (null != tableInfo.getServiceName() && null != pathInfo.get(ConstVal.SERVICE_PATH)) {
                String serviceFile = String.format((pathInfo.get(ConstVal.SERVICE_PATH) + File.separator + tableInfo.getServiceName() + suffixJavaOrKt()), entityName);
                if (isCreate(FileType.SERVICE, serviceFile)) {
                    this.writer(context, templateFilePath(template.getService()), zip, serviceFile);
                }
            }
            // MpServiceImpl.java
            if (null != tableInfo.getServiceImplName() && null != pathInfo.get(ConstVal.SERVICE_IMPL_PATH)) {
                String implFile = String.format((pathInfo.get(ConstVal.SERVICE_IMPL_PATH) + File.separator + tableInfo.getServiceImplName() + suffixJavaOrKt()), entityName);
                if (isCreate(FileType.SERVICE_IMPL, implFile)) {
                    this.writer(context, templateFilePath(template.getServiceImpl()), zip, implFile);
                }
            }
            // MpController.java
            if (null != tableInfo.getControllerName() && null != pathInfo.get(ConstVal.CONTROLLER_PATH)) {
                String controllerFile = String.format((pathInfo.get(ConstVal.CONTROLLER_PATH) + File.separator + tableInfo.getControllerName() + suffixJavaOrKt()), entityName);
                if (isCreate(FileType.CONTROLLER, controllerFile)) {
                    this.writer(context, templateFilePath(template.getController()), zip, controllerFile);
                }
            }
            // html->模板
            if (template instanceof HtmlTemplateConfig) {
                this.genHtml(tableInfo, (HtmlTemplateConfig) template, context, zip);
            }
        }
    } catch (Exception e) {
        logger.error("无法创建文件,请检查配置信息!", e);
        throw e;
    }
    return this;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) HtmlTemplateConfig(com.chao.cloud.common.extra.mybatis.generator.template.HtmlTemplateConfig) HtmlTemplateConfig(com.chao.cloud.common.extra.mybatis.generator.template.HtmlTemplateConfig) TemplateConfig(com.baomidou.mybatisplus.generator.config.TemplateConfig) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) BusinessException(com.chao.cloud.common.exception.BusinessException) IOException(java.io.IOException) FileOutConfig(com.baomidou.mybatisplus.generator.config.FileOutConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo)

Aggregations

InjectionConfig (com.baomidou.mybatisplus.generator.InjectionConfig)6 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)6 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)5 ArrayList (java.util.ArrayList)5 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)4 AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)3 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)3 GlobalConfig (com.baomidou.mybatisplus.generator.config.GlobalConfig)3 PackageConfig (com.baomidou.mybatisplus.generator.config.PackageConfig)3 StrategyConfig (com.baomidou.mybatisplus.generator.config.StrategyConfig)3 FreemarkerTemplateEngine (com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)2 TableField (com.baomidou.mybatisplus.generator.config.po.TableField)1 TableFill (com.baomidou.mybatisplus.generator.config.po.TableFill)1 VelocityTemplateEngine (com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine)1 BusinessException (com.chao.cloud.common.exception.BusinessException)1 HtmlTemplateConfig (com.chao.cloud.common.extra.mybatis.generator.template.HtmlTemplateConfig)1 IOException (java.io.IOException)1 VelocityContext (org.apache.velocity.VelocityContext)1 Bean (org.springframework.context.annotation.Bean)1