Mybatis reports an error: could not find resource mybatis-conf.xml
Screenshot of error reporting:
Error content:
java.io.IOException: Could not find resource mybatis-conf.xml at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114) at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100) at org.apache.ibatis.io.Resources.getResourceAsReader(Resources.java:160) at MybatisAdvancedTest.testQueryByNo(MybatisAdvancedTest.java:23)
The default solution is that his article is compiled in the ide-src-jebat.xml directory, which is not posted on my ide-jebat When the XML file is in the resources directory, debug goes in to watch the process and finds resources Getresourceasreader is not loaded into this XML configuration file at all
Code diagram:
// Query a student based on their school number @Test public void testQueryByNo() throws IOException { String resource = "mybatis-conf.xml"; Reader reader = Resources. getResourceAsReader(resource); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); // pass in the StudentMapper interface, return the mapper proxy object studentMapper studentMapper = session.getMapper( StudentMapper.class);//interface //pass the mapper proxy object studentMapper to call the methods in the ISTudentMapper interface Student student = studentMapper.queryStudentByNo(1); System.out.println(student+"****"); session.close(); }
Solution 1:
Replace this part of the code in the test method above:
String resource = "mybatis-conf.xml"; Reader reader = Resources.getResourceAsReader(resource);
Replace with
Reader reader = Resources.getResourceAsReader("mybatis-conf.xml");
If the xml is not compiled properly by idea, such as IDEA development, but not mybatis-config.xml in the resources directory, for example, in src/main/java, then add the build code at the end of pom.xml to tell idea to compile our configuration file:
Solution 2:
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
Solution 3:
Move the configuration file to the resources directory and rebuild our project
Read More:
- Build a mybatis and it will appear session.selectOne Method error
- [Solved] Could not find resource COM / atguigu / Dao / studentdao.xm, the mapper file for storing SQL statements could not be found and an error occurred
- Byte 1 of UTF-8 sequence of error creating document instance. And bytes is invalid
- How to Solve Mybatis error: invalid bound statement (not found)
- Mybatis Error: The error may exist in xxxxMapper.xml [How to Solve]
- [Solved] BindingException: Type interface XXX is not known to the MapperRegistry
- Mybatis Error: Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.
- [Solved] ### Error building SqlSession. ### The error may exist in com/atguigu/dao/SysUserMapper.xml ### Caus
- Mybatis reports an error (error building sqlsession.) when using annotations without deleting redundant files
- Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber
- [Solved] Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException
- [Solved] mybatis Error: Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.
- [Solved] mybatis Error querying database. Cause: java.sql.SQLException: The server time zone value
- Resources is configured in the build of Maven project to prevent the failure of resource export
- [How to Solve] Invalid bound statement (not found)
- [Solved] Mybatis-Plus Error: Invalid bound statement (not found)
- Mybatis single parameter pass in exception (How to Fix)
- [Solved] Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration
- [Solved] Error creating bean with name ‘fileVisitController‘ defined in file
- Mybatis plus configuration console prints complete SQL statement with parameters