MyBatis cannot find mybatis-config.xml

Viewed 2973

I'm some trouble getting MyBatis (3.4.6) working.

I have placed my mybatis-config.xml file in the src/main/resources folder of my project but when I run a unit test I get the following error.

> java.io.IOException: Could not find resource mybatis-config.xml   at
> org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)

This is the code I am using to read the XML file.

String resource = "mybatis-config.xml";
try 
{
    InputStream inputStream = Resources.getResourceAsStream(resource);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

} catch (IOException ex) {

    ex.printStackTrace();
}

Anyone got any ideas?

5 Answers

Thanks for the input folks, I figured out what was wrong.

It was the weirdest Eclipse config issue I have seen in 15 years of programming, somehow all the XML files in the project were marked as TEXT files.

Think it might be time to move to IntelliJ.

It all depends on what your Resources class is doing. Maybe try with "/mybatis-config.xml" ( note the leading slash ).

I realize this is an old question but for those who stumble on this I'd place that config.xml file under src/test/resources in addition to the one for regular code. You might want a different test configuration.

You can use SQLMapClient for reading the SqlConfig file.

private static String path= "/sqlmap-config.xml";

public static SqlMapClient getSqlMapInstance() throws IOException {
        Reader reader = Resources.getResourceAsReader(path);
        sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);

        return sqlMapClient;

    }

make sure your sqlmap-config.xml is located in classpath resoures folder.

If you use IDEA, you could try mark resources folder as root resources file so that the xml file could be found by program.

Related