baomidou/mybatis-plus · error · IllegalArgumentException

`url` cannot be empty

Error message

`url` cannot be empty

What it means

DataSourceConfig.Builder's primary constructor requires a non-blank JDBC URL; passing null, empty, or whitespace-only url throws IllegalArgumentException('`url` cannot be empty') before any connection is attempted. This is a fail-fast guard in the code generator (mybatis-plus-generator) so a useless DataSource is never constructed.

Source

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

    public static class Builder implements IConfigBuilder<DataSourceConfig> {

        private final DataSourceConfig dataSourceConfig;

        private Builder() {
            this.dataSourceConfig = new DataSourceConfig();
        }

        /**
         * 构造初始化方法
         *
         * @param url      数据库连接地址
         * @param username 数据库账号
         * @param password 数据库密码
         */
        public Builder(@NotNull String url, String username, String password) {
            this();
            if (StringUtils.isBlank(url)) {
                throw new IllegalArgumentException("`url` cannot be empty");
            }
            this.dataSourceConfig.url = url;
            this.dataSourceConfig.username = username;
            this.dataSourceConfig.password = password;
        }

        /**
         * 构造初始化方法
         *
         * @param dataSource 外部数据源实例
         */
        public Builder(@NotNull DataSource dataSource) {
            this();
            this.dataSourceConfig.dataSource = dataSource;
            try {
                Connection conn = dataSource.getConnection();
                this.dataSourceConfig.url = conn.getMetaData().getURL();
                try {

View on GitHub (pinned to bf67d90747)

Solutions

  1. Supply a concrete, correct JDBC URL (jdbc:mysql://host:3306/db, jdbc:postgresql://..., etc.) to the Builder.
  2. If it comes from config/env, validate it is present and non-blank before building (fail with a clear message about which property is missing).
  3. Check for typos in the property/env name and that the activated profile actually defines it.
  4. Alternatively pass a pre-built DataSource instance to the Builder(DataSource) overload when URL assembly is complex.

Example fix

// before
new DataSourceConfig.Builder(url, user, pass).build(); // url null when env var missing

// after
String url = System.getenv("DB_URL");
if (StringUtils.isBlank(url)) {
    throw new IllegalStateException("DB_URL env variable is required for code generation");
}
new DataSourceConfig.Builder(url, user, pass).build();
Defensive patterns

Strategy: validation

Validate before calling

String url = System.getenv("DB_URL");
if (StringUtils.isBlank(url)) {
    throw new IllegalStateException("DB_URL must be set (e.g. jdbc:mysql://host:3306/db)");
}
new DataSourceConfig.Builder(url, user, pass).build();

Type guard

static boolean isUsableJdbcUrl(String url) {
    return url != null && !url.trim().isEmpty() && url.startsWith("jdbc:");
}

Try / catch

try {
    dsConfig = new DataSourceConfig.Builder(url, user, pass).build();
} catch (IllegalArgumentException e) {
    throw new IllegalStateException("Generator datasource misconfigured: DB URL missing — check env/profile", e);
}

Prevention

When it happens

Trigger: Building a generator DataSourceConfig with a url that resolved to null/blank at runtime — reading it from a config property that is unset, an environment variable missing in CI, a typo'd property key, or string concatenation producing empty.

Common situations: Generator run from Maven/Gradle or CLI where the db URL property lives in a profile not activated; CI pipelines missing the DB_URL environment variable; property placeholder not resolved (literal '${db.url}' also fails later, unset ones fail here).

Related errors


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