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:
- [Solved] Mybatis.generator error: Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2
- [Solved] Tk-Mybatis Error: tk.mybatis.mapper.MapperException:
- Mybatis Error: The error may exist in xxxxMapper.xml [How to Solve]
- [Solved] mybatis plus Insert Error: mybatis plus Error setting null for parameter #1 with JdbcType OTHER
- How to Solve Mybatis error: invalid bound statement (not found)
- [Solved] Mybatis-Plus Error: Invalid bound statement (not found)
- [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
- Build a mybatis and it will appear session.selectOne Method error
- Mybatis Error: Invalid bound statement (not found)
- Springboot mybatis Integrate Error: Invalid bound statement (not found): com…DepartmentMapper.save
- [Solved] Mybatis Batch Modify Error: multi-statement not allow
- Mybatis single parameter pass in exception (How to Fix)
- [Solved] mybatis Error querying database. Cause: java.sql.SQLException: The server time zone value
- How to Solve SQL comments error in the mybatis query
- Mybatis plus configuration console prints complete SQL statement with parameters
- Mybatis sets the primary key Auto-Increment error: No setter found for the keyProperty
- [Solved] Mybatis Error: attempting to get column ‘XX’ from result set
- Mybatis Error: Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.
- mybatis-plus Common Error and Their Solution
- Mybatis reports an error (error building sqlsession.) when using annotations without deleting redundant files