Java笔记--Mybatis动态代理--2021-05-06

13 篇文章 0 订阅

Mybatis动态代理

一、动态代理

使用SqlSession.getMapper(dao接口.class) 获取这个dao接口的对象,不要自己写实现了

回顾没有动态代理的代码:
StudentDao–Dao层的接口

public interface StudentDao {

    //查询student表的所有的教据
    List<Student> selectStudent();

    //插入方法
    int insertStudent(Student student);

}

StudentDaoImpl–Dao层的接口的实现类

public class StudentDaoImpl implements StudentDao {

    @Override
    public List<Student> selectStudent() {
        // 使用工具类获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        // 定义sql语句的唯一标识
        String sqlId = "com.apps.dao.StudentDao.selectStudent";
        // 查询
        List<Student> studentsList = sqlSession.selectList(sqlId);
        // 关闭
        sqlSession.close();
        return studentsList;
    }

    @Override
    public int insertStudent(Student student) {
        // 使用工具类获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        // 定义sql语句的唯一标识
        String sqlId = "com.apps.dao.StudentDao.insertStudent";
        // 查询
        int insert = sqlSession.insert(sqlId, student);
        // 提交事物
        sqlSession.commit();
        // 关闭
        sqlSession.close();
        // 返回
        return insert;
    }

}

注意:MyBatisUtils.getSqlSession();是自己定义的工具类用于获取SqlSession对象

package com.apps.utils;

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;

/**
 * @author hk Email:2113438464@qq.com
 * @ClassName MyBatisUtils
 * @Description : 作用
 * @date 2021/4/17
 */
public class MyBatisUtils {

    //SqlSessionFactory对象
    // SqlSessionFactory : 重量级对象, 程序创建一个对象耗时比较长,使用资源比较多。
    // 在整个项目中,有一个就够用了。
    private static SqlSessionFactory factory;

    static {
        try {
            // 1、定义mybatis主配置文件的名称,从类路径的根开始(target/clasess)
            String config = "mybatis-config.xml";
            // 2、读取这个config表示的文件--mybatis中的一个类, 负责读取主配置文件
            InputStream in = Resources.getResourceAsStream(config);
            // 3、SqlSessionFactoryBuilder : 创建SqlSessionFactory对象,
            factory  = new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取SqlSession对象的方法
    public static SqlSession getSqlSession() {
        return factory.openSession();
        /*
        openSession()方法说明:
          1. openSession() :无参数的, 获取是非自动提交事务的SqlSession对象
          2. openSession(boolean): openSession(true)  获取自动提交事务的SqlSession.
                                   openSession(false)  非自动提交事务的SqlSession对象
        */
    }

}

回到我们的StudentDaoImpl–Dao层的接口的实现类,我们看selectStudent()方法,我们通过调用sqlSession对象的selectList方法传入Dao接口的方法全限定名称,这个selectList方法底层就是根据方法全限定名称,获取方法返回值,使用指定的sql语句等。实现这个Dao接口中的方法很麻烦,大部分操作都相同。

所以sqlSession对象给了我们getMapper()方法,传入dao接口.class,获取dao接口的实现类对象,它的底层就是通过反射来实现的,如调用getMapper()方法实现selectStudent()方法时,它会获取这方法的全限定名称,通过全限定名称使用指定的sql语,内部区分调用sqlSession对象的selectList()方法,然后关闭对象

public class TestStudentDao {

    @Test
    public void testSelectStudent() {
        /*
        * 使用mybatis的动态代理机制,使用sqlSession.getMapper(dao接口)
        * getMapper()获取dao接口对于的实现类对象
        */
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        // 传入dao接口的class,获取实现类
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        // 调用studentDao的方法,执行数据库的操作
        List<Student> studentList = studentDao.selectStudent();
        // 打印
        studentList.forEach(student -> System.out.println(student));
    }

    @Test
    public void testInsertStudent() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        // 反射
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        // 设置参数
        Student s = new Student(1005,"小李子","sb@qq.com",33);
        // 用studentDao的方法,执行数据库的操作
        int i = studentDao.insertStudent(s);
        // 提交事物
        sqlSession.commit();
        // 打印
        System.out.println(i);
    }

}

不用写dao层实现类,现在只要定义接口,在使用、测试时通过sqlSession.getMapper(接口.class)获取实现类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张德帅-001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值