Search in sources :

Example 1 with HtmlTemplateConfig

use of com.chao.cloud.common.extra.mybatis.generator.template.HtmlTemplateConfig 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)1 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)1 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)1 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)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