RxJava编程语言爱好者Java服务器端编程

userMapper.getUser(1);底层实现原理

2021-02-06  本文已影响0人  迦叶_金色的人生_荣耀而又辉煌

上一篇 <<<sqlSession如何获得具体的Mapper接口信息
下一篇 >>>sqlSession.selectOne底层实现原理


userMapper实际代理类:org.apache.ibatis.binding.MapperProxy@279ad2e3

代理层实现

if (Object.class.equals(method.getDeclaringClass())) {
//如果是实现类,则直接调用
    try {
        return method.invoke(this, args);
    } catch (Throwable var5) {
        throw ExceptionUtil.unwrapThrowable(var5);
    }
} else {
//接口层走这里
    MapperMethod mapperMethod = MapperProxy.cachedMapperMethod(method);
    {
 
            mapperMethod = new MapperMethod(MapperProxy.mapperInterface, method, MapperProxy.sqlSession.getConfiguration());
            MapperMethod.command = new MapperMethod.SqlCommand(config, mapperInterface, method);
                    --configuration.getMappedStatement(statementName); 从config中获取执行的语句ID和内容
            MapperMethod.method = new MapperMethod.MethodSignature(config, method);
            //做好缓存
            MapperProxy.methodCache.put(method, mapperMethod);
            method:public abstract com.jgspx.entity.UserEntity com.jgspx.mapper.UserMapper.getUser(int)
    }
    return mapperMethod.execute(this.sqlSession, args);
}

execute真实操作

判断类型并执行具体的sql语句
if (SqlCommandType.INSERT == this.command.getType()) {
 
}else if (SqlCommandType.SELECT == this.command.getType()) {
    if (this.method.returnsVoid() && this.method.hasResultHandler()) {
        this.executeWithResultHandler(sqlSession, args);
        result = null;
    } else if (this.method.returnsMany()) {
        result = this.executeForMany(sqlSession, args);
    } else if (this.method.returnsMap()) {
        result = this.executeForMap(sqlSession, args);
    } else {
        param = this.method.convertArgsToSqlCommandParam(args);
        result = sqlSession.selectOne(this.command.getName(), param);
           name:com.jarye.mapper.UserMapper.getUser
    }

推荐阅读:
<<<Mybatis的整体执行原理图解
<<<SqlSessionFactory的创建过程原理
<<<SqlSession的创建过程
<<<sqlSession如何获得具体的Mapper接口信息
<<<sqlSession.selectOne底层实现原理
<<<Mybatis一级缓存知识汇总
<<<Mybatis二级缓存知识汇总
<<<Springboot整合Mybatis二级缓存
<<<Mybatis常见面试题

上一篇 下一篇

猜你喜欢

热点阅读