Main 方法,mybatis 版本为 3.5.0

使用 MapperProxyFactory 创建一个 MapperProxy 的代理对象

代理对象里面包含了 DefaultSqlSession(Executor)

InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
DeptMapper mapper = session.getMapper(DeptMapper.class);

session.getMapper(DeptMapper.class)

org.apache.ibatis.session.defaults.DefaultSqlSession

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

configuration.<T>getMapper(type, this)

org.apache.ibatis.session.Configuration

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

mapperRegistry.getMapper(type, sqlSession)

org.apache.ibatis.binding.MapperRegistry

// 返回代理类
@SuppressWarnings("unchecked")
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);
}
}

mapperProxyFactory.newInstance(sqlSession)

org.apache.ibatis.binding.MapperProxyFactory

@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
// 用JDK自带的动态代理生成映射器
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
} public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}

new MapperProxy<>(sqlSession, mapperInterface, methodCache)

org.apache.ibatis.binding.MapperProxy

/**
* 映射器代理
*/
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 {
// 代理以后,所有 Mapper 的方法调用时,都会调用这个 invoke 方法
// 并不是任何一个方法都需要执行调用代理对象进行执行,如果这个方法是 Object 中通用的方法(toString、hashCode等)则无需执行
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);
}
// 去缓存中找 MapperMethod
final MapperMethod mapperMethod = cachedMapperMethod(method);
// 执行
return mapperMethod.execute(sqlSession, args);
} // 去缓存中找 MapperMethod
private MapperMethod cachedMapperMethod(Method method) {
return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
} private Object invokeDefaultMethod(Object proxy, Method method, Object[] args) throws Throwable {
final Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
final Class<?> declaringClass = method.getDeclaringClass();
return constructor
.newInstance(declaringClass, MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC)
.unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args);
} /**
* Backport of java.lang.reflect.Method#isDefault()
*/
private boolean isDefaultMethod(Method method) {
return (method.getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) == Modifier.PUBLIC && method.getDeclaringClass().isInterface();
}
}

时序图


https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis

MyBatis-获取 xxxMapper的更多相关文章

  1. Mybatis获取自动增长Id

    Mybatis获取自动增长Id MyBatis成功插入后获取自动增长的id 1.向xxMapping.xml配置中加上两个配置. <insert id="insertUser" ...

  2. MyBatis 获取插入记录的 id

    现在的项目改用 Guns 了,也是一个很不错的框架,用起来也感觉很不错,上手也挺方便的.毕竟对于只是应用层面的知识,也基本上就是看看手册,熟悉熟悉就可以轻松上手了.如果是想要深入,或者刨根问底,那么就 ...

  3. Mybatis获取数据库自增主键

    一般我们都为将表中主键列设置为自增,当我们执行插入语句时,比如这样 //测试添加 Employee employee = new Employee(null, "jerry4",n ...

  4. Mybatis获取自增主键的值

    pojo: public class User { private Integer id; private String name; private String pwd; setter和getter ...

  5. MyBatis获取参数值的两种方式

    MyBatis获取参数值的两种方式:${}和#{} ${}的本质就是字符串拼接,#{}的本质就是占位符赋值 ${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单 ...

  6. MyBatis获取插入记录的自增长字段值

    在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名! <insert id=" ...

  7. Mybatis获取插入记录的自增长ID(转)

    1.在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名. <ins ...

  8. mybatis 获取自增ID

    在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名. <inser ...

  9. myBatis获取批量插入数据的主键id

    在myBatis中获取刚刚插入的数据的主键id是比较容易的 , 一般来说下面的一句话就可以搞定了 , 网上也有很多相关资料去查. @Options(useGeneratedKeys = true, k ...

  10. MyBatis 入门到精通(一) 了解MyBatis获取SqlSession

    MyBatis是什么? MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis ...

随机推荐

  1. 洛谷P2512 糖果传递

    环形均分纸牌 普通的均分纸牌前缀和的总和就是答案. 但是这里是环形的,要断开的位置需要最佳,我们把每个数减去sum/n,这样总的前缀和就为0了,若在第k个数之后把环断开,环形前缀和可以统一写成s[i] ...

  2. 白兔的刁难 IDFT

    题目描述 给你\(n,k\),求 \[ \forall 0\leq t< k,s_t=\sum_{i=-t}^{n-t}[k|i]\binom{n}{i+t} \] 对\(998244353\) ...

  3. 写个shell脚本依次运行每个程序半小时

    @echo off :: 运行时间1800000毫秒 echo wscript.sleep 1800000 >run.vbs ::运行Debug/lab1.exe程序,后面是参数 start D ...

  4. Markdown 使用技巧

    懒得复制,直接贴网页吧 懒得复制,直接贴网页吧*2 懒得复制,直接贴网页吧*3

  5. 聊聊openjdk的BufferPoolMXBean

    本文主要研究一下openjdk的BufferPoolMXBean PlatformManagedObjectjava.management/java/lang/management/PlatformM ...

  6. [luogu1198][bzoj1012][JSOI2008]最大数【线段树+分块】

    题目描述 区间查询最大值,结尾插入,强制在线. 分析 线段树可以做,但是练了一下分块,发现自己打错了两个地方,一个是分块的地方把/打成了%,还有是分块的时候标号要-1. 其他也没什么要多讲的. 代码 ...

  7. html概述和基本结构

    html概述 HTML是 HyperText Mark-up Language 的首字母简写,意思是超文本标记语言,超文本指的是超链接,标记指的是标签,是一种用来制作网页的语言,这种语言由一个个的标签 ...

  8. [SCOI2007]压缩(区间dp)

    神仙题,看了半天题解才看明白... 因为题目里说如果没有m,会自动默认m在最前面. 我们设计状态为dp[l][r][0/1]为在区间l到r中有没有m的最小长度. 转移:枚举我们要压缩的起点,dp[l] ...

  9. CF1106E Lunar New Year and Red Envelopes

    比赛时看到这题懵逼了,比完赛仔细一想是个很简单的dp = = 由于题目限制,可以发现\(B\)取红包的策略是唯一的,可以用优先队列预处理出\(B\)在第\(i\)秒可以拿到的红包的收益\(w_i\)和 ...

  10. wildfly tomcat 服务器不响应 不返回 死住了 查看tcp CLOSE_WAIT 暴多

    I'm also having the same issue with a very latest Tomcat server (7.0.40). It goes non-responsive onc ...