Mybatis Insert、update、delete流程
上文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流程的更多相关文章
- mybatis insert update delete返回都是整型 0,1,增,删,改要提交事物
mybatis insert update delete返回都是整型 0,1, 没有扔 增,删,改要提交事物
- 关于MyBatis mapper的insert, update, delete返回值
这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...
- mybatis select/insert/update/delete
这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...
- PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)
原文: PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...
- mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干
1.mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干 2.一般来说,事务是必须满足4个条件(ACID): Atomicity(原子性).Con ...
- [Hive - LanguageManual] DML: Load, Insert, Update, Delete
LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...
- insert update delete 语法 以及用法
insert update delete 被称为 数据定义语句语句 也就是数据的增加 修改 删除 其中不包括查询 譬如: create database -创建数据库 alter database - ...
- sql中同一个Trigger里同时包含Insert,Update,Delete
sql中同一个Trigger里同时包含Insert,Update,Delete SQLServer是靠Inserted表和Deleted表来处理的,判断一下就可以了,只不过比ORACLE麻烦一点 cr ...
- mysql数据恢复 insert\update\delete 工具MyFlash
一.简介MyFlash是由美团点评公司技术工程部开发维护的一个回滚DML操作的工具.该工具通过解析v4版本的binlog,完成回滚操作.相对已有的回滚工具,其增加了更多的过滤选项,让回滚更加容易. 该 ...
- LINQ体验(9)——LINQ to SQL语句之Insert/Update/Delete操作
我们继续讲解LINQ to SQL语句,这篇我们来讨论Insert/Update/Delete操作.这个在我们的程序中最为常用了.我们直接看例子. Insert/Update/Delete操作 插入( ...
随机推荐
- 每日一练ACM 2019.0417
Problem Description 给定两个正整数,计算这两个数的最小公倍数. Input 输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数. Output 对于每个测试 ...
- sqlserver存储过程分页记录
USE [HK_ERP]GO/****** Object: StoredProcedure [dbo].[GetPageBillsByShopID] Script Date: 2018/10/30 1 ...
- openmp入门总结
Ref: https://wdxtub.com/2016/03/20/openmp-guide/ 简介 这门课作为 ECE 中少有的跟计算机科学相关的课,自然是必上不可.不过无论是 OpenMP 还是 ...
- 分享几个写 demo 的思路
好久没有动笔,最近发现了一个新的写 demo 的思路,非常有意思.仔细一想,自己仿佛积累了不少写 demo 的思路和想法,总结一下,抛砖引玉. 本文所说 demo 主要分以下三种: 本地 demo 外 ...
- pip3 install的时候报错timed out
问题: 执行pip install requests报错 Read timed out. 解决方法: 修改超时时间: pip --default-timeout=1000 install -U r ...
- Appium之Android功能脚本
Android功能脚本 注:这里只写了登录和退出功能,以下不提供app的包名,下面我使用的是线上包 准备:1.Eclipse的Java环境:2.Appium环境:3.Android真机一台. 创建一个 ...
- Python3.* 和Python2.*的区别
许多Python初学者都会问:我应该学习哪个版本的Python.对于这个问题,我的回答通常是“先选择一个最适合你的Python教程,教程中使用哪个版本的Python,你就用那个版本.等学得差不多了,再 ...
- [转] 分代垃圾回收的 新旧代引用问题(原标题:Back To Basics: Generational Garbage Collection)
原文链接: https://blogs.msdn.microsoft.com/abhinaba/2009/03/02/back-to-basics-generational-garbage-colle ...
- java实现wc.exe
Github地址:https://github.com/ztz1998/wc/tree/master 项目相关要求 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功 ...
- 在ASP.NET Core MVC中构建简单 Web Api
Getting Started 在 ASP.NET Core MVC 框架中,ASP.NET 团队为我们提供了一整套的用于构建一个 Web 中的各种部分所需的套件,那么有些时候我们只需要做一个简单的 ...