四、MyBatis 框架 Dao 动态代理

1.1 步骤

(1) 去掉 之前编写的Dao 接口实现类

(2) getMapper 获取代理对象

只需调用 SqlSession 的 getMapper()方法,即可获取指定接口的实现类对象。该方法的参数为指定 Dao接口类的 class 值。

不使用工具类:

SqlSession session = factory.openSession();
StudentDao dao = session.getMapper(StudentDao.class);

使用工具类:

SqlSession sqlSession = MybatisUtils.getSqlSession();
// 这句代码可以自动创建dao接口的实现类对象
StudentDao dao = sqlSession.getMapper(StudentDao.class); 

(3) 使用 Dao 代理对象方法执行 sql 语句

@Test
    public void testSelectStudents() {
    
    
        /**
         * 使用mybatis的动态代理机制,使用SqlSession.getMapper(dao接口)
         * getMapper能够获取dao接口对应的实现类对象。
         */
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
        //调用dao的方法,执行数据库的操作
        List<Student> students = dao.selectStudents();
        for (Student student : students) {
    
    
            System.out.println("学生=" + student);
        }
    }

    @Test
    public void testInsertStudents() {
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
        student.setId(1004);
        student.setName("亚瑟");
        student.setEmail("[email protected]");
        student.setAge(35);
        int nums = dao.insertStudent(student);
        sqlSession.commit();
        System.out.println("添加对象的数量:" + nums);
    }

    @Test
    public void testUpdateStudents() {
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
        student.setId(1002);
        student.setAge(222);
        int nums = dao.updateStudent(student);
        sqlSession.commit();
        System.out.println("更新对象的数量:" + nums);
    }

    @Test
    public void testDeleteStudents() {
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        int nums = dao.deleteStudent(1003);
        sqlSession.commit();
        System.out.println("删除对象的数量:" + nums);
    }

在上一篇博文三、MyBatis 使用传统 Dao 开发方式的基础上,只做了以下更改:

  1. 删除dao的实现类StudentDaoImpl
  2. 修改TestMybatis的代码:

所以缺少的代码请参考三、MyBatis 使用传统 Dao 开发方式

package com.zep;

import com.zep.dao.StudentDao;
import com.zep.domain.Student;
import com.zep.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class TestMybatis {
    
    

    @Test
    public void testSelectStudents() {
    
    
        /**
         * 使用mybatis的动态代理机制,使用SqlSession.getMapper(dao接口)
         * getMapper能够获取dao接口对应的实现类对象。
         */
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
        //调用dao的方法,执行数据库的操作
        List<Student> students = dao.selectStudents();
        for (Student student : students) {
    
    
            System.out.println("学生=" + student);
        }
    }

    @Test
    public void testInsertStudents() {
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
        student.setId(1004);
        student.setName("亚瑟");
        student.setEmail("[email protected]");
        student.setAge(35);
        int nums = dao.insertStudent(student);
        sqlSession.commit();
        System.out.println("添加对象的数量:" + nums);
    }

    @Test
    public void testUpdateStudents() {
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
        student.setId(1002);
        student.setAge(222);
        int nums = dao.updateStudent(student);
        sqlSession.commit();
        System.out.println("更新对象的数量:" + nums);
    }

    @Test
    public void testDeleteStudents() {
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        int nums = dao.deleteStudent(1003);
        sqlSession.commit();
        System.out.println("删除对象的数量:" + nums);
    }

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44827418/article/details/111409205