baomidou/mybatis-plus · critical · IOException

Failed to parse config resource:

Error message

Failed to parse config resource: 

What it means

Thrown by MybatisSqlSessionFactoryBean.buildSqlSessionFactory when the MyBatis configuration XML (configLocation resource) fails to parse: any exception from xmlConfigBuilder.parse() is wrapped in IOException('Failed to parse config resource: ' + configLocation). The root cause in the exception chain is the actual XML/D TD/property error.

Source

Thrown at mybatis-plus-spring/src/main/java/com/baomidou/mybatisplus/spring/MybatisSqlSessionFactoryBean.java:661

        Optional.ofNullable(this.defaultScriptingLanguageDriver)
            .ifPresent(targetConfiguration::setDefaultScriptingLanguage);

        if (this.databaseIdProvider != null) {// fix #64 set databaseId before parse mapper xmls
            try {
                targetConfiguration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
            } catch (SQLException e) {
                throw new IOException("Failed getting a databaseId", e);
            }
        }

        Optional.ofNullable(this.cache).ifPresent(targetConfiguration::addCache);

        if (xmlConfigBuilder != null) {
            try {
                xmlConfigBuilder.parse();
                LOGGER.debug(() -> "Parsed configuration file: '" + this.configLocation + "'");
            } catch (Exception ex) {
                throw new IOException("Failed to parse config resource: " + this.configLocation, ex);
            } finally {
                ErrorContext.instance().reset();
            }
        }

        targetConfiguration.setEnvironment(new Environment(this.environment,
            this.transactionFactory == null ? new SpringManagedTransactionFactory() : this.transactionFactory,
            this.dataSource));

        if (this.mapperLocations != null) {
            if (this.mapperLocations.length == 0) {
                LOGGER.warn(() -> "Property 'mapperLocations' was specified but matching resources are not found.");
            } else {
                for (Resource mapperLocation : this.mapperLocations) {
                    if (mapperLocation == null) {
                        continue;
                    }
                    try {

View on GitHub (pinned to bf67d90747)

Solutions

  1. Read the cause exception — MyBatis reports the exact element/attribute that failed; fix that line in the config file
  2. Validate the XML is well-formed (IDE validation, xmllint) and matches the MyBatis configuration DTD
  3. Check any <properties resource=...> references resolve on the classpath
  4. Remove settings duplicated between configLocation XML and Java-side setters that conflict during parse

Example fix

<!-- before: typo in element -->
<configuration>
  <setting name="mapUnderscoreToCamelCase" value="true"/>
</configuration>
<!-- after -->
<configuration>
  <settings>
    <setting name="mapUnderscoreToCamelCase" value="true"/>
  </settings>
</configuration>
Defensive patterns

Strategy: validation

Validate before calling

// Validate mybatis-config.xml is well-formed before wiring it
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
try (InputStream in = configLocation.getInputStream()) {
    f.newDocumentBuilder().parse(in); // throws on malformed XML
}

Try / catch

try {
    factory.getObject();
} catch (IOException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Failed to parse config resource:")) {
        throw new IllegalStateException("Invalid mybatis-config.xml: " + e.getCause().getMessage(), e.getCause());
    }
    throw e;
}

Prevention

When it happens

Trigger: Setting configLocation on MybatisSqlSessionFactoryBean to a mybatis-config.xml that is malformed XML, references an undefined or missing properties file, uses invalid settings names, or contains unsupported element ordering.

Common situations: Hand-editing mybatis-config.xml and breaking tags; referencing classpath resources that moved; environment-specific config overlays; upgrading mybatis-plus where previously-tolerated settings become strict.

Understand the failure class

Related errors


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