MyBatis入门 -- 2、使用MyBatis实现传统Dao层

介绍

MyBatis传统方式实现Dao层

流程 : html页面 - 控制层 - 业务层 - 持久层 - DB

解决Dao开发存在的问题---Mapper接口开发

实现

controller控制层
StudentController.java
public class StudentController {
    //创建业务层对象
    private StudentService service = new StudentServiceImpl();

    //查询全部功能测试
    @Test
    public void selectAll(){
        List<Student> students = service.selectAll();
        for (Student student : students) {
            System.out.println(student);
        }
    }
    //新增功能测试
    /**
     * 注意:
     * 1、i=sqlSession.insert("StudentMapper.insert",student);传参数
     * 2、sqlSession.commit(); 提交事务
     */
    @Test
    public void insert(){
        Student student = new Student(2,"李四",24);
        Integer result = service.insert(student);
        System.out.println(result);
    }
}
service业务层
StudentService.java
public interface StudentService {
    List<Student> selectAll();
    Integer insert(Student student);
}
StudentServiceImpl.java
public class StudentServiceImpl implements StudentService {
    //创建持久层对象
    private StudentMapper mapper = new StudentMapperImpl();
    @Override
    public List<Student> selectAll() {
        return mapper.selectAll();
    }
    @Override
    public Integer insert(Student student) {
        return mapper.insert(student);
    }
}
mapper持久层
StudentMapper.java
public interface StudentMapper{
    List<Student> selectAll();
    Integer insert(Student student);
}
StudentMapperImpl.java
/**
 *  持久层实现类
 */
public class StudentMapperImpl implements StudentMapper {

    @Override
    public List<Student> selectAll() {
        List<Student> 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.执行映射配置文件中的sql语句,并接收结果
            list = sqlSession.selectList("StudentMapper.selectAll");

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

    @Override
    public Integer insert(Student student) {
        InputStream is = null;
        SqlSession sqlSession = null;
        Integer i = null;
        try {
            //1、加载资源
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //2、获取SqlSession
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession();
            //3、执行操作
            i = sqlSession.insert("StudentMapper.insert",student);
            //4、提交事务
            sqlSession.commit();

        } catch (IOException e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return i;
  	 }
}

还是麻烦,优化:使用接口代理的方式实现Dao层

镜花水月
原文地址:https://www.cnblogs.com/fengbingshui/p/13413024.html