谈谈Java Resources用法

     做Ibatis的配置文件时,用到了Java Resources,把配置文件放置在项目工程中,直接运行始终找不到资源,只能加上几句Java代码查看类加载路径:

ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();

URL[] urls = ((URLClassLoader) sysClassLoader).getURLs();

for (int i = 0; i < urls.length; i++)

{

System.out.println(urls[i].getFile());

}

输出为:

/D:/wps/club/Mybatis-club/target/test-classes/ 

/D:/wps/club/Mybatis-club/target/classes/

/C:/Users/**/.m2/repository/org/mybatis/mybatis/3.2.2/mybatis-3.2.2.jar

/C:/Users/**/.m2/repository/mysql/mysql-connector-java/5.1.26/mysql-connector-java-5.1.26.jar

/C:/Users/**/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar

把配置文件放到路径下,即可成功。

如果是用Maven 加上资源拷贝的的功能,如:

<build>

<resources>

<resource>

<directory>src/main/resources</directory>

<filtering>true</filtering>

</resource>

<resource>

<directory>

src/main/java/com/jcloud/mybatis/dao

</directory>

<filtering>true</filtering>

<targetPath>com/jcloud/mybatis/dao</targetPath>

<includes>

<include>**/*.xml</include>

</includes>

</resource>

</resources>

即可!

参考:

http://blog.csdn.net/dongwujing/article/details/7751057


你可能感兴趣的:(java,mybatis,resouces)