解决java.io.IOException: Could not find resource SqlMapConfig.xml
可以参照如下,将SqlMapConfig.xml放到src下
![
public class Test {
private SqlSessionFactory sqlSessionFactory = null;
@org.junit.Test
public void finduserbyid() throws IOException{
// 1. 创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 2. 加载SqlMapConfig.xml配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 3. 创建SqlSessionFactory对象
this.sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
// 4. 创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 5. 执行SqlSession对象执行查询,获取结果User
// 第一个参数是User.xml的statement的id,第二个参数是执行sql需要的参数;
sqlSession.selectList("userbatis.queryUserById", 10);
}
}