[Solved] Mybatis Error: Could not find resource mybatis-conf.xml

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: