目录

<mapper namespace="demo3.dao.StudentMapper"> 
- @Override
- public List
selectAll() { - List
list = null; - SqlSession sqlSession = null;
- InputStream is = 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;
- }

- @Override
- public Student selectById(Integer sid) {
- Student stu = null;
- SqlSession sqlSession = null;
- InputStream is = 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.通过实现类对象调用方法,接收结果
- stu = mapper.selectById(sid);
- }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 stu;
- }

- @Override
- public Integer insert(Student stu) {
- Integer result = null;
- SqlSession sqlSession = null;
- InputStream is = 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.通过实现类对象调用方法,接收结果
- result = mapper.insert(stu);
- }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 result;
- }

- @Override
- public Integer update(Student stu) {
- Integer result = null;
- SqlSession sqlSession = null;
- InputStream is = 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.通过实现类对象调用方法,接收结果
- result = mapper.update(stu);
- }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 result;
- }

- @Override
- public Integer delete(Integer sid) {
- Integer result = null;
- SqlSession sqlSession = null;
- InputStream is = 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.通过实现类对象调用方法,接收结果
- result = mapper.delete(sid);
- }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 result;
- }