上文mybatis源码简书我们讲到sqlsession中通过executor来执行sql,我们接着往下看

update方法点进去,我们进到baseexecutor

这里我们看到 clearLocalCache 方法,可见每次更新都会清除缓存

我们再看到doUpdate

 public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
stmt = prepareStatement(handler, ms.getStatementLog());
return handler.update(stmt);
} finally {
closeStatement(stmt);
}
}

先看第5行,生成一个 StatementHandler,StatementHandler是mybatis4大对象之一,负责处理Mybatis与JDBC之间Statement的交互。

我们再看下newStatementHandler,

这里又看 RoutingStatementHandler,先不管,再点进去,会发现会有不同类型的StatementHandler

SimpleStatementHandler,这个很简单了,就是对应我们JDBC中常用的Statement接口,用于简单SQL的处理;
PreparedStatementHandler,这个对应JDBC中的PreparedStatement,预编译SQL的接口;
CallableStatementHandler,这个对应JDBC中CallableStatement,用于执行存储过程相关的接口;
RoutingStatementHandler,这个接口是以上三个接口的路由,没有实际操作,只是负责上面三个StatementHandler的创建及调用。

这几个StatementHandler他有共同的构造器,都在BaseStatementHandler

 protected BaseStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
this.configuration = mappedStatement.getConfiguration();
this.executor = executor;
this.mappedStatement = mappedStatement;
this.rowBounds = rowBounds; this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
this.objectFactory = configuration.getObjectFactory(); if (boundSql == null) { // issue #435, get the key before calculating the statement
generateKeys(parameterObject);
boundSql = mappedStatement.getBoundSql(parameterObject);
} this.boundSql = boundSql; this.parameterHandler = configuration.newParameterHandler(mappedStatement, parameterObject, boundSql);
this.resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement, rowBounds, parameterHandler, resultHandler, boundSql);
}

这里顺便创建了parameterHandler和 resultSetHandler

parameterHandler的作用就是进行参数预编译设置,resultSetHandler封装我们得到的结果。

public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
2 Statement stmt = null;
3 try {
4 Configuration configuration = ms.getConfiguration();
5 StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
6 stmt = prepareStatement(handler, ms.getStatementLog());
7 return handler.update(stmt);
8 } finally {
9 closeStatement(stmt);
10 }
11 }

再看第六行,我们在生成StatementHandler,开始执行prepareStatement

 private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
Statement stmt;
Connection connection = getConnection(statementLog);
//调用StatementHandler.prepare
stmt = handler.prepare(connection);
//调用StatementHandler.parameterize
handler.parameterize(stmt);
return stmt;
}

我们看到prepare方法,生成statement,用过jdbc的都知道,Statement对象主要用于将 SQL 语句发送到数据库中,执行对数据库的数据的检索或者更新

生成好statement后,我们再看下第7行

public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
2 Statement stmt = null;
3 try {
4 Configuration configuration = ms.getConfiguration();
5 StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
6 stmt = prepareStatement(handler, ms.getStatementLog());
7 return handler.update(stmt);
8 } finally {
9 closeStatement(stmt);
10 }
11 }

这里就是真正的执行sql的地方。

Mybatis Insert、update、delete流程的更多相关文章

  1. mybatis insert update delete返回都是整型 0,1,增,删,改要提交事物

    mybatis insert update delete返回都是整型 0,1, 没有扔 增,删,改要提交事物

  2. 关于MyBatis mapper的insert, update, delete返回值

    这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...

  3. mybatis select/insert/update/delete

    这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...

  4. PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)

    原文: PHP5: mysqli 插入, 查询, 更新和删除  Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...

  5. mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干

    1.mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干 2.一般来说,事务是必须满足4个条件(ACID): Atomicity(原子性).Con ...

  6. [Hive - LanguageManual] DML: Load, Insert, Update, Delete

    LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...

  7. insert update delete 语法 以及用法

    insert update delete 被称为 数据定义语句语句 也就是数据的增加 修改 删除 其中不包括查询 譬如: create database -创建数据库 alter database - ...

  8. sql中同一个Trigger里同时包含Insert,Update,Delete

    sql中同一个Trigger里同时包含Insert,Update,Delete SQLServer是靠Inserted表和Deleted表来处理的,判断一下就可以了,只不过比ORACLE麻烦一点 cr ...

  9. mysql数据恢复 insert\update\delete 工具MyFlash

    一.简介MyFlash是由美团点评公司技术工程部开发维护的一个回滚DML操作的工具.该工具通过解析v4版本的binlog,完成回滚操作.相对已有的回滚工具,其增加了更多的过滤选项,让回滚更加容易. 该 ...

  10. LINQ体验(9)——LINQ to SQL语句之Insert/Update/Delete操作

    我们继续讲解LINQ to SQL语句,这篇我们来讨论Insert/Update/Delete操作.这个在我们的程序中最为常用了.我们直接看例子. Insert/Update/Delete操作 插入( ...

随机推荐

  1. JAVA微信公众号通过openid发送模板消息~

    1,问题产生 在微信公众号开发过程中,我们有时候做不同权限的时候,比如在注册的时候,需要审核,然后我们要想办法让对方知道审核的结果.这时候我们可以通过模板消息来通知. 2,第一步,首先在微信公众号上获 ...

  2. (PMP)解题技巧和典型题目分析(模拟二)

  3. 解决ORA-30036:无法按8扩展段(在还原表空间‘XXXX’中)

    在update一数据量很大的表时,提示“ORA-30036:无法按8扩展段” 度娘了下原因与解决办法:   1.查询了一下undo表空间的使用,发现已经超过了80% SELECT a.tablespa ...

  4. 编译Spark源码

    Spark编译有两种处理方式,第一种是通过SBT,第二种是通过Maven.作过Java工作的一般对于Maven工具会比较熟悉,这边也是选用Maven的方式来处理Spark源码编译工作. 在开始编译工作 ...

  5. Notes : <Hands-on ML with Sklearn & TF> Chapter 5

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  6. python之路(八)-迭代器&生成器

    迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优点是 ...

  7. 小白Monkey学习笔记

    Monkey是google提供的一款对Android app进行压力测试工具,基于随机坐标位置,进行点击.滑动.输入等操作. Monkey的环境配置 pc电脑需要配置adb环境 Monkey程序由An ...

  8. 手把手教你利用Python自动下载CL社区图片

    需求描述:     最近发现CL社区上好多精华的帖子分享的图片非常棒,好想好想保存下来,但是一张一张地保存太费时间了,因此,造物者思想主义的我就想动手写个工具,实现只要输入帖子的链接,就能把所有的精华 ...

  9. ubuntu系统用docker搭建wordpress

    目标:在docker中搭建wordpress 安装顺序: 首先要有一个云服务器---购买或者自己搭建(本人是自己在主机上装了虚拟机,搭建了一个ubuntu14.04,安装链接:https://www. ...

  10. HARD FAULT

    程序陷在while(1)里面 解决办法 定点到发生死循环的位置 打开stack windows逐层查找发生死循环之前运行过的函数 导致原因 1 内存溢出或者访问越界,通常为数组或结构体访问越界.这个需 ...