Four, MyBatis framework Dao dynamic agent

1.1 Step

(1) Remove the Dao interface implementation class written before

(2) getMapper to obtain the proxy object

Just call the getMapper() method of SqlSession to get the implementation class object of the specified interface . The parameter of this method is the class value of the specified Dao interface class.

Do not use tools:

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

Use tools:

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

(3) Use Dao proxy object method to execute sql statement

@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);
    }

Based on the previous blog post 3. MyBatis uses the traditional Dao development method , only the following changes have been made:

  1. Delete the implementation class StudentDaoImpl of dao
  2. Modify the code of TestMybatis:

So please refer to the missing code. 3. MyBatis uses the traditional Dao development method

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);
    }

}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44827418/article/details/111409205