mybatis 获取对应的mapper

本文探讨了在MyBatis中获取Mapper的过程,从DefaultSqlSession的getMapper开始,经由Configuration和MapperRegistry,最后通过MapperProxyFactory创建代理对象。MapperProxy作为InvocationHandler,持有SqlSession并实现动态调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看下执行下面这行代码的时候,都发生了什么

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

org.apache.ibatis.session.defaults.DefaultSqlSession#getMapper 

public <T> T getMapper(Class<T> type) {
    return configuration.<T>getMapper(type, this);
}

org.apache.ibatis.session.Configuration#getMapper

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
}

org.apache.ibatis.binding.MapperRegistry#getMapper

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
        throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
        return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
        throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
}

org.apache.ibatis.binding.MapperProxyFactory#newInstance(org.apache.ibatis.session.SqlSession)

public T newInstance(SqlSession sqlSession) {
    //创建mapperProxy,封装sqlSession等属性值
    final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
}

org.apache.ibatis.binding.MapperProxyFactory#newInstance(org.apache.ibatis.binding.MapperProxy<T>)

protected T newInstance(MapperProxy<T> mapperProxy) {
    //创建代理对象,要注意的是创建对象时最后一个参数mapperProxy
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}

总结:简单的来说,获取mapper就是创建一个代理对象,创建代理对象时所用的InvocationHandler是org.apache.ibatis.binding.MapperProxy。这个类实现了InvocationHandler接口,并封装了sqlSession属性。

public class MapperProxy<T> implements InvocationHandler, Serializable {

    private static final long serialVersionUID = -6424540398559729838L;
    private final SqlSession sqlSession;
    private final Class<T> mapperInterface;
    private final Map<Method, MapperMethod> methodCache;

    public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
        this.methodCache = methodCache;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            if (Object.class.equals(method.getDeclaringClass())) {
                return method.invoke(this, args);
            } else if (isDefaultMethod(method)) {
                return invokeDefaultMethod(proxy, method, args);
            }
        } catch (Throwable t) {
            throw ExceptionUtil.unwrapThrowable(t);
        }
        final MapperMethod mapperMethod = cachedMapperMethod(method);
        return mapperMethod.execute(sqlSession, args);
    }
    /**
     * 省略其他代码
     */
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值