baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.42k stars 4.31k forks source link

FastAutoGenerator 无法禁止 service 以及controller生成 #6148

Closed iqolome closed 6 months ago

iqolome commented 6 months ago

当前使用版本 3.5.6 当前环境信息 JDK22 Mysql 8.3.0 描述bug现象 FastAutoGenerator 无法禁止 service 以及controller生成 提供问题复现步骤

  // 自定义上下文
        Map<String, Object> customMap = new HashMap<>(NumberConstant.INT4);

        customMap.put(CodeGenConstants.COPYRIGHT, genCodePropertie.getCopyright());
        customMap.put(CodeGenConstants.PROJECT_NAME, daoCodeGenInfo.getProjectName());
        customMap.put(CodeGenConstants.COLUMN_CONSTANT_FIELD_PREFIX,
                daoCodeGenInfo.getColumnConstantFieldPrefix());

        customMap.put(CodeGenConstants.DATETIME,
                DateUtils.format(LocalDateTime.now(), DateUtils.CN_DATE_TIME));

        DatabaseTypeEnum databaseType = genCodePropertie.getDatabaseType();
        IKeyWordsHandler keyWordsHandler;

        ITypeConvert typeConvert;

        switch (databaseType) {

            case MYSQL -> {
                keyWordsHandler = new MySqlKeyWordsHandler();
                typeConvert = new MySqlTypeConvert();
            }
            case PGSQL -> {
                keyWordsHandler = new PostgreSqlKeyWordsHandler();
                typeConvert = new PostgreSqlTypeConvert();
            }
            case null, default -> {
                throw new UnsupportedOperationException("数据库类型不支持");
            }

        }

        // https://baomidou.com/pages/981406/
        // 使用元数据查询的方式生成代码,默认已经根据jdbcType来适配java类型,支持使用typeConvertHandler来转换需要映射的类型映射
        DataSourceConfig.Builder dataSourceConfig = new DataSourceConfig.Builder(
                genCodePropertie.getDbUrl(), genCodePropertie.getDbUsername(),
                genCodePropertie.getDbPassword())

                .keyWordsHandler(keyWordsHandler)
                .typeConvert(typeConvert)
                .typeConvertHandler(enumTypeConvertHandler);

        FastAutoGenerator.create(dataSourceConfig)
                .globalConfig(globalConfig -> globalConfig
                        .outputDir(daoCodeGenInfo.getProjectPath()
                                + CodeGenConstants.JAVA_CODE_PATH)
                        .author(daoCodeGenInfo.getAuth())
                        // 禁止打开目录
                        .disableOpenDir())

                .packageConfig(packageConfig -> packageConfig
                        .parent(daoCodeGenInfo.getBasePackage())
                        .entity(daoCodeGenInfo.getEntityPackage())
                        .mapper(daoCodeGenInfo.getMapperPackage())
                        .xml(daoCodeGenInfo.getXmlPackage())
                        .pathInfo(Collections.singletonMap(OutputFile.xml,
                                daoCodeGenInfo.getProjectPath()
                                        + CodeGenConstants.JAVA_RESOURCE_PATH
                                        + StringConstant.SLASH
                                        + daoCodeGenInfo.getXmlPackage()))

                )

                .injectionConfig(injectionConfig -> injectionConfig
                        .beforeOutputFile((tableInfo, map) -> {
                            List<TableField> tableFields = tableInfo.getFields();
                            // 渲染注释
                            tableFields.forEach(iteam -> {
                                iteam.setComment(mergeComment(iteam.getComment()));
                            });

                            // 所有表字段
                            List<TableField> allField = getAllField(tableInfo);
                            String allFieldNames = Joiner.on(StringConstant.COMMA).join(
                                    allField.stream().map(TableField::getName).toList());
                            map.put(CodeGenConstants.TABLE_FIELDS, allField);
                            map.put(CodeGenConstants.ALL_FIELD_NAMES, allFieldNames);
                        }).customMap(customMap))
                .strategyConfig(strategyConfig -> strategyConfig
                        .enableCapitalMode()
                        .addInclude(daoCodeGenInfo.getTables())
                        .entityBuilder()
                        .enableFileOverride()
                        .superClass(genCodePropertie.getModelSuperClass())
                        .disableSerialVersionUID().enableColumnConstant()
                        .enableLombok()
                        .versionPropertyName(genCodePropertie.getVersionPropertyName())
                        .idType(genCodePropertie.getIdType())
                        .mapperBuilder()
                        .mapperAnnotation(genCodePropertie.getMapperAnnotation())
                        .enableFileOverride()
                        .enableBaseResultMap()
                        .enableBaseColumnList()
                        .serviceBuilder()
                        .disable()
                        .controllerBuilder()
                        .disable()
                        .build()

                )

                .templateEngine(velocityTemplateComponent)

                .execute();

提供完整堆栈日志(可选)

提供问题复现工程(可选) 请尽量提供复现工程,减少大家排错的时间.

iqolome commented 6 months ago

image

iqolome commented 6 months ago

com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine#outputService:154 行

image

service.isGenerateServiceImpl() 这个判断是对的

StringUtils.isNotBlank(tableInfo.getServiceImplName() 这里面一定可以取导致 因为默认初始化了

image

&& StringUtils.isNotBlank(serviceImplPath))) // 这个里面也有默认初始化的值

image

俩个都有默认初始的值导致一定为true

Controller 的生成也是一样的问题

iqolome commented 6 months ago

packageConfig 会默认赋值所有的配置

    public Map<String, String> getPackageInfo() {
        if (packageInfo.isEmpty()) {
            packageInfo.put(ConstVal.MODULE_NAME, this.getModuleName());
            packageInfo.put(ConstVal.ENTITY, this.joinPackage(this.getEntity()));
            packageInfo.put(ConstVal.MAPPER, this.joinPackage(this.getMapper()));
            packageInfo.put(ConstVal.XML, this.joinPackage(this.getXml()));
            packageInfo.put(ConstVal.SERVICE, this.joinPackage(this.getService()));
            packageInfo.put(ConstVal.SERVICE_IMPL, this.joinPackage(this.getServiceImpl()));
            packageInfo.put(ConstVal.CONTROLLER, this.joinPackage(this.getController()));
            packageInfo.put(ConstVal.PARENT, this.getParent());
        }
        return Collections.unmodifiableMap(this.packageInfo);
    }
nieqiurong commented 6 months ago

6060

iqolome commented 6 months ago

6060

没发版不建议关闭问题,还以为解决了呢

@nieqiurong

miemieYaho commented 6 months ago

https://baomidou.com/pages/bab2db/#snapshot

iqolome commented 6 months ago

https://baomidou.com/pages/bab2db/#snapshot

ok