Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

补充类图、时序图等 #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Mybatis主要类及之间的关系.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 127 additions & 0 deletions Mybatis主要类及之间的关系.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
@startuml

abstract class BaseBuilder{
Configuration configuration;
TypeAliasRegistry typeAliasRegistry;
TypeHandlerRegistry typeHandlerRegistry;
}
note left of BaseBuilder:所有构造者的基类

class SqlSessionFactoryBuilder{
SqlSessionFactory build()
}
note right of SqlSessionFactoryBuilder:产生工厂,工厂需要一个\nConfiguration类通过\nXMLConfigBuilder获取。
SqlSessionFactoryBuilder "build()"..> "parse()"XMLConfigBuilder

class XMLConfigBuilder{
Configuration configuration
Configuration parse()
}
note right of XMLConfigBuilder:parse方法会解析xml文件\n的所有标签放到configuration\n对象中,在解析mappers时会根据\nurl或者resource获取对应的mapper\n文件然后调用XMLMapperBuilder类\n来解析。
XMLConfigBuilder "parse()"..> "parse()"XMLMapperBuilder
BaseBuilder <|-- XMLConfigBuilder

class XMLMapperBuilder{
void parse()
}
BaseBuilder <|-- XMLMapperBuilder
XMLMapperBuilder --> Configuration


class Configuration{
MapperRegistry mapperRegistry
}
Configuration --> MapperRegistry

note left of Configuration: Mybatis最重要的类,存放所有配置相关的信息\n,解析的xml文件的所有内容都存到这里,大部\n分内容在这个类里都是一个map的结构,key是\nmapper的命名空间+标签id,值是标签对应的对象。

class MapperRegistry{
Configuration configuration
}
MapperRegistry --> Configuration
note left of MapperRegistry: XMLMapperBuilder类通过Configuration\n类将mapper映射文件对应的接口类型作为\nvalue存到该类的map中,key是命名空间。

interface SqlSession{
}

class DefaultSqlSession{
Configuration configuration;
Executor executor;
}
SqlSession <|-- DefaultSqlSession
DefaultSqlSession-->Configuration
DefaultSqlSession-->Executor
note right of DefaultSqlSession: 各种sql语句都是通过session中的Executor\n执行的,而Executor则是通过Transaction对\n象获取数据库连接,然后executor创建\nstatementhandler,由statementhandler\n创建管理statement与数据库交互。

class JdbcTransaction{
Connection connection;
DataSource dataSource;
TransactionIsolationLevel level;
boolean autoCommmit;
}

class SimpleExcutor{
Transaction transaction;
Executor wrapper;
}
SimpleExcutor-> JdbcTransaction
Executor<|--SimpleExcutor
SimpleExcutor..>RoutingStatementHandler

interface Executor{
}
class SqlSessionManager implements SqlSessionFactory,SqlSession{
}
note bottom of SqlSessionManager: SqlSessionFactory优化版\n,复用session,同一线程\n每次会获取同一session,\n通过threadlocal实现。

interface ResultHandler{
handleResult()
}
note right of ResultHandler: 对从库中查询的结果\n进行处理,功能与插件\n一样,但是效率会更高\n一些,用户自己写接口\n实现。

interface TypeHandler{

}
note left of TypeHandler: JDBC的PreparedStatement类先是用占位符\n给sql,然后再调用.setString|setArrayList\n等方法。typeHandler有很多实现类,都是\n对应的某种类型处理器,作用是在set时根据\n要set的类型,调用不同的类型处理器,第二个\n作用是在取得查询结果,将结果映射为对应的\njava类型。
class DefaultParameterHandler{

}
note left of DefaultParameterHandler: 在产生statement后,通过\nDefaultParameterHandler\n来给之前sql语句中的占位符\n进行替换,具体实现是在内部\n根据参数类型产生对应的\ntypeHandler,在对应的handler\n进行替换。
DefaultParameterHandler--> TypeHandler

class BaseStatementHandler{
Configuration configuration;
ObjectFactory objectFactory;
TypeHandlerRegistry typeHandlerRegistry;
ResultSetHandler resultSetHandler;
ParameterHandler parameterHandler;
Executor executor;
MappedStatement mappedStatement;
RowBounds rowBounds;
BoundSql boundSql;
}
note right of BaseStatementHandler:最最重要的接口之一,executor执行的查询和更新都是通过它的子类实现的

class RoutingStatementHandler{
private final StatementHandler delegate;
}
RoutingStatementHandler->BaseStatementHandler
note left of RoutingStatementHandler:这个接口是BaseStatementHandler的三个子类的路由,\n没有实际操作,只是负责三个StatementHandler的创\n建及调用。

class ErrorContext{
private ErrorContext stored;
//存储异常存在于哪个资源文件中
private String resource;
//存储异常是做什么操作时发生的
private String activity;
//存储哪个对象操作时发生异常。
private String object;
//存储异常的概览信息。
private String message;
//存储发生异常的 SQL 语句。
private String sql;
//存储详细的 Java 异常日志。
private Throwable cause;
}
note top of ErrorContext:在线程执行过程中不断将执行环境信息\n存到该类中,当发生异常时mybatis会\n将该对象的toString()方法传给统一的\n持久层异常进行打印。
@enduml
14 changes: 14 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
阅读准备参考:https://www.jianshu.com/p/e739afb8fe31
需要导入模块:需要将mybatis-parent模块导入。mybatis-parent模块链接 https://github.com/mybatis/parent

------------------
补充Mybatis主要类及之间的关系(包含类的功能的注释)
/Users/network/GithubProject/mybatis注释/Mybatis主要类及之间的关系.puml
补充selectOne()方法的调用时序图(查询是流程最复杂的,捋清楚查询流程,插入等操作自然就会了)
select调用的时序图.puml

学习建议:
1.在看源码之前一定要掌握框架可以实现的功能
(强烈推荐看官方文档: https://mybatis.org/mybatis-3/zh/getting-started.html ),如果不是为了跳槽面试&不是真正热爱的话,吃透官方文档即可)
2.对照着官方文档的流程,在源码中一步一步跟着文档调试
3.看源码的过程中要了解每个包具体的作用
------------------


1.兵马未动,日志先行
org.apache.ibatis.logging
org.apache.ibatis.logging.commons
Expand Down
Binary file added select调用的时序图.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions select调用的时序图.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@startuml

participant main
participant SqlSessionFactoryBuilder
participant SqlSessionFactory
participant DefaultSqlSession


main->SqlSessionFactoryBuilder:1.builder()
SqlSessionFactoryBuilder-->main:SqlSessionFactory对象
main->SqlSessionFactory:2.openSession()
SqlSessionFactory-->main:SqlSession对象
main->DefaultSqlSession:3.selectOne()
DefaultSqlSession->DefaultSqlSession:selectList()
DefaultSqlSession->Configuration:getMappedStatement(statement)
Configuration-->DefaultSqlSession:MappedStatement对象

DefaultSqlSession->Executor:query(mappedStatement,parameter,rowBounds,Executor.NO_RESULT_HANDLER)
Executor->MappedStatement:getBoundSql()
MappedStatement-->Executor:BoundSql对象
Executor->Executor:createCacheKey()
Executor->Executor:query()
Executor->Executor:queryFromDatabase()




Executor-->DefaultSqlSession:查询的数据
DefaultSqlSession-->DefaultSqlSession:查询的数据
DefaultSqlSession-->main:查询的数据

SqlSessionFactory->DefaultSqlSession:openSession()
DefaultSqlSession->DefaultSqlSession:selectOne()
DefaultSqlSession->Configuration:selectList()
Configuration->Executor:getMappedStatement()
Executor->MappedStatement:getBoundSql()
@enduml
3 changes: 3 additions & 0 deletions src/main/java/org/apache/ibatis/annotations/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
/**
* Contains all the annotation that are used in mapper interfaces
*/
//本包定义了Mybatis框架中的24个注解。
//本包对Mybatis的其它包没有任何依赖,也不依赖于第三方的库。
//本包只被builder.annotation包的MapperAnnotationBuilder类引用。
package org.apache.ibatis.annotations;

1 change: 1 addition & 0 deletions src/main/java/org/apache/ibatis/binding/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Bings mapper interfaces with mapped statements
*/
//生成mapper.xml文件对应的实体的工具(反射相关)
package org.apache.ibatis.binding;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Parses annotions to create a Configuration
*/
//注解相关建造者类(根据注解获取配置信息存储到Configuration对象中)及相关异常
package org.apache.ibatis.builder.annotation;
1 change: 1 addition & 0 deletions src/main/java/org/apache/ibatis/builder/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Base package for the Configuration building code
*/
//建造者基类及相关异常
package org.apache.ibatis.builder;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Parses XML files to create a Configuration
*/
//xml配置文件相关建造者类(根据配置文件获取配置信息存储到Configuration对象中)及相关异常
package org.apache.ibatis.builder.xml;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Contains cache decorators
*/
//通过装饰器模式为缓存提供某种功能,形成一个装饰器链。
package org.apache.ibatis.cache.decorators;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Contains the default cache implementation
*/
//直接实现了Cache接口
package org.apache.ibatis.cache.impl;
2 changes: 2 additions & 0 deletions src/main/java/org/apache/ibatis/cache/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
/**
* Base package for caching stuff
*/
//缓存基类、异常、缓存中的key对象。
// 缓存框架按照 Key-Value方式存储,Key的生成采取规则为:[hashcode:checksum:mappedStementId:offset:limit:executeSql:queryParams]。
package org.apache.ibatis.cache;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* JNDI Datasource factory
*/
//JNDI数据源工厂
package org.apache.ibatis.datasource.jndi;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Base package for Datasources
*/
//数据源工厂及异常。(工厂在xmlConfigBuilder中被调用)
package org.apache.ibatis.datasource;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Simple single-thread pooled datasource
*/
//观上,及该包各个类的注释
package org.apache.ibatis.datasource.pooled;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Hyper-simple Datasource.
*/
//不使用池的数据源
package org.apache.ibatis.datasource.unpooled;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Base package for exceptions.
*/
//本包定义了Mybatis框架中的异常,只依赖于Mybatis的executor的ErrorContext。Mybatis的其它包大量引用了本包中的类和接口,即严重依赖于本包。
package org.apache.ibatis.exceptions;
6 changes: 6 additions & 0 deletions src/main/java/org/apache/ibatis/executor/ErrorContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ public class ErrorContext {
private static final ThreadLocal<ErrorContext> LOCAL = new ThreadLocal<ErrorContext>();

private ErrorContext stored;
//异常存在于哪个资源文件中
private String resource;
//存储异常是做什么操作时发生的
private String activity;
//存储哪个对象操作时发生异常。
private String object;
//存储异常的概览信息。
private String message;
//存储发生异常的 SQL 语句。
private String sql;
//存储详细的 Java 异常日志。
private Throwable cause;

//单例模式
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Contains the key generators
*/
//主键生成器
package org.apache.ibatis.executor.keygen;
1 change: 1 addition & 0 deletions src/main/java/org/apache/ibatis/executor/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Contains the statement executors.
*/
//包含各种执行器,对数据库的增删改查都是通过session调用执行器来执行的
package org.apache.ibatis.executor;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Base package for handling parameters.
*/
//参数处理器基类。参数处理器是对sql语句中的参数进行类型转换及对查询结果类型装换。ps:类型转换指java类型和数据库类型的映射
package org.apache.ibatis.executor.parameter;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Contains the result handlers.
*/
//结果处理器。功能与拦截器后置处理类似。只是效率会更高一些。
package org.apache.ibatis.executor.result;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Contains the result processing logic
*/
//将jdbc的resultset映射为mybatis中的resultMap
package org.apache.ibatis.executor.resultset;
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
* 语句处理器
*
*/
//创建、管理JDBC中的statement对象
public interface StatementHandler {

//准备语句
Statement prepare(Connection connection)
throws SQLException;

//参数化
//参数化 用于初始化 Statement 对象以及对sql的占位符进行赋值
void parameterize(Statement statement)
throws SQLException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Statement handlers.
*/
//StatementHandler是执行器内部调用,真正来创建、管理jdbc中statement的类
package org.apache.ibatis.executor.statement;
1 change: 1 addition & 0 deletions src/main/java/org/apache/ibatis/jdbc/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* TODO fillme.
*/
//与Sql相关的操作。如Sql运行器,脚本运行器和Sql封装类等
package org.apache.ibatis.jdbc;
1 change: 1 addition & 0 deletions src/main/java/org/apache/ibatis/logging/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* TODO fillme.
*/
//各个类型的日志适配器,都实现了Log接口。
package org.apache.ibatis.logging;
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* 映射的语句
*
*/
//实际上就是mapper.xml中的(select|insert|update|delete)四个标签的实例化对象
public final class MappedStatement {

private String resource;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/apache/ibatis/mapping/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* TODO fillme.
*/
//比较杂,主要是一些mapper.xml配置文件的解析工具及标签解析后对应的实体
package org.apache.ibatis.mapping;
2 changes: 2 additions & 0 deletions src/main/java/org/apache/ibatis/parsing/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
/**
* Parsing utils
*/
//变量解析.如解析${},#{}等

package org.apache.ibatis.parsing;
1 change: 1 addition & 0 deletions src/main/java/org/apache/ibatis/plugin/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Base package for handing plugins.
*/
//包含插件的定义接口。插件主要是拦截器的实现,顺便一提pageHelper插件,即是插件接口的实现
package org.apache.ibatis.plugin;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Reflection utils.
*/
//主要是一些反射操作的工具方法和对象工厂类,以及一些常用的包装类
package org.apache.ibatis.reflection;
2 changes: 1 addition & 1 deletion src/main/java/org/apache/ibatis/session/SqlSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public interface SqlSession extends Closeable {
* Execute an insert statement with the given parameter object. Any generated
* autoincrement values or selectKey entries will modify the given parameter
* object properties. Only the number of rows affected will be returned.
* 插入记录,容许传入参数
* 插入记录,允许传入参数
* @param statement Unique identifier matching the statement to execute.
* @param parameter A parameter object to pass to the statement.
* @return int The number of rows affected by the insert. 注意返回的是受影响的行数
Expand Down
Loading