baomidou/mybatis-plus · error · IllegalArgumentException

`include` and `exclude` configurations are mutually exclusiv

Error message

`include` and `exclude` configurations are mutually exclusive and cannot be used simultaneously.

What it means

StrategyConfig.validate() throws IllegalArgumentException when both include and exclude table lists are non-empty at generation time. The two filters are semantically contradictory (generate ONLY these vs generate all EXCEPT these), so the library rejects the combination instead of guessing precedence.

Source

Thrown at mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/StrategyConfig.java:270

     * 表名称匹配过滤表前缀
     *
     * @param tableName 表名称
     * @since 3.3.2
     */
    public boolean startsWithTablePrefix(@NotNull String tableName) {
        return this.tablePrefix.stream().anyMatch(tableName::startsWith);
    }

    /**
     * 验证配置项
     *
     * @since 3.5.0
     */
    public void validate() {
        boolean isInclude = !this.getInclude().isEmpty();
        boolean isExclude = !this.getExclude().isEmpty();
        if (isInclude && isExclude) {
            throw new IllegalArgumentException("`include` and `exclude` configurations are mutually exclusive and cannot be used simultaneously.");
        }
        if (this.getNotLikeTable() != null && this.getLikeTable() != null) {
            throw new IllegalArgumentException("`likeTable` and `notLikeTable` configurations are mutually exclusive and cannot be used simultaneously.");
        }
    }

    /**
     * 包含表名匹配
     *
     * @param tableName 表名
     * @return 是否匹配
     * @since 3.5.0
     */
    public boolean matchIncludeTable(@NotNull String tableName) {
        return matchTable(tableName, this.getInclude());
    }

    /**

View on GitHub (pinned to bf67d90747)

Solutions

  1. Keep only one of the two filters: if you want an allow-list, remove the exclude(...) call; if you want a deny-list, remove the include(...) call.
  2. If you need both semantics, express the deny part by trimming the include list yourself before configuring it (e.g. include.removeAll(excluded)).
  3. Run strategyConfig.validate() early in a unit test so misconfiguration fails fast with a clear message instead of at generate time.

Example fix

// before
StrategyConfig strategy = new StrategyConfig();
strategy.addInclude("sys_user", "sys_role");
strategy.addExclude("sys_log"); // throws on validate()

// after
StrategyConfig strategy = new StrategyConfig();
strategy.addInclude("sys_user", "sys_role"); // deny-list removed
Defensive patterns

Strategy: validation

Validate before calling

// Call validate() yourself at startup to fail fast with the clear message
strategyConfig.validate();
// or pre-check:
if (!strategyConfig.getInclude().isEmpty() && !strategyConfig.getExclude().isEmpty()) {
    throw new IllegalArgumentException("choose include OR exclude");
}

Prevention

When it happens

Trigger: Building a FastAutoGenerator/AutoGenerator whose StrategyConfig has both .include(...) (or includePrefix/addInclude) and .exclude(...) (or notLikeTable-adjacent exclude methods) populated, then calling generate() which invokes validate().

Common situations: Copy-pasting strategy configuration from two examples into one config; incrementally adding .exclude("tmp_x") to a config that already had .include("sys_user") and forgetting the two cannot coexist.

Related errors


AI-assisted analysis of baomidou/mybatis-plus@bf67d90747 (2026-08-14). Data as JSON: /api/errors/d377abf9e228c05f. Report an issue: GitHub.