user1094865
user1094865

Reputation:

MyBatis cannot find mybatis-config.xml

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?

Upvotes: 5

Views: 5426

Answers (5)

dyy.alex
dyy.alex

Reputation: 534

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

Upvotes: 0

MangduYogii
MangduYogii

Reputation: 985

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.

Upvotes: 0

Woodsman
Woodsman

Reputation: 1179

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.

Upvotes: 0

user1094865
user1094865

Reputation:

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.

Upvotes: 1

Robert Munteanu
Robert Munteanu

Reputation: 68268

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

Upvotes: 0

Related Questions