baomidou/mybatis-plus · error · RuntimeException

获取自定义字段错误:

Error message

获取自定义字段错误:

What it means

DbQueryDecorator.getCustomFields wraps a SQLException in a RuntimeException while reading each custom field (from fieldCustom()) out of the table-metadata ResultSet. It fires when the underlying JDBC driver cannot resolve a column name the dbQuery implementation declared, so resultSet.getObject(fc) throws.

Source

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

    @Override
    public String[] fieldCustom() {
        return dbQuery.fieldCustom();
    }

    @Override
    public String primaryKeySql(DataSourceConfig dataSourceConfig, String tableName) {
        return dbQuery.primaryKeySql(dataSourceConfig, tableName);
    }

    public Map<String, Object> getCustomFields(ResultSet resultSet) {
        String[] fcs = this.fieldCustom();
        if (null != fcs) {
            Map<String, Object> customMap = CollectionUtils.newHashMapWithExpectedSize(fcs.length);
            for (String fc : fcs) {
                try {
                    customMap.put(fc, resultSet.getObject(fc));
                } catch (SQLException sqlException) {
                    throw new RuntimeException("获取自定义字段错误:", sqlException);
                }
            }
            return customMap;
        }
        return Collections.emptyMap();
    }

    /**
     * 执行 SQL 查询,回调返回结果
     *
     * @param sql      执行SQL
     * @param consumer 结果处理
     */
    public void execute(String sql, Consumer<ResultSetWrapper> consumer) throws SQLException {
        logger.debug("执行SQL:{}", sql);
        int count = 0;
        long start = System.nanoTime();
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql);

View on GitHub (pinned to bf67d90747)

Solutions

  1. Check the SQLException cause in the stack trace: 'column not found' identifies exactly which fieldCustom() entry is wrong for your driver.
  2. Upgrade mybatis-plus-generator to a version whose query adapter matches your database/driver version.
  3. If using a custom IDbQuery, align fieldCustom() with the columns actually produced by your tablesSql().
  4. As a workaround, supply a DataSourceConfig with a SchemaDataSource query override that removes the failing custom field.

Example fix

// before (custom adapter declares a column the ResultSet lacks)
@Override
public String[] fieldCustom() {
    return new String[]{" remarks_bad_name"};
}

// after
@Override
public String[] fieldCustom() {
    return new String[]{"REMARKS"};
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
    generator.generate();
} catch (RuntimeException e) {
    if (e.getCause() instanceof SQLException sqlEx) {
        // identify failing fieldCustom column from sqlEx message; fix IDbQuery/driver version
        log.error("custom field metadata read failed: {}", sqlEx.getMessage(), e);
    }
}

Prevention

When it happens

Trigger: Using a DbType whose IDbQuery implementation declares fieldCustom() columns that the actual query/ResultSet does not return — typically after a driver/dbms version change where metadata column names differ, or when a custom IDbQuery is registered with misspelled field names.

Common situations: ClickHouse/DM/Kingbase and other less-common DbType query adapters whose fieldCustom() list drifts from what the shipped SQL selects; using an older JDBC driver whose metadata result lacks a column (e.g. REMARKS variants) the adapter expects.

Related errors


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