baomidou/mybatis-plus · error · BuilderException
Environment requires an id attribute.
Error message
Environment requires an id attribute.
What it means
Thrown by isSpecifiedEnvironment when an <environment> element in mybatis-config.xml lacks its id attribute. The id is how MyBatis matches an environment against the requested/default environment; without it the environment is anonymous and cannot be selected, so configuration parsing fails.
Source
Thrown at mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisXMLConfigBuilder.java:430
mapperParser.parse();
}
} else if (resource == null && url == null && mapperClass != null) {
Class<?> mapperInterface = Resources.classForName(mapperClass);
configuration.addMapper(mapperInterface);
} else {
throw new BuilderException(
"A mapper element may only specify a url, resource or class, but not more than one.");
}
}
}
}
private boolean isSpecifiedEnvironment(String id) {
if (environment == null) {
throw new BuilderException("No environment specified.");
}
if (id == null) {
throw new BuilderException("Environment requires an id attribute.");
}
return environment.equals(id);
}
private static Configuration newConfig(Class<? extends Configuration> configClass) {
try {
return configClass.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw new BuilderException("Failed to create a new Configuration instance.", ex);
}
}
}
View on GitHub (pinned to bf67d90747)
Solutions
- Add a unique id to every <environment>: <environment id="development">.
- Ensure the id matches (case-sensitively) the default attribute of <environments> or the environment id passed to build().
- Use one environment per deployment profile (development, test, production) each with a distinct id.
Example fix
<!-- before -->
<environments default="development">
<environment>
<transactionManager type="JDBC"/>
<dataSource type="POOLED">...</dataSource>
</environment>
</environments>
<!-- after -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">...</dataSource>
</environment>
</environments> Defensive patterns
Strategy: validation
Validate before calling
// every <environment> must have an id
for (int i = 0; i < envs.getLength(); i++) {
org.w3c.dom.Element env = (org.w3c.dom.Element) envs.item(i);
if (!env.hasAttribute("id") || env.getAttribute("id").isEmpty())
throw new IllegalStateException("<environment> without id at index " + i);
} Try / catch
Catch BuilderException 'Environment requires an id attribute.'; add a unique id matching the requested/default environment and rebuild.
Prevention
- Give every <environment> a profile-named id (development/test/production).
- Validate the config XML in CI so missing attributes fail the build, not runtime.
When it happens
Trigger: <environment>...</environment> with no id attribute in mybatis-config.xml while an <environments> section is being processed with a specified environment.
Common situations: Hand-written or template-generated configs omitting id; refactoring renaming ids and accidentally deleting one; copy-paste of an environment block where the id attribute line was lost.
Related errors
- Error parsing SQL Mapper Configuration. Cause: %s
- The properties element cannot specify both a URL and a resou
- Environment declaration requires a TransactionFactory.
- Environment declaration requires a DataSourceFactory.
- A mapper element may only specify a url, resource or class,
AI-assisted analysis of baomidou/mybatis-plus@bf67d90747 (2026-08-14).
Data as JSON: /api/errors/6f1d0e02f2a07a73.
Report an issue: GitHub.