如何在Eclipse中引用不在src中的文件
我正在尝试获取 MyBatis 的资源。该教程指出,我的连接工厂中需要以下内容:
String resource = "org/mybatis/example/Configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
我的目录结构是:
src/ com/ 实用程序/ MyBatisConnectionFactory.java 配置/ Configuration.xml
我在引用配置文件时遇到问题。我尝试了“config/Configuration.xml”、“Configuration.xml”和“/config/Configuration.xml”。
有人知道该怎么做吗?
I'm trying to get a resource for MyBatis. The tutorial states that I will need the following in my Connection Factory:
String resource = "org/mybatis/example/Configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
My directory structure is:
src/
com/
utils/
MyBatisConnectionFactory.java
config/
Configuration.xml
I am having troubles referencing the configuration file. I tried "config/Configuration.xml", "Configuration.xml" and "/config/Configuration.xml".
Anyone have a good idea for what to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
config
目录添加为源文件夹(右键单击 > 构建路径 > 用作源文件夹)。因此,您的配置文件将位于类路径的根目录中,并且可以通过 getClass().getResourceAsStream("/Configuration.xml") 访问
You can add your
config
directory as a source-folder (right-click > build path > use as source folder).Thus your configuration files will go on the root of the classpath and will be accessible via
getClass().getResourceAsStream("/Configuration.xml")
使用
getResourcesAsStream()
而不是Resources.getResourceAsReader()
通过类路径打开文件 例如:Open up the file via the classpath using
getResourcesAsStream()
rather thanResources.getResourceAsReader()
For example: