Search in sources :

Example 1 with DateType

use of com.baomidou.mybatisplus.generator.config.rules.DateType in project code-generator-maven-plugin by mengweijin.

the class DefaultAutoGenerator method execute.

public void execute() {
    Parameters parameters = projectInfo.getParameters();
    String outputDir = FileUtil.file(projectInfo.getBaseDir(), "target/code-generator/").getAbsolutePath();
    // clean directory target/code-generator
    FileUtil.del(outputDir);
    FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(dataSourceConfigBuilder()).globalConfig(builder -> builder.fileOverride().author(parameters.getAuthor()).enableSwagger().disableOpenDir().outputDir(outputDir).dateType(DateType.TIME_PACK).commentDate("yyyy-MM-dd")).packageConfig(builder -> builder.parent(parameters.getOutputPackage())).templateConfig((Consumer<TemplateConfig.Builder>) TemplateConfig.Builder::disable).strategyConfig(builder -> builder.addInclude(this.trimItemName(parameters.getTables())).addTablePrefix(this.trimItemName(parameters.getTablePrefix())).entityBuilder().superClass(parameters.getSuperEntityClass()).enableChainModel().enableLombok().enableTableFieldAnnotation().versionColumnName("version").versionPropertyName("version").logicDeleteColumnName("deleted").logicDeletePropertyName("deleted").naming(NamingStrategy.underline_to_camel).addSuperEntityColumns(this.generateDefaultSuperEntityColumns()).controllerBuilder().superClass(parameters.getSuperControllerClass()).enableHyphenStyle().enableRestStyle().serviceBuilder().superServiceClass(parameters.getSuperServiceClass()).superServiceImplClass(parameters.getSuperServiceImplClass()).mapperBuilder().superClass(parameters.getSuperDaoClass()).enableBaseColumnList().enableBaseResultMap()).injectionConfig(builder -> {
        builder.beforeOutputFile(((tableInfo, objectMap) -> {
            enhanceObjectMap(objectMap, parameters);
            FileOutput.outputFile(tableInfo, objectMap, projectInfo, outputDir);
        }));
    }).templateEngine(TemplateEngineFactory.getTemplateEngine(this.projectInfo.getParameters().getTemplateType()));
    fastAutoGenerator.execute();
}
Also used : DbInfo(com.github.mengweijin.generator.entity.DbInfo) Arrays(java.util.Arrays) Getter(lombok.Getter) CustomerDataSource(com.github.mengweijin.generator.config.CustomerDataSource) ClassUtil(cn.hutool.core.util.ClassUtil) DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) ArrayList(java.util.ArrayList) FastAutoGenerator(com.baomidou.mybatisplus.generator.FastAutoGenerator) NamingStrategy(com.baomidou.mybatisplus.generator.config.rules.NamingStrategy) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) Map(java.util.Map) DbInfoUtils(com.github.mengweijin.generator.util.DbInfoUtils) IdField(com.github.mengweijin.generator.entity.IdField) TemplateEngineFactory(com.github.mengweijin.generator.factory.TemplateEngineFactory) Parameters(com.github.mengweijin.generator.entity.Parameters) ProjectInfo(com.github.mengweijin.generator.entity.ProjectInfo) Field(java.lang.reflect.Field) Consumer(java.util.function.Consumer) TemplateConfig(com.baomidou.mybatisplus.generator.config.TemplateConfig) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DateType(com.baomidou.mybatisplus.generator.config.rules.DateType) FileOutput(com.github.mengweijin.generator.config.FileOutput) FileUtil(cn.hutool.core.io.FileUtil) TableField(com.baomidou.mybatisplus.generator.config.po.TableField) Parameters(com.github.mengweijin.generator.entity.Parameters) Consumer(java.util.function.Consumer) TemplateConfig(com.baomidou.mybatisplus.generator.config.TemplateConfig) FastAutoGenerator(com.baomidou.mybatisplus.generator.FastAutoGenerator)

Example 2 with DateType

use of com.baomidou.mybatisplus.generator.config.rules.DateType 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

FastAutoGenerator (com.baomidou.mybatisplus.generator.FastAutoGenerator)2 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)2 DateType (com.baomidou.mybatisplus.generator.config.rules.DateType)2 NamingStrategy (com.baomidou.mybatisplus.generator.config.rules.NamingStrategy)2 Map (java.util.Map)2 Slf4j (lombok.extern.slf4j.Slf4j)2 FileUtil (cn.hutool.core.io.FileUtil)1 ClassUtil (cn.hutool.core.util.ClassUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 IdWorker (com.baomidou.mybatisplus.core.toolkit.IdWorker)1 StringPool (com.baomidou.mybatisplus.core.toolkit.StringPool)1 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)1 OutputFile (com.baomidou.mybatisplus.generator.config.OutputFile)1 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)1 TemplateType (com.baomidou.mybatisplus.generator.config.TemplateType)1 TableField (com.baomidou.mybatisplus.generator.config.po.TableField)1 VelocityTemplateEngine (com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine)1 CustomerDataSource (com.github.mengweijin.generator.config.CustomerDataSource)1 FileOutput (com.github.mengweijin.generator.config.FileOutput)1 DbInfo (com.github.mengweijin.generator.entity.DbInfo)1