baomidou/mybatis-plus · error · RuntimeException

读取[%s]字段出错!

Error message

读取[%s]字段出错!

What it means

ResultSetWrapper.getStringResult wraps the SQLException thrown by ResultSet.getString(columnLabel) for a single metadata column (table name, remarks, key name, etc.). It means the configured IDbQuery asked the ResultSet for a column label the executed metadata query did not produce.

Source

Thrown at mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/DbQueryDecorator.java:243

        private final IDbQuery dbQuery;

        @Getter
        private final ResultSet resultSet;

        private final DbType dbType;

        ResultSetWrapper(ResultSet resultSet, IDbQuery dbQuery, DbType dbType) {
            this.resultSet = resultSet;
            this.dbQuery = dbQuery;
            this.dbType = dbType;
        }

        public String getStringResult(String columnLabel) {
            try {
                return resultSet.getString(columnLabel);
            } catch (SQLException sqlException) {
                throw new RuntimeException(String.format("读取[%s]字段出错!", columnLabel), sqlException);
            }
        }

        /**
         * @return 获取字段注释
         */
        public String getFiledComment() {
            return getComment(dbQuery.fieldComment());
        }

        /**
         * 获取格式化注释
         *
         * @param columnLabel 字段列
         * @return 注释
         */
        private String getComment(String columnLabel) {
            return StringUtils.isNotBlank(columnLabel) ? formatComment(getStringResult(columnLabel)) : StringPool.EMPTY;

View on GitHub (pinned to bf67d90747)

Solutions

  1. Read the wrapped SQLException to see the missing column label and the SQL that ran; the mismatch is usually obvious.
  2. Do not force dbQuery manually — pass the correct JDBC URL and let DataSourceConfig infer the DbType, so the matching adapter is used.
  3. If a custom IDbQuery is used, verify every column label in tableName()/tableComment()/fieldNames() is selected by your tablesSql()/columnsSql().
  4. Upgrade the JDBC driver to one whose metadata column labels match what the bundled adapter expects.

Example fix

// before
new DataSourceConfig.Builder(url, user, pass).dbQuery(new MySqlQuery()); // url is postgres

// after
new DataSourceConfig.Builder(url, user, pass); // DbType auto-detected from URL -> PostgreSqlQuery
Defensive patterns

Strategy: validation

Validate before calling

// Before generating: verify DbType inferred from URL matches the adapter you intend
DataSourceConfig ds = new DataSourceConfig.Builder(url, user, pass).build();
// ensure you did NOT force a mismatched dbQuery; rely on auto-detection:
// ds.getDbQuery() must be an instance of the adapter for your real database

Try / catch

try {
    generator.generate();
} catch (RuntimeException e) {
    Throwable root = e;
    while (root.getCause() != null) root = root.getCause();
    if (root instanceof SQLException) log.error("metadata column mismatch: {}", root.getMessage());
}

Prevention

When it happens

Trigger: Any DbQueryDecorator metadata read (tableName(), tableComment(), fieldName()...) where the driver fails to resolve columnLabel — mismatched DbType (e.g. DbType set to MYSQL against a PostgreSQL URL), custom IDbQuery tablesSql/columnsSql not matching fieldNames(), or driver metadata column renames.

Common situations: Setting dbQuery/DbType explicitly to a wrong database type so the adapter's SQL runs but returns different column names; using a custom IDbQuery copied from another database's adapter without adjusting the column labels; older JDBC drivers returning lowercase/uppercase labels.

Related errors


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