Search in sources :

Example 1 with OutputFile

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

the class BladeCodeGenerator method run.

/**
 * 代码生成执行
 */
public void run() {
    Properties props = getProperties();
    String url = Func.toStr(this.url, props.getProperty("spring.datasource.url"));
    String username = Func.toStr(this.username, props.getProperty("spring.datasource.username"));
    String password = Func.toStr(this.password, props.getProperty("spring.datasource.password"));
    String servicePackage = serviceName.split("-").length > 1 ? serviceName.split("-")[1] : serviceName;
    Map<String, Object> customMap = new HashMap<>(11);
    customMap.put("codeName", codeName);
    customMap.put("serviceName", serviceName);
    customMap.put("servicePackage", servicePackage);
    customMap.put("servicePackageLowerCase", servicePackage.toLowerCase());
    customMap.put("tenantColumn", tenantColumn);
    customMap.put("hasWrapper", hasWrapper);
    Map<String, String> customFile = new HashMap<>(15);
    customFile.put("menu.sql", "/templates/sql/menu.sql.vm");
    customFile.put("entityVO.java", "/templates/entityVO.java.vm");
    customFile.put("entityDTO.java", "/templates/entityDTO.java.vm");
    if (hasWrapper) {
        customFile.put("wrapper.java", "/templates/wrapper.java.vm");
    }
    if (Func.isNotBlank(packageWebDir)) {
        if (Func.equals(systemName, DevelopConstant.SWORD_NAME)) {
            customFile.put("action.js", "/templates/sword/action.js.vm");
            customFile.put("model.js", "/templates/sword/model.js.vm");
            customFile.put("service.js", "/templates/sword/service.js.vm");
            customFile.put("list.js", "/templates/sword/list.js.vm");
            customFile.put("add.js", "/templates/sword/add.js.vm");
            customFile.put("edit.js", "/templates/sword/edit.js.vm");
            customFile.put("view.js", "/templates/sword/view.js.vm");
        } else if (Func.equals(systemName, DevelopConstant.SABER_NAME)) {
            customFile.put("api.js", "/templates/saber/api.js.vm");
            customFile.put("crud.vue", "/templates/saber/crud.vue.vm");
        }
    }
    FastAutoGenerator.create(url, username, password).globalConfig(builder -> builder.author(props.getProperty("author")).dateType(DateType.TIME_PACK).enableSwagger().outputDir(getOutputDir()).disableOpenDir()).packageConfig(builder -> builder.parent(packageName).controller("controller").entity("entity").service("service").serviceImpl("service.impl").mapper("mapper").xml("mapper")).strategyConfig(builder -> builder.addTablePrefix(tablePrefix).addInclude(includeTables).addExclude(excludeTables).entityBuilder().naming(NamingStrategy.underline_to_camel).columnNaming(NamingStrategy.underline_to_camel).enableLombok().superClass("org.springblade.core.mp.base.BaseEntity").addSuperEntityColumns(superEntityColumns).serviceBuilder().superServiceClass("org.springblade.core.mp.base.BaseService").superServiceImplClass("org.springblade.core.mp.base.BaseServiceImpl").formatServiceFileName("I%sService").formatServiceImplFileName("%sServiceImpl").mapperBuilder().enableMapperAnnotation().enableBaseResultMap().enableBaseColumnList().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper").controllerBuilder().superClass("org.springblade.core.boot.ctrl.BladeController").formatFileName("%sController").enableRestStyle().enableHyphenStyle()).templateConfig(builder -> builder.disable(TemplateType.ENTITY).entity("/templates/entity.java.vm").service("/templates/service.java.vm").serviceImpl("/templates/serviceImpl.java.vm").mapper("/templates/mapper.java.vm").xml("/templates/mapper.xml.vm").controller("/templates/controller.java.vm")).injectionConfig(builder -> builder.beforeOutputFile((tableInfo, objectMap) -> System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size())).customMap(customMap).customFile(customFile)).templateEngine(new VelocityTemplateEngine() {

        @Override
        protected void outputCustomFile(Map<String, String> customFile, TableInfo tableInfo, Map<String, Object> objectMap) {
            String entityName = tableInfo.getEntityName();
            String entityNameLower = tableInfo.getEntityName().toLowerCase();
            customFile.forEach((key, value) -> {
                String outputPath = getPathInfo(OutputFile.other);
                if (StringUtil.equals(key, "menu.sql")) {
                    objectMap.put("entityKey", entityNameLower);
                    objectMap.put("menuId", IdWorker.getId());
                    objectMap.put("addMenuId", IdWorker.getId());
                    objectMap.put("editMenuId", IdWorker.getId());
                    objectMap.put("removeMenuId", IdWorker.getId());
                    objectMap.put("viewMenuId", IdWorker.getId());
                    outputPath = getOutputDir() + StringPool.SLASH + "sql" + StringPool.SLASH + entityNameLower + ".menu.sql";
                }
                if (StringUtil.equals(key, "entityVO.java")) {
                    outputPath = getOutputDir() + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "vo" + StringPool.SLASH + entityName + "VO" + StringPool.DOT_JAVA;
                }
                if (StringUtil.equals(key, "entityDTO.java")) {
                    outputPath = getOutputDir() + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "dto" + StringPool.SLASH + entityName + "DTO" + StringPool.DOT_JAVA;
                }
                if (StringUtil.equals(key, "wrapper.java")) {
                    outputPath = getOutputDir() + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "wrapper" + StringPool.SLASH + entityName + "Wrapper" + StringPool.DOT_JAVA;
                }
                if (StringUtil.equals(key, "action.js")) {
                    outputPath = getOutputWebDir() + StringPool.SLASH + "actions" + StringPool.SLASH + entityNameLower + ".js";
                }
                if (StringUtil.equals(key, "model.js")) {
                    outputPath = getOutputWebDir() + StringPool.SLASH + "models" + StringPool.SLASH + entityNameLower + ".js";
                }
                if (StringUtil.equals(key, "service.js")) {
                    outputPath = getOutputWebDir() + StringPool.SLASH + "services" + StringPool.SLASH + entityNameLower + ".js";
                }
                if (StringUtil.equals(key, "list.js")) {
                    outputPath = getOutputWebDir() + StringPool.SLASH + "pages" + StringPool.SLASH + StringUtil.upperFirst(servicePackage) + StringPool.SLASH + entityName + StringPool.SLASH + entityName + ".js";
                }
                if (StringUtil.equals(key, "add.js")) {
                    outputPath = getOutputWebDir() + StringPool.SLASH + "pages" + StringPool.SLASH + StringUtil.upperFirst(servicePackage) + StringPool.SLASH + entityName + StringPool.SLASH + entityName + "Add.js";
                }
                if (StringUtil.equals(key, "edit.js")) {
                    outputPath = getOutputWebDir() + StringPool.SLASH + "pages" + StringPool.SLASH + StringUtil.upperFirst(servicePackage) + StringPool.SLASH + entityName + StringPool.SLASH + entityName + "Edit.js";
                }
                if (StringUtil.equals(key, "view.js")) {
                    outputPath = getOutputWebDir() + StringPool.SLASH + "pages" + StringPool.SLASH + StringUtil.upperFirst(servicePackage) + StringPool.SLASH + entityName + StringPool.SLASH + entityName + "View.js";
                }
                if (StringUtil.equals(key, "api.js")) {
                    outputPath = getOutputWebDir() + StringPool.SLASH + "api" + StringPool.SLASH + servicePackage.toLowerCase() + StringPool.SLASH + entityNameLower + ".js";
                }
                if (StringUtil.equals(key, "crud.vue")) {
                    outputPath = getOutputWebDir() + StringPool.SLASH + "views" + StringPool.SLASH + servicePackage.toLowerCase() + StringPool.SLASH + entityNameLower + ".vue";
                }
                outputFile(new File(String.valueOf(outputPath)), objectMap, value, Boolean.TRUE);
            });
        }
    }).execute();
}
Also used : IdWorker(com.baomidou.mybatisplus.core.toolkit.IdWorker) Properties(java.util.Properties) StringUtil(org.springblade.core.tool.utils.StringUtil) ClassPathResource(org.springframework.core.io.ClassPathResource) IOException(java.io.IOException) HashMap(java.util.HashMap) File(java.io.File) OutputFile(com.baomidou.mybatisplus.generator.config.OutputFile) PropertiesLoaderUtils(org.springframework.core.io.support.PropertiesLoaderUtils) Slf4j(lombok.extern.slf4j.Slf4j) FastAutoGenerator(com.baomidou.mybatisplus.generator.FastAutoGenerator) TemplateType(com.baomidou.mybatisplus.generator.config.TemplateType) NamingStrategy(com.baomidou.mybatisplus.generator.config.rules.NamingStrategy) DevelopConstant(org.springblade.develop.constant.DevelopConstant) StringPool(com.baomidou.mybatisplus.core.toolkit.StringPool) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) VelocityTemplateEngine(com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine) Map(java.util.Map) DateType(com.baomidou.mybatisplus.generator.config.rules.DateType) Data(lombok.Data) Func(org.springblade.core.tool.utils.Func) Resource(org.springframework.core.io.Resource) HashMap(java.util.HashMap) VelocityTemplateEngine(com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) OutputFile(com.baomidou.mybatisplus.generator.config.OutputFile)

Aggregations

IdWorker (com.baomidou.mybatisplus.core.toolkit.IdWorker)1 StringPool (com.baomidou.mybatisplus.core.toolkit.StringPool)1 FastAutoGenerator (com.baomidou.mybatisplus.generator.FastAutoGenerator)1 OutputFile (com.baomidou.mybatisplus.generator.config.OutputFile)1 TemplateType (com.baomidou.mybatisplus.generator.config.TemplateType)1 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)1 DateType (com.baomidou.mybatisplus.generator.config.rules.DateType)1 NamingStrategy (com.baomidou.mybatisplus.generator.config.rules.NamingStrategy)1 VelocityTemplateEngine (com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Data (lombok.Data)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Func (org.springblade.core.tool.utils.Func)1 StringUtil (org.springblade.core.tool.utils.StringUtil)1 DevelopConstant (org.springblade.develop.constant.DevelopConstant)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1