{"record":{"id":"f4cf603a3fb7e5ee","repo":"baomidou/mybatis-plus","slug":"no-environment-specified","errorCode":null,"errorMessage":"No environment specified.","messagePattern":"No environment specified\\.","errorType":"exception","errorClass":"BuilderException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisXMLConfigBuilder.java","lineNumber":427,"sourceCode":"                    try (InputStream inputStream = Resources.getUrlAsStream(url)) {\n                        XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url,\n                            configuration.getSqlFragments());\n                        mapperParser.parse();\n                    }\n                } else if (resource == null && url == null && mapperClass != null) {\n                    Class<?> mapperInterface = Resources.classForName(mapperClass);\n                    configuration.addMapper(mapperInterface);\n                } else {\n                    throw new BuilderException(\n                        \"A mapper element may only specify a url, resource or class, but not more than one.\");\n                }\n            }\n        }\n    }\n\n    private boolean isSpecifiedEnvironment(String id) {\n        if (environment == null) {\n            throw new BuilderException(\"No environment specified.\");\n        }\n        if (id == null) {\n            throw new BuilderException(\"Environment requires an id attribute.\");\n        }\n        return environment.equals(id);\n    }\n\n    private static Configuration newConfig(Class<? extends Configuration> configClass) {\n        try {\n            return configClass.getDeclaredConstructor().newInstance();\n        } catch (Exception ex) {\n            throw new BuilderException(\"Failed to create a new Configuration instance.\", ex);\n        }\n    }\n\n}\n","sourceCodeStart":409,"sourceCodeEnd":444,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisXMLConfigBuilder.java#L409-L444","documentation":"Thrown by isSpecifiedEnvironment when the builder was constructed without an environment id and encounters an <environments> section trying to match one. The environment field is set only when the caller (e.g. SqlSessionFactoryBuilder.build(config, environmentId)) supplies one; with it null, MyBatis cannot select which <environment> to activate and fails.","triggerScenarios":"Building with new SqlSessionFactoryBuilder().build(reader) — no environment argument — while the mybatis-config.xml contains multiple <environment> definitions so no default can be inferred, or explicitly calling build(reader, null).","commonSituations":"Adding a second <environment> to a config previously parsed with a single default environment; wrapping the builder in custom code that drops the environment parameter; tests reusing a config file with several environments but no environment id passed.","solutions":["Pass an explicit environment id: new SqlSessionFactoryBuilder().build(reader, \"development\").","Or mark one environment as default via <environments default=\"development\"> so parsing can pick it automatically.","In Spring Boot, delete the <environments> block entirely and inject the Spring-managed DataSource."],"exampleFix":"// before\nSqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);\n\n// after\nSqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, \"development\");","handlingStrategy":"validation","validationCode":"// decide environment id up front and pass it explicitly\nString envId = System.getProperty(\"app.env\", \"development\");\n// ensure it exists in the XML before building\nSqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, envId);","typeGuard":null,"tryCatchPattern":"Catch BuilderException 'No environment specified.' from build(); re-run with an explicit environment id argument or set the default attribute in <environments>.","preventionTips":["Always pass an environment id to SqlSessionFactoryBuilder.build in multi-environment configs.","Externalize the environment id per deployment profile."],"tags":["mybatis","mybatis-plus","xml-config","environment","initialization"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}