RoutingStatementHandler类似路由器,在其构造函数中会根据Mapper文件中设置的StatementType来选择使用SimpleStatementHandler.PreparedStatementHandler和CallableStatementHandler,其实现的接口StatementHandler的方法也是由这三个具体实现类来实现. 源码如下: /** * @author Clinton Begin */ //类似于路由器 public class RoutingS…
StatementHandler通过类名我们可以了解到它可能是Statement的处理器,它是一个接口,其实现类如下: BaseStatementHandler:一个抽象类,只是实现了一些不涉及具体操作的方法 RoutingStatementHandler:类似路由器,根据配置文件来路由选择具体实现类SimpleStatementHandler.CallableStatementHandler和PreparedStatementHandler SimpleStatementHandler:就是直…
CallableStatementHandler实际就是使用CallableStatement来执行SQL语句,当然它执行的是存储过程. 源码如下: /** * @author Clinton Begin */ /***/ public class CallableStatementHandler extends BaseStatementHandler { public CallableStatementHandler(Executor executor, MappedStatement ma…
PreparedStatementHandler就是调用PreparedStatement来执行SQL语句,这样在第一次执行sql语句时会进行预编译,在接下来执行相同的SQL语句时会提高数据库性能 源码如下: /** * @author Clinton Begin */ /* 使用PrepareStatement**/ public class PreparedStatementHandler extends BaseStatementHandler { public PreparedState…
SimpleStatementHandler就是使用基本的Statement来执行query.batch.update等操作,其实现还是比较简单的,当然在执行过程中会涉及keyGenerator和ResultHandler操作,这些我们会在接下来的博客中进行详解.SimpleStatementHandler用于执行简单的sql语句,这里简单的sql语句是指sql语句中没有变量,不会通过外部进行参数传入的sql语句. 源码如下: /** * @author Clinton Begin */ pub…
BaseStatementHandler是一个抽象类,并没有实现和CURD相关的类,只是更多的设置了一些参数相关. 源码如下: /** * @author Clinton Begin */ public abstract class BaseStatementHandler implements StatementHandler { protected final Configuration configuration; protected final ObjectFactory objectF…
官网上面讲到:无论是 MyBatis 在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值时, 都会用类型处理器将获取的值以合适的方式转换成 Java 类型.那么为什么会有类型处理器呢?这一点并不难理解,SQL语句事实上可以理解为一门面向数据库的编程语言.所以相对而言都有自己的数据类型.这也就意味着存在数据类型不一至的问题.同时不同的数据库之间数据类型还有一定的差义.类型处理器则就是用于处理数据类型不一至的问题. 笔者看过几个不同的ORM框架都存在着类型处…
说明 读了3遍:https://my.oschina.net/zudajun/blog/665956 现在统一整理成笔记 并跟着源码一行一行调试 统一整理起来 SqlSession 接口定义 public interface SqlSession extends Closeable { <T> T selectOne(String var1); <T> T selectOne(String var1, Object var2); <E> List<E> se…
这一篇博客我们学习一下Mybatis调用存储过程的使用和运行流程.首先我们先创建一个简单的存储过程 DELIMITER $ CREATE PROCEDURE mybatis.ges_user_count(IN age INT, OUT user_count INT) BEGIN SELECT COUNT(*) FROM users WHERE users.age=age INTO user_count; END $ 这个存储过程的含义其实比较简单的,就是输入age,然后执行select coun…
该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub 地址.Spring-Boot-Starter 源码分析 GitHub 地址)进行阅读 MyBatis 版本:3.5.2 MyBatis-Spring 版本:2.0.3 MyBatis-Spring-Boot-Starter 版本:2.1.4 MyBatis的初始化 在MyBatis初始化过程中,大…