第一节中,分析了Mybatis的ORM框架的初始化,这篇来分析SQL执行过程中,对象->SQL是如何转换的

  其中包含两种映射思想

  ①DAO接口->Mapper实例

  ②执行DAO的方法时,参数->SQL的转换

  • DAO接口如何转变成具体可执行SQL的Mapper

   我们在使用mybatis的时候,Mapper会设置命名空间。

<mapper namespace="org.mybatis.example.mapper.MybatistestMapper">

 Java对象的DAO接口声明如下:

 package org.mybatis.example.mapper;

 import java.util.List;
import org.mybatis.example.model.Mybatistest; public interface MybatistestMapper {

  从包结构以及命名分析,Dao接口与Mapper有关系(XML 以及Mapper对象)

  在不使用Spring的时候,mybatis通过sqlSession.getMapper(MybatistestMapper.class) 来获得DAO-Mapper代理代理类。

  查看SqlSession的默认实现类DefaultSqlSession.getMapper(),通过configuration得到Mapper

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

  查看Configuration.getMapper

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

  继续查看mapperRegistry.getMapper。可以得到以下信息

  ①创建Mapper的工厂被缓存在knownMappers

   如果工厂不存在,则报错。查看代码,工厂是在XMLMapperBuilder解析Mapper时候添加进去的。

   根据Mapper.xml中的namespace,缓存Factory。有个好处。对获取的DaoMapper需要在配置文件中配置过。限定了使用范围。也避免后期执行时候出错。

   代码可自行查找源码

  ②当缓存中不存在时,则创建动态代理MapperProxyFactory.newInstance

  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

  @SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
} public T newInstance(SqlSession sqlSession) {
  //代理类中实际执行操作的代码
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}

  通过以上步骤,我们找到了Dao接口的实现(Mapper 实例接口)

  • 当调用一个DAO接口中的方法时,如何找到SQL以及转化为SQL的?.

  通过上面的代码,可以得知实际执行SQL的是mapperProxy 代理类

  看下mapperProxy 的invoke方法

  @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);   //通过MapperMethod执行
return mapperMethod.execute(sqlSession, args);
}

查看MapperMethod的execute方法

public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
switch (command.getType()) {
case INSERT: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
case UPDATE: {
Object param = method.convertArgsToSqlCommandParam(args);
     //实际找到SqlSession的update方法。command.getName()是Mapper的Id
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}

  到此为止,我们只是找到了要执行的SQL。由SqlSession来执行。

  

  

  

     

mybatis关于ORM的使用以及设计(二)[DaoInterface 转换 Mapper代理对象]的更多相关文章

  1. springboot集成下,mybatis的mapper代理对象究竟是如何生成的

    前言 开心一刻 中韩两学生辩论. 中:端午节是属于谁的? 韩:韩国人! 中:汉字是谁发明的? 韩:韩国人! 中:中医是属于谁的? 韩:韩国人! 中:那中国人到底发明过什么? 韩:韩国人! 前情回顾 M ...

  2. Mybatis源码解析 - mapper代理对象的生成,你有想过吗

    前言 开心一刻 本人幼教老师,冬天戴帽子进教室,被小朋友看到,这时候,有个小家伙对我说:老师你的帽子太丑,赶紧摘了吧.我逗他:那你好好学习,以后给老师买个漂亮的?这孩子想都没想立刻回答:等我赚钱了,带 ...

  3. mybatis关于ORM的使用以及设计(三)[参数对象转换为SQL语言]

    上节分析了Mapper对象的创建. 在ORM的定义中可以理解为Object->SQLMapper抽象层(这一层并不负责具体的SQL执行.这一层可以理解为SQL代理层) 本节分析以下内容: ①Sq ...

  4. mybatis关于ORM的使用以及设计(一)[ORM的初始化]

    ORM WIKI中的解释.画重点 Object-relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is ...

  5. mybatis入门基础(二)----原始dao的开发和mapper代理开发

    承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...

  6. Mybatis学习总结(二)——Mapper代理开发

    一.概要 1.原始DAO开发中存在的问题:(1)DAO实现方法体中存在很多过程性代码.(2)调用SqlSession的方法(select/insert/update)需要指定Statement的id, ...

  7. Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...

  8. Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合

    Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...

  9. 【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder ...

随机推荐

  1. 解决:安装SQL Server 2008 Native Client遇到错误(在Navicat premium新建sqlserver连接时 需要):An error occurred during ...HRESULT: 0x80070422(注意尾部的错误号)

    解决:安装SQL Server 2008 Native Client遇到错误(在Navicat premium新建sqlserver连接时 需要):An error occurred during . ...

  2. C#中的Cookie

    cookie属性: name字段为一个cookie的名称. value字段为一个cookie的值. domain字段为可以访问此cookie的域名. path字段为可以访问此cookie的页面路径. ...

  3. 《Java编程思想》读书笔记-第一个Java程序

    积少成多,欢迎大家关注我的微信公众号,共同探讨Java相关技术 今天的重点是:编写源代码.编译.运行.查看成果.除此之外,还需要搭建起与前面学习的知识的联系. 写本文时依据的系统环境是Windows1 ...

  4. javascript最全最好的判断数组的方法

    var arr = [1,2,3,1]; var arr2 = [{ abac : 1, abc : 2 }]; function isArrayFn(value){ if (typeof Array ...

  5. UT源码105032014093

    需求描述: 设计佣金问题的程序 commission方法是用来计算销售佣金的需求,手机配件的销售商,手机配件有耳机(headphone).手机壳(Mobile phone shell).手机贴膜(Ce ...

  6. js cookie 操作 封装

    pCookie.js (function(){ var PotatogCookie = {}; //设置cookie PotatogCookie.set = function(key, value, ...

  7. 用turtle实现动态汉诺塔

    代码如下: (此代码最多可支持七层) import turtle class Stack: def __init__(self): self.items = [] def isEmpty(self): ...

  8. [Java] 基本資料包裝類別 Wrapper Classes

    基本型別包裝 (Wrapper Classes) 將基本型別生成物件,要將基本型別先包裝成物件,才能執行生成, Boxing: Integer a = new Integer(1) Unboxing: ...

  9. iTrash for Mac(卸载工具)破解版含注册机

    iTrash for Mac是一款专为Mac用户打造非常好用的卸载工具,itrash mac版简单好用,只需要把需要卸载的程序的拖拽到iTrash Mac版窗口内就可以删除应用程序.现为大家带来itr ...

  10. C++ 基于凸包的Delaunay三角网生成算法

    Delaunay三角网,写了用半天,调试BUG用了2天……醉了. 基本思路比较简单,但效率并不是很快. 1. 先生成一个凸包: 2. 只考虑凸包上的点,将凸包环切,生成一个三角网,暂时不考虑Delau ...