前言
org.apache.ibatis.io.Resources工具类用于读取MyBatis的配置文件,转换成输入流,最终用于获取SqlSessionFactory。
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
主要方法
设置默认的ClassLoader
public static void setDefaultClassLoader(ClassLoader defaultClassLoader);
获取配置文件的绝对路径
public static URL getResourceURL(String resource);
public static URL getResourceURL(ClassLoader loader, String resource);
例子:
try {
System.out.println("ResourceURL = " + Resources.getResourceURL("mybatis-config.xml"));
} catch (Exception ex) {
}
//输出
ResourceURL = file:/home/lgj/aProject/MyBatis/target/classes/mybatis-config.xml
获取配置文件的输入流(字节流和字符流)
//使用相对路径,也就是类路径下
//mybatis-config.xml
public static InputStream getResourceAsStream(String resource);
public static InputStream getResourceAsStream(ClassLoader loader, String resource);
public static Reader getResourceAsReader(String resource);
public static Reader getResourceAsReader(ClassLoader loader, String resource);
//使用url ,也就是使用相对于根目录的路径
//file:/home/lgj/aProject/MyBatis/target/classes/mybatis-config.xml
public static InputStream getUrlAsStream(String urlString);
public static Reader getUrlAsReader(String urlString);
例子:
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
获取配置文件,返回为File对象
public static File getResourceAsFile(String resource);
public static File getResourceAsFile(ClassLoader loader, String resource);
public static File getResourceAsFile(String resource) ;
public static File getResourceAsFile(ClassLoader loader, String resource);
获取配置文件中的Properties对象
测试时输出各个节点和内容,不确定这两个方法具体有什么用
通过相对路径
public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {
Properties props = new Properties();
InputStream in = getResourceAsStream(loader, resource);
props.load(in);
in.close();
return props;
}
通过绝对路径
public static Properties getUrlAsProperties(String urlString) throws IOException {
Properties props = new Properties();
InputStream in = getUrlAsStream(urlString);
props.load(in);
in.close();
return props;
}
例子
try {
Properties properties = Resources.getResourceAsProperties("mybatis-config.xml");
Enumeration<Object> enumeration = properties.keys();
while (enumeration.hasMoreElements()){
System.out.println(enumeration.nextElement());
}
Collection<Object> values = properties.values();
for(Object v:values){
System.out.println(v);
}
// properties.keys();
} catch (Exception ex) {
} finally {
}
输出(注释部分出现乱码)
</settings>
</mappers>
</typeAliases>
<property
<environments
<properties
</configuration>
<transactionManager
<!DOCTYPE
<dataSource
<environment
</transactionManager>
<!--
<?xml
</dataSource>
<package
<typeAliases>
</environments>
<configuration>
</properties>
</environment>
<setting
<settings>
<mappers>
PUBLIC
"http
|
name="password" value="563739007"/>
default="development">
resource="config.properties">
type="JDBC">
configuration
type="UNPOOLED">
id="development">
å
¨å±€å¯ç”¨æˆ–ç¦ç”¨å»¶è¿ŸåŠ 载。当ç¦ç”¨æ—¶ï¼Œæ‰€æœ‰å
³è”对象都会å³æ—¶åŠ è½½ -->
version="1.0" encoding="UTF-8" ?>
name="com.mybatis.dao.mapper"/>
name="mapUnderscoreToCamelCase" value="true"/>
"-//mybatis.org//DTD Config 3.0//EN"
//mybatis.org/dtd/mybatis-3-config.dtd">
-->