栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis 程序之以传统方式实现Dao层

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis 程序之以传统方式实现Dao层

一、持久层 mapper

1、StudentMapper接口

package com.itheima.mapper;

import com.itheima.bean.Student;

import java.util.List;


public interface StudentMapper {
    //查询全部
    public abstract List selectAll();

    //根据id查询
    public abstract Student selectById(Integer id);

    //新增数据
    public abstract Integer insert(Student stu);

    //删除数据
    public abstract Integer delete(Integer id);

    //修改数据
    public abstract Integer update(Student stu);
}

2、StudentMapperImpl接口的实现类

package com.itheima.mapper.impl;

import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
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.InputStream;
import java.util.List;


        
public class StudentMapperImpl implements 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对象
            sqlSession = sqlSessionFactory.openSession(true);

            //4、调用方法执行sql语句,并接收结果
            list = sqlSession.selectList("StudentMapper.selectAll");
            //5、处理结果

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //6、释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    @Override
    public Student selectById(Integer id) {
        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对象
            sqlSession = sqlSessionFactory.openSession(true);

            //4、调用方法执行sql语句,并接收结果
            stu = sqlSession.selectOne("StudentMapper.selectById",id);
            //5、处理结果

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //6、释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        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对象
            sqlSession = sqlSessionFactory.openSession(true);

            //4、调用方法执行sql语句,并接收结果
            result = sqlSession.insert("StudentMapper.insert",stu);
            //5、处理结果

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //6、释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Override
    public Integer delete(Integer id) {
        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对象
            sqlSession = sqlSessionFactory.openSession(true);

            //4、调用方法执行sql语句,并接收结果
            result = sqlSession.delete("StudentMapper.delete",id);
            //5、处理结果

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //6、释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        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对象
            sqlSession = sqlSessionFactory.openSession(true);

            //4、调用方法执行sql语句,并接收结果
            result = sqlSession.update("StudentMapper.update",stu);
            //5、处理结果

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //6、释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

二、service业务层

1、StudentService接口

package com.itheima.service;

import com.itheima.bean.Student;

import java.util.List;


    
public interface StudentService {
    //查询全部
    public abstract List selectAll();

    //根据id查询
    public abstract Student selectById(Integer id);

    //新增数据
    public abstract Integer insert(Student stu);

    //删除数据
    public abstract Integer delete(Integer id);

    //修改数据
    public abstract Integer update(Student stu);
}

2、StudentServiceImpl接口的实现类

package com.itheima.service.impl;

import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.mapper.impl.StudentMapperImpl;
import com.itheima.service.StudentService;
import com.sun.xml.internal.fastinfoset.util.ValueArrayResourceException;
import jdk.nashorn.internal.ir.CallNode;

import java.util.List;


    //业务层实现类
public class StudentServiceImpl implements StudentService {

    //创建持久层对象
    private StudentMapper mapper = new StudentMapperImpl();

    @Override
    public List selectAll() {
        return mapper.selectAll();
    }

    @Override
    public Student selectById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public Integer insert(Student stu) {
        return mapper.insert(stu);
    }

    @Override
    public Integer delete(Integer id) {
        return mapper.delete(id);
    }

    @Override
    public Integer update(Student stu) {
        return mapper.update(stu);
    }
}

三、cantroller控制层

此处只要是对代码进行一个测试

package com.itheima.controller;

import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import jdk.nashorn.internal.ir.CallNode;
import org.junit.jupiter.api.Test;

import java.util.List;


    //控制层测试类
    public class StudentController {
    //创建业务层对象
    private StudentService service = new StudentServiceImpl();

    @Test
    public void selectAll(){
        List students = service.selectAll();
        for(Student stu : students){
            System.out.println(stu);
        }
    }

    @Test
    public void selectById(){
        Student student = service.selectById(2);
        System.out.println(student);
    }

    @Test
    public void insert(){
        Student student1 = new Student(3, "焦子逊", 24);
        Integer result = service.insert(student1);
        System.out.println(result);
    }

    @Test
    public void delete(){
        Integer result = service.delete(3);
        System.out.println(result);
    }

    @Test
    public void update(){
        Student student1 = new Student(2, "焦子逊", 24);
        Integer result = service.update(student1);
        System.out.println(result);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/298856.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号