Handwritten Mybatis, thoroughly get to know the principles of the framework

mybatis, formerly known as iBatis, which is derived from "Internet" and "abatis" The combination is an excellent persistence framework that supports custom SQL, stored procedures and advanced mappings. mybatis avoids almost all JDBC code and manual setting parameters and obtaining the result set, it can use a simple XML or annotations native configuration and mapping information, the mapping interface and Java POJO to records in the database.
Handwritten Mybatis, thoroughly get to know the principles of the framework

Mybatis architectural design

Handwritten Mybatis, thoroughly get to know the principles of the framework
Mybatis functional architecture is divided into three layers:

  • Interface Layer: provides the interface to the API, developers external databases used to manipulate these local API. The interface layer receives the call request will call the data processing layer to accomplish their data processing.
  • Data processing layer: responsible for specific SQL lookup, SQL parsing, SQL execution and implementation of the results of the mapping process and so on. Its main purpose is to complete the operation at the request of a database call.
  • Frame support layer: is responsible for supporting the most basic functions, including connection management, transaction management, caching configuration and loading, these things are common, they are extracted out as the most basic components. Most basic support for the upper layer data processing.
  • mybatis的运行流程

    Handwritten Mybatis, thoroughly get to know the principles of the framework
    1.加载配置:配置来源于两个地方,配置文件和注解。将SQL的配置信息加载成为MappedStatement对象(包括了传入参数映射配置、执行的SQL语句、结果映射配置),存储在内存中。
    2.SQL解析:当API接口层接收到调用请求时,会接收到传入SQL的ID和传入对象(可以是Map、JavaBean或者基本数据类型),Mybatis会根据SQL的ID找到对应的MappedStatement,然后根据传入参数对象对MappedStatement进行解析,解析后可以得到最终要执行的SQL语句和参数。
    3.SQL执行:将最终得到的SQL和参数拿到数据库进行执行,得到操作数据库的结果。
    4.结果映射:将操作数据库的结果按照映射的配置进行转换,可以转换成HashMap、JavaBean或者基本数据类型,并将最终结果返回。
    手写mybatis的目录
    来看下笔者的手写mybatis的文件目录。查看Bootstrap文件上说明,然后运行该文件,断点调试即可一步步了解mybatis框架的原理了。(很多地方都注解了,方便初学者更多了解框架细节)

代码地址请查看,https://github.com/y277an/java-you-need-know。再看看正版的代码结构吧
Handwritten Mybatis, thoroughly get to know the principles of the framework

考虑到transaction对理解框架原理产生干扰,所以代码全部删掉

mybatis目录结构及内容
以下目录对照的是mybatis-3.5.1的源码。

  • annotations。注解目录。包括所有的注解,如@SELECT,@UPDATE等。
  • binding。Mapper类的实例反射生成工具目录。
  • builder。主要是注解,mapper和SqlSource的构造器及转换器。
  • cache。Mybatis内部缓存接口。实现了一些特定的缓存策略,FifoCache、LruCache、BlockingCache、LoggingCache等。
  • dataSource。数据源工厂类及实现。实现类包括JndiDataSourceFactory、PooledDataSourceFactory和UnpooledDataSourceFactory。数据源实现类:UnpooledDataSource和PooledDataSource。
  • exceptions。Mybatis自定义的异常类,都继承自RuntimeException。
  • executor。执行器相关包。包括Key生成器、加载器(包括Cglib、Javassist的代理,结果加载器)、参数处理器接口、结果处理器、结果集(resultSet)处理器、Statement处理器(实现类:BaseStatementHandler、CallableStatementHandler、PreparedStatementHandler、RoutingStatementHandler、SimpleStatementHandler)、执行器(SimpleExecutor、ReuseExecutor、CachingExecutor、BatchExecutor、BaseExecutor)。
  • io。主要是定义的几个VFS(VFS、DefaultVFS、ClassLoaderWrapper)。
  • jdbc。与Sql相关的操作。如Sql运行器,脚本运行器和Sql封装类等。
  • logging。各个类型的日志适配器,都实现了Log接口。StdOutImpl、Slf4jImpl、NoLoggingImpl、Log4j2Impl、Log4jImpl、Jdk14LoggingImpl、BaseJdbcLogger和JakartaCommonsLoggingImpl。
  • mapping。主要是接口参数,sql和返回结果的映射类,主要类包括:MappedStatement、ParameterMap、ParameterMapping、ResultMap、ResultMapping、BoundSql和SqlSource等类。
  • parsing。变量解析,如解析${},#{}等。
  • plugin。主要包含插件的定义接口。如Interceptor、Plugin和InterceptorChain等。
  • reflection。主要是一些反射操作的工具方法和对象工厂类,以及一些常用的包装类,如BaseWrapper、BeanWrapper、CollectionWrapper、MapWrapper和ObjectWrapper。
  • scripting。执行驱动和动态Sql解析的处理器。
  • session。主要是SqlSession和SqlSessionFactory。
  • transaction。主要是mybatis简单封装的jdbc事务操作类。
  • type。各个类型数据的处理器。用于动态的设置参数和转换数据,如IntegerTypeHandler用来处理Integer类型的值的set和get操作。除了八大基本类型,还有常用的集合及Map类型,还增加了各种时间类型的处理器。
    MyBatis的核心成员和职责
    从MyBatis代码实现的角度来看,MyBatis的核心组件如下
  • SqlSession。作为MyBatis工作的主要顶层API,表示和数据库交互的会话,完成必要数据库增删改查功能。
  • Executor。MyBatis执行器,是MyBatis调度的核心,负责SQL语句的生成和查询缓存的维护。
  • StatementHandler。封装了JDBC Statement操作,负责对JDBCstatement的操作,如设置参数、将Statement结果集转换成List集合。
  • ParameterHandler. Responsible for the user to pass parameters into the parameters required JDBC Statement.
  • ResultSetHandler. Responsible for converting JDBC ResultSet returned result set as a collection of objects of type List.
  • TypeHandler responsible for mapping and converting between data types and data type jdbc java.
  • MappedStatement. MappedStatement maintain a <select | insert update | | delete> package node.
  • SqlSource. The user is responsible for delivery parameterObject dynamically generate SQL statements, the encapsulation information to BoundSql object, and returns.
  • BoundSql. It represents a dynamically generated SQL statements and the corresponding parameter information.
  • Configuration. All MyBatis configuration information is maintained in the Configuration object.

Guess you like

Origin blog.51cto.com/14541438/2438586