Resources(org.apache.ibatis.io.Resources)
一、Resources 类常用于以下几种情况:
1 从类路径加载 SQL Map 配置文件(如 sqlMap-config.xml)。
2 从类路径加载 DAO Manager 配置文件(如 dao.xml)。
3 从类路径加载各种.properties 文件。
二、加载一个资源有很多方式,包括:
1 对于简单的只读文本数据,加载为 Reader。
2 对于简单的只读二进制或文本数据,加载为 Stream。
3 对于可读写的二进制或文本文件,加载为 File。
4 对于只读的配置属性文件,加载为 Properties。
5 对于只读的通用资源,加载为 URL。
按以上的顺序,Resources 类加载资源的方法如下:
1 Reader getResourceAsReader(String resource);
2 Stream getResourceAsStream(String resource);
3 File getResourceAsFile(String resource);
4 Properties getResourceAsProperties(String resource);
5 Url getResourceAsUrl(String resource);
以上方法的 resource 参数名称应该是全限定名,加上全文件/资源名。例如,如果在类路径中有资源“com/domain/mypackage/MyPropertiesFile.properties”,您使用下面的代码加载资源为 Properties(注意,资源名前面不需要斜杠/)。
String resource = “com/domain/mypackage/MyPropertiesFile.properties”;
Properties props = Resources.getResourceAsProperties (resource);
同样地,您可以从类路径加载 SQL Map 配置文件为一个 Reader。假设它在类路径的
properties 目录下(properties.sqlMap-config.xml)。
String resource = “properties/sqlMap-config.xml”;
Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
原文地址:http://blog.youkuaiyun.com/xiaozaq/article/details/54173807