MyBatis 源码分析-- getMapper(获取Mapper)

前言:

前面我们从源码层面梳理了 SqlSessionFactory、SqlSession 的创建过程,本篇我们继续分析一下 Mapper 的的获取过程。

初识 MyBatis 【MyBatis 核心概念】

MyBatis 源码分析--SqlSessionFactory

MyBatis 源码分析--获取SqlSession

案例代码:

java 复制代码
public class MyBatisTest {
    @Test
    public void test() throws IOException {
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //创建 SqlSessionFactoryBuilder 对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //通过 SqlSessionBuilder 对象 解析 mybatis-config.xml 文件 构建一个SqlSessionFactory 
        SqlSessionFactory sqlSessionFactory = builder.build(is);
        //通过SqlSessionFactory构建一个SqlSession
        SqlSession session = sqlSessionFactory.openSession();
        //通过SqlSession 获取 Mapper 实例
        UserMapper userMapper = session.getMapper(UserMapper.class);
        //获取数据
		List<User> users = userMapper.findAll();
        //打印输出
        for (User user : users) {
            System.out.println(user);
        }
        //关闭资源
        session.close();
        is.close();
    }
}

本篇我们将主要对 session.getMapper(UserMapper.class); 这句代码进行分析。

DefaultSqlSession#getMapper 源码分析

java 复制代码
//org.apache.ibatis.session.defaults.DefaultSqlSession#getMapper
public <T> T getMapper(Class<T> type) {
	return this.configuration.getMapper(type, this);
}


//org.apache.ibatis.session.Configuration#getMapper
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
	return this.mapperRegistry.getMapper(type, sqlSession);
}

MapperRegistry#getMapper 源码分析

java 复制代码
//org.apache.ibatis.binding.MapperRegistry#getMapper
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
	//根据 class 获取 Mapper 代理工厂
	MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory)this.knownMappers.get(type);
	if (mapperProxyFactory == null) {
		throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
	} else {
		try {
			//使用 Mapper 代理工厂创建 Mapper 代理对象返回
			return mapperProxyFactory.newInstance(sqlSession);
		} catch (Exception var5) {
			throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
		}
	}
}

//org.apache.ibatis.binding.MapperProxyFactory#newInstance(org.apache.ibatis.session.SqlSession)
public T newInstance(SqlSession sqlSession) {
	//Mapper 代理
	MapperProxy<T> mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
	//创建 Mapper 代理对象
	return this.newInstance(mapperProxy);
}


//org.apache.ibatis.binding.MapperProxyFactory#newInstance(org.apache.ibatis.binding.MapperProxy<T>)
protected T newInstance(MapperProxy<T> mapperProxy) {
	//使用 JDK 创建一个 Mapper 的代理对象
	return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
}

从源码可以看出,获取 Mapper 其实就是根据 Mapper 的 Class 类型从 MapperRegistry 中获取一个 MapperProxyFactory,最终调用 MapperProxyFactory.newInstance 方法创建一个代理对象,生成一个代理类来调用 Mapper 的方法,这里的 MapperProxyFactory 其实就是在获取 SqlSessionFactory 中存入 MapperRegistry 的 MapperProxyFactory。

欢迎提出建议及对错误的地方指出纠正。

相关推荐
VX_bysjlw98511 小时前
基于微信小程序的宠物用品商城系统-后端74346-计算机毕设原创(免费领源码+带部署教程)
java·redis·微信小程序·eclipse·mybatis·idea·微信开发者工具
hexu_blog16 小时前
springboot3集成shardingsphere4.0 分表分库
java·spring boot·mybatis
乐观的Terry16 小时前
9、发布系统-Webhook自动发布
java·spring boot·spring·spring cloud·mybatis
聆听。。花开雨落1 天前
mybatis的typeHandler 作用
数据库·mybatis
源码宝2 天前
制造工厂如何挑选MES?有哪些注意事项?
源码·mes
我是大卫4 天前
【图】React源码解析-从数据结构、调度器、源码设计模式到状态计算引擎,深挖useReducer原理
前端·react.js·源码
天若有情6734 天前
SpringBoot4 + MyBatis 前后端分离实战|从零实现坏习惯管理系统,支持局域网手机访问CRUD
智能手机·mybatis
不能只会打代码4 天前
Day 011 — Spring 全家桶深度拆解
spring boot·mybatis·spring aop·spring mvc·spring ioc
憧憬成为web高手4 天前
皮卡丘靶场速通--sql 2
数据库·sql·mybatis
我是大卫5 天前
【图】React源码解析-从底层数据结构、渲染调度到闭包原理深挖useRef的核心机制
前端·react.js·源码