为什么读取不到configuration.xml
package org.ge.tbrqcms.db; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; /** * 访问数据库类 * @author geyi * */ public class DBAccess { String resources = "main/java/org/ge/tbrqcms/config/Configuration.xml"; public SqlSession getSqlSession() throws IOException{ //通过配置文件获取数据库连接 Reader render = Resources.getResourceAsReader(resources); //通过配置信息构建一个SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(render); //通过SqlSessionFactory打开一个数据库会话 SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } }
用main函数测试DBAccess类的时候就报错了。说找不到路径
我的文件目录:
UserDao类:
package org.ge.tbrqcms.dao; import java.io.IOException; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.ge.tbrqcms.db.DBAccess; import org.ge.tbrqcms.model.User; /** * 和user表相关的数据库操作 * @author geyi * */ public class UserDao { public List<User> queryUserList(String userId){ DBAccess dbAccess = new DBAccess(); SqlSession sqlSession = null; try { sqlSession = dbAccess.getSqlSession(); //通过sqlSession执行sql语句 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if (sqlSession != null) { sqlSession.close(); } } return null; } public static void main(String[] args){ UserDao userDao = new UserDao(); userDao.queryUserList(""); } }