![](https://s5-media.51cto.com/ost/pc/static/noavatar.gif)
回复
本博客由 金陵科技学院-开放原子开源社 徐晨璐编写
@Override
public List<Student> selectAll(){
SqlSession sqlSession=null;
InputStream is=null;
List<Student> list=null;
try{
//1.加载核心配置文件
is= Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMApper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//StudentMapper mapper =new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
list = mapper.selectAll();
}catch (Exception e){
e.printStackTrace();
}finally {
//6.释放资源
if(sqlSession!=null)
sqlSession.close();
if(is!=null){
try {
is.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
//7.返回结果
return list;
}