MyBatis 找不到mybatis-config.xml
MyBatis cannot find mybatis-config.xml
我在 MyBatis (3.4.6) 工作时遇到了一些问题。
我已经将我的 mybatis-config.xml 文件放在我项目的 src/main/resources 文件夹中但是当我 运行 单元测试出现以下错误。
> java.io.IOException: Could not find resource mybatis-config.xml at
> org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
这是我用来读取 XML 文件的代码。
String resource = "mybatis-config.xml";
try
{
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException ex) {
ex.printStackTrace();
}
有人有什么想法吗?
这完全取决于您的 Resources
class 在做什么。也许尝试使用“/mybatis-config.xml”(注意前导斜杠)。
感谢大家的意见,我知道哪里出了问题。
这是我在 15 年的编程中遇到的最奇怪的 Eclipse 配置问题,项目中的所有 XML 文件都以某种方式标记为文本文件。
认为是时候转向 IntelliJ 了。
我知道这是一个老问题,但对于那些偶然发现这个问题的人来说,除了用于常规代码的文件外,我还将 config.xml 文件放在 src/test/resources 下。您可能需要不同的测试配置。
您可以使用 SQLMapClient
读取 SqlConfig 文件。
private static String path= "/sqlmap-config.xml";
public static SqlMapClient getSqlMapInstance() throws IOException {
Reader reader = Resources.getResourceAsReader(path);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
return sqlMapClient;
}
确保您的 sqlmap-config.xml
位于类路径 resoures
文件夹中。
如果您使用 IDEA,您可以尝试将资源文件夹标记为根资源文件,以便程序可以找到 xml 文件。
我在 MyBatis (3.4.6) 工作时遇到了一些问题。
我已经将我的 mybatis-config.xml 文件放在我项目的 src/main/resources 文件夹中但是当我 运行 单元测试出现以下错误。
> java.io.IOException: Could not find resource mybatis-config.xml at
> org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
这是我用来读取 XML 文件的代码。
String resource = "mybatis-config.xml";
try
{
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException ex) {
ex.printStackTrace();
}
有人有什么想法吗?
这完全取决于您的 Resources
class 在做什么。也许尝试使用“/mybatis-config.xml”(注意前导斜杠)。
感谢大家的意见,我知道哪里出了问题。
这是我在 15 年的编程中遇到的最奇怪的 Eclipse 配置问题,项目中的所有 XML 文件都以某种方式标记为文本文件。
认为是时候转向 IntelliJ 了。
我知道这是一个老问题,但对于那些偶然发现这个问题的人来说,除了用于常规代码的文件外,我还将 config.xml 文件放在 src/test/resources 下。您可能需要不同的测试配置。
您可以使用 SQLMapClient
读取 SqlConfig 文件。
private static String path= "/sqlmap-config.xml";
public static SqlMapClient getSqlMapInstance() throws IOException {
Reader reader = Resources.getResourceAsReader(path);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
return sqlMapClient;
}
确保您的 sqlmap-config.xml
位于类路径 resoures
文件夹中。
如果您使用 IDEA,您可以尝试将资源文件夹标记为根资源文件,以便程序可以找到 xml 文件。