diff --git "a/Mybatis\344\270\273\350\246\201\347\261\273\345\217\212\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.png" "b/Mybatis\344\270\273\350\246\201\347\261\273\345\217\212\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.png" new file mode 100644 index 0000000000..03029084bf Binary files /dev/null and "b/Mybatis\344\270\273\350\246\201\347\261\273\345\217\212\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.png" differ diff --git "a/Mybatis\344\270\273\350\246\201\347\261\273\345\217\212\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.puml" "b/Mybatis\344\270\273\350\246\201\347\261\273\345\217\212\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.puml" new file mode 100644 index 0000000000..17ef301a89 --- /dev/null +++ "b/Mybatis\344\270\273\350\246\201\347\261\273\345\217\212\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.puml" @@ -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 \ No newline at end of file diff --git a/README b/README index 97b24fc01c..795390c4f0 100644 --- a/README +++ b/README @@ -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 diff --git "a/select\350\260\203\347\224\250\347\232\204\346\227\266\345\272\217\345\233\276.png" "b/select\350\260\203\347\224\250\347\232\204\346\227\266\345\272\217\345\233\276.png" new file mode 100644 index 0000000000..278a4583e1 Binary files /dev/null and "b/select\350\260\203\347\224\250\347\232\204\346\227\266\345\272\217\345\233\276.png" differ diff --git "a/select\350\260\203\347\224\250\347\232\204\346\227\266\345\272\217\345\233\276.puml" "b/select\350\260\203\347\224\250\347\232\204\346\227\266\345\272\217\345\233\276.puml" new file mode 100644 index 0000000000..15985ec2ed --- /dev/null +++ "b/select\350\260\203\347\224\250\347\232\204\346\227\266\345\272\217\345\233\276.puml" @@ -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 \ No newline at end of file diff --git a/src/main/java/org/apache/ibatis/annotations/package-info.java b/src/main/java/org/apache/ibatis/annotations/package-info.java index 12c86c8974..77394b80b0 100644 --- a/src/main/java/org/apache/ibatis/annotations/package-info.java +++ b/src/main/java/org/apache/ibatis/annotations/package-info.java @@ -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; diff --git a/src/main/java/org/apache/ibatis/binding/package-info.java b/src/main/java/org/apache/ibatis/binding/package-info.java index c61dcd13c2..39f382a16d 100644 --- a/src/main/java/org/apache/ibatis/binding/package-info.java +++ b/src/main/java/org/apache/ibatis/binding/package-info.java @@ -17,4 +17,5 @@ /** * Bings mapper interfaces with mapped statements */ +//生成mapper.xml文件对应的实体的工具(反射相关) package org.apache.ibatis.binding; diff --git a/src/main/java/org/apache/ibatis/builder/annotation/package-info.java b/src/main/java/org/apache/ibatis/builder/annotation/package-info.java index 5f13244da3..76b5ab48e8 100644 --- a/src/main/java/org/apache/ibatis/builder/annotation/package-info.java +++ b/src/main/java/org/apache/ibatis/builder/annotation/package-info.java @@ -17,4 +17,5 @@ /** * Parses annotions to create a Configuration */ +//注解相关建造者类(根据注解获取配置信息存储到Configuration对象中)及相关异常 package org.apache.ibatis.builder.annotation; diff --git a/src/main/java/org/apache/ibatis/builder/package-info.java b/src/main/java/org/apache/ibatis/builder/package-info.java index 4721dc46a5..0a7a37c06e 100644 --- a/src/main/java/org/apache/ibatis/builder/package-info.java +++ b/src/main/java/org/apache/ibatis/builder/package-info.java @@ -17,4 +17,5 @@ /** * Base package for the Configuration building code */ +//建造者基类及相关异常 package org.apache.ibatis.builder; diff --git a/src/main/java/org/apache/ibatis/builder/xml/package-info.java b/src/main/java/org/apache/ibatis/builder/xml/package-info.java index e9b87d81ab..80c9686da0 100644 --- a/src/main/java/org/apache/ibatis/builder/xml/package-info.java +++ b/src/main/java/org/apache/ibatis/builder/xml/package-info.java @@ -17,4 +17,5 @@ /** * Parses XML files to create a Configuration */ +//xml配置文件相关建造者类(根据配置文件获取配置信息存储到Configuration对象中)及相关异常 package org.apache.ibatis.builder.xml; diff --git a/src/main/java/org/apache/ibatis/cache/decorators/package-info.java b/src/main/java/org/apache/ibatis/cache/decorators/package-info.java index 18466b8087..18025c7ee4 100644 --- a/src/main/java/org/apache/ibatis/cache/decorators/package-info.java +++ b/src/main/java/org/apache/ibatis/cache/decorators/package-info.java @@ -17,4 +17,5 @@ /** * Contains cache decorators */ +//通过装饰器模式为缓存提供某种功能,形成一个装饰器链。 package org.apache.ibatis.cache.decorators; diff --git a/src/main/java/org/apache/ibatis/cache/impl/package-info.java b/src/main/java/org/apache/ibatis/cache/impl/package-info.java index d8f7772edf..a8d6163c0b 100644 --- a/src/main/java/org/apache/ibatis/cache/impl/package-info.java +++ b/src/main/java/org/apache/ibatis/cache/impl/package-info.java @@ -17,4 +17,5 @@ /** * Contains the default cache implementation */ +//直接实现了Cache接口 package org.apache.ibatis.cache.impl; diff --git a/src/main/java/org/apache/ibatis/cache/package-info.java b/src/main/java/org/apache/ibatis/cache/package-info.java index 8adb882596..0be2da9f62 100644 --- a/src/main/java/org/apache/ibatis/cache/package-info.java +++ b/src/main/java/org/apache/ibatis/cache/package-info.java @@ -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; diff --git a/src/main/java/org/apache/ibatis/datasource/jndi/package-info.java b/src/main/java/org/apache/ibatis/datasource/jndi/package-info.java index afd3364d2e..bfc79f5cc3 100644 --- a/src/main/java/org/apache/ibatis/datasource/jndi/package-info.java +++ b/src/main/java/org/apache/ibatis/datasource/jndi/package-info.java @@ -17,4 +17,5 @@ /** * JNDI Datasource factory */ +//JNDI数据源工厂 package org.apache.ibatis.datasource.jndi; diff --git a/src/main/java/org/apache/ibatis/datasource/package-info.java b/src/main/java/org/apache/ibatis/datasource/package-info.java index cfebdfd94b..d00ff7185d 100644 --- a/src/main/java/org/apache/ibatis/datasource/package-info.java +++ b/src/main/java/org/apache/ibatis/datasource/package-info.java @@ -17,4 +17,5 @@ /** * Base package for Datasources */ +//数据源工厂及异常。(工厂在xmlConfigBuilder中被调用) package org.apache.ibatis.datasource; diff --git a/src/main/java/org/apache/ibatis/datasource/pooled/package-info.java b/src/main/java/org/apache/ibatis/datasource/pooled/package-info.java index 8940e50193..b23998d4b3 100644 --- a/src/main/java/org/apache/ibatis/datasource/pooled/package-info.java +++ b/src/main/java/org/apache/ibatis/datasource/pooled/package-info.java @@ -17,4 +17,5 @@ /** * Simple single-thread pooled datasource */ +//观上,及该包各个类的注释 package org.apache.ibatis.datasource.pooled; diff --git a/src/main/java/org/apache/ibatis/datasource/unpooled/package-info.java b/src/main/java/org/apache/ibatis/datasource/unpooled/package-info.java index 4e03cdcb1c..4879002698 100644 --- a/src/main/java/org/apache/ibatis/datasource/unpooled/package-info.java +++ b/src/main/java/org/apache/ibatis/datasource/unpooled/package-info.java @@ -17,4 +17,5 @@ /** * Hyper-simple Datasource. */ +//不使用池的数据源 package org.apache.ibatis.datasource.unpooled; diff --git a/src/main/java/org/apache/ibatis/exceptions/package-info.java b/src/main/java/org/apache/ibatis/exceptions/package-info.java index 7ca0eac520..5d6cf8bbb6 100644 --- a/src/main/java/org/apache/ibatis/exceptions/package-info.java +++ b/src/main/java/org/apache/ibatis/exceptions/package-info.java @@ -17,4 +17,5 @@ /** * Base package for exceptions. */ +//本包定义了Mybatis框架中的异常,只依赖于Mybatis的executor的ErrorContext。Mybatis的其它包大量引用了本包中的类和接口,即严重依赖于本包。 package org.apache.ibatis.exceptions; diff --git a/src/main/java/org/apache/ibatis/executor/ErrorContext.java b/src/main/java/org/apache/ibatis/executor/ErrorContext.java index a92f102ef0..1cb286013c 100644 --- a/src/main/java/org/apache/ibatis/executor/ErrorContext.java +++ b/src/main/java/org/apache/ibatis/executor/ErrorContext.java @@ -29,11 +29,17 @@ public class ErrorContext { private static final ThreadLocal LOCAL = new ThreadLocal(); private ErrorContext stored; + //异常存在于哪个资源文件中 private String resource; + //存储异常是做什么操作时发生的 private String activity; + //存储哪个对象操作时发生异常。 private String object; + //存储异常的概览信息。 private String message; + //存储发生异常的 SQL 语句。 private String sql; + //存储详细的 Java 异常日志。 private Throwable cause; //单例模式 diff --git a/src/main/java/org/apache/ibatis/executor/keygen/package-info.java b/src/main/java/org/apache/ibatis/executor/keygen/package-info.java index 9e29def660..cc86e12e40 100644 --- a/src/main/java/org/apache/ibatis/executor/keygen/package-info.java +++ b/src/main/java/org/apache/ibatis/executor/keygen/package-info.java @@ -17,4 +17,5 @@ /** * Contains the key generators */ +//主键生成器 package org.apache.ibatis.executor.keygen; diff --git a/src/main/java/org/apache/ibatis/executor/package-info.java b/src/main/java/org/apache/ibatis/executor/package-info.java index 0eb2ad3303..73f68ece9d 100644 --- a/src/main/java/org/apache/ibatis/executor/package-info.java +++ b/src/main/java/org/apache/ibatis/executor/package-info.java @@ -17,4 +17,5 @@ /** * Contains the statement executors. */ +//包含各种执行器,对数据库的增删改查都是通过session调用执行器来执行的 package org.apache.ibatis.executor; diff --git a/src/main/java/org/apache/ibatis/executor/parameter/package-info.java b/src/main/java/org/apache/ibatis/executor/parameter/package-info.java index 4ee1603717..81877aa473 100644 --- a/src/main/java/org/apache/ibatis/executor/parameter/package-info.java +++ b/src/main/java/org/apache/ibatis/executor/parameter/package-info.java @@ -17,4 +17,5 @@ /** * Base package for handling parameters. */ +//参数处理器基类。参数处理器是对sql语句中的参数进行类型转换及对查询结果类型装换。ps:类型转换指java类型和数据库类型的映射 package org.apache.ibatis.executor.parameter; diff --git a/src/main/java/org/apache/ibatis/executor/result/package-info.java b/src/main/java/org/apache/ibatis/executor/result/package-info.java index bd0425375e..0fada8d113 100644 --- a/src/main/java/org/apache/ibatis/executor/result/package-info.java +++ b/src/main/java/org/apache/ibatis/executor/result/package-info.java @@ -17,4 +17,5 @@ /** * Contains the result handlers. */ +//结果处理器。功能与拦截器后置处理类似。只是效率会更高一些。 package org.apache.ibatis.executor.result; diff --git a/src/main/java/org/apache/ibatis/executor/resultset/package-info.java b/src/main/java/org/apache/ibatis/executor/resultset/package-info.java index 449dbe8787..c31bc87ac8 100644 --- a/src/main/java/org/apache/ibatis/executor/resultset/package-info.java +++ b/src/main/java/org/apache/ibatis/executor/resultset/package-info.java @@ -17,4 +17,5 @@ /** * Contains the result processing logic */ +//将jdbc的resultset映射为mybatis中的resultMap package org.apache.ibatis.executor.resultset; diff --git a/src/main/java/org/apache/ibatis/executor/statement/StatementHandler.java b/src/main/java/org/apache/ibatis/executor/statement/StatementHandler.java index adc92cb1ad..b9189aa18e 100644 --- a/src/main/java/org/apache/ibatis/executor/statement/StatementHandler.java +++ b/src/main/java/org/apache/ibatis/executor/statement/StatementHandler.java @@ -31,13 +31,14 @@ * 语句处理器 * */ +//创建、管理JDBC中的statement对象 public interface StatementHandler { //准备语句 Statement prepare(Connection connection) throws SQLException; - //参数化 + //参数化 用于初始化 Statement 对象以及对sql的占位符进行赋值 void parameterize(Statement statement) throws SQLException; diff --git a/src/main/java/org/apache/ibatis/executor/statement/package-info.java b/src/main/java/org/apache/ibatis/executor/statement/package-info.java index 568e5632cf..cb6a4795d4 100644 --- a/src/main/java/org/apache/ibatis/executor/statement/package-info.java +++ b/src/main/java/org/apache/ibatis/executor/statement/package-info.java @@ -17,4 +17,5 @@ /** * Statement handlers. */ +//StatementHandler是执行器内部调用,真正来创建、管理jdbc中statement的类 package org.apache.ibatis.executor.statement; diff --git a/src/main/java/org/apache/ibatis/jdbc/package-info.java b/src/main/java/org/apache/ibatis/jdbc/package-info.java index 55e5271a80..b359d135cb 100644 --- a/src/main/java/org/apache/ibatis/jdbc/package-info.java +++ b/src/main/java/org/apache/ibatis/jdbc/package-info.java @@ -17,4 +17,5 @@ /** * TODO fillme. */ +//与Sql相关的操作。如Sql运行器,脚本运行器和Sql封装类等 package org.apache.ibatis.jdbc; diff --git a/src/main/java/org/apache/ibatis/logging/package-info.java b/src/main/java/org/apache/ibatis/logging/package-info.java index 083a6aaae2..e6c1f1b1ad 100644 --- a/src/main/java/org/apache/ibatis/logging/package-info.java +++ b/src/main/java/org/apache/ibatis/logging/package-info.java @@ -17,4 +17,5 @@ /** * TODO fillme. */ +//各个类型的日志适配器,都实现了Log接口。 package org.apache.ibatis.logging; diff --git a/src/main/java/org/apache/ibatis/mapping/MappedStatement.java b/src/main/java/org/apache/ibatis/mapping/MappedStatement.java index bd8cdd81cd..e6487d09d8 100644 --- a/src/main/java/org/apache/ibatis/mapping/MappedStatement.java +++ b/src/main/java/org/apache/ibatis/mapping/MappedStatement.java @@ -35,6 +35,7 @@ * 映射的语句 * */ +//实际上就是mapper.xml中的(select|insert|update|delete)四个标签的实例化对象 public final class MappedStatement { private String resource; diff --git a/src/main/java/org/apache/ibatis/mapping/package-info.java b/src/main/java/org/apache/ibatis/mapping/package-info.java index 3f5facbb05..840b025bf4 100644 --- a/src/main/java/org/apache/ibatis/mapping/package-info.java +++ b/src/main/java/org/apache/ibatis/mapping/package-info.java @@ -17,4 +17,5 @@ /** * TODO fillme. */ +//比较杂,主要是一些mapper.xml配置文件的解析工具及标签解析后对应的实体 package org.apache.ibatis.mapping; diff --git a/src/main/java/org/apache/ibatis/parsing/package-info.java b/src/main/java/org/apache/ibatis/parsing/package-info.java index 40bcf3f1e7..d9fd3a445c 100644 --- a/src/main/java/org/apache/ibatis/parsing/package-info.java +++ b/src/main/java/org/apache/ibatis/parsing/package-info.java @@ -17,4 +17,6 @@ /** * Parsing utils */ +//变量解析.如解析${},#{}等 + package org.apache.ibatis.parsing; diff --git a/src/main/java/org/apache/ibatis/plugin/package-info.java b/src/main/java/org/apache/ibatis/plugin/package-info.java index cc0e3686cb..6fccb43254 100644 --- a/src/main/java/org/apache/ibatis/plugin/package-info.java +++ b/src/main/java/org/apache/ibatis/plugin/package-info.java @@ -17,4 +17,5 @@ /** * Base package for handing plugins. */ +//包含插件的定义接口。插件主要是拦截器的实现,顺便一提pageHelper插件,即是插件接口的实现 package org.apache.ibatis.plugin; diff --git a/src/main/java/org/apache/ibatis/reflection/package-info.java b/src/main/java/org/apache/ibatis/reflection/package-info.java index 69f184937d..d68df5a8e9 100644 --- a/src/main/java/org/apache/ibatis/reflection/package-info.java +++ b/src/main/java/org/apache/ibatis/reflection/package-info.java @@ -17,4 +17,5 @@ /** * Reflection utils. */ +//主要是一些反射操作的工具方法和对象工厂类,以及一些常用的包装类 package org.apache.ibatis.reflection; diff --git a/src/main/java/org/apache/ibatis/session/SqlSession.java b/src/main/java/org/apache/ibatis/session/SqlSession.java index f675f1865d..247b72c598 100644 --- a/src/main/java/org/apache/ibatis/session/SqlSession.java +++ b/src/main/java/org/apache/ibatis/session/SqlSession.java @@ -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. 注意返回的是受影响的行数 diff --git a/src/main/java/org/apache/ibatis/session/SqlSessionManager.java b/src/main/java/org/apache/ibatis/session/SqlSessionManager.java index 3b1ee73aec..78dd7a9cce 100644 --- a/src/main/java/org/apache/ibatis/session/SqlSessionManager.java +++ b/src/main/java/org/apache/ibatis/session/SqlSessionManager.java @@ -35,6 +35,7 @@ * SqlSession管理员,可参考SqlSessionManagerTest * */ +//作用是复用session,session池,同一线程每次都获取同一session,而不需要像sessionfactory那样每次都创建新的,使用threadlocal实现。 public class SqlSessionManager implements SqlSessionFactory, SqlSession { private final SqlSessionFactory sqlSessionFactory; diff --git a/src/main/java/org/apache/ibatis/transaction/jdbc/package-info.java b/src/main/java/org/apache/ibatis/transaction/jdbc/package-info.java index a86d994b90..3663211b8d 100644 --- a/src/main/java/org/apache/ibatis/transaction/jdbc/package-info.java +++ b/src/main/java/org/apache/ibatis/transaction/jdbc/package-info.java @@ -17,4 +17,5 @@ /** * JDBC transaction. */ +//简单的jdbc事务,在statementhandler创建statement时,就是在事务中获取的数据源及连接等信息。 package org.apache.ibatis.transaction.jdbc; diff --git a/src/main/java/org/apache/ibatis/transaction/package-info.java b/src/main/java/org/apache/ibatis/transaction/package-info.java index bdbce546db..7133c22d40 100644 --- a/src/main/java/org/apache/ibatis/transaction/package-info.java +++ b/src/main/java/org/apache/ibatis/transaction/package-info.java @@ -17,4 +17,5 @@ /** * Base package for transactions. */ +//事务接口 package org.apache.ibatis.transaction; diff --git a/src/test/java/SimpleExample.java b/src/test/java/SimpleExample.java new file mode 100644 index 0000000000..6aa06f19e6 --- /dev/null +++ b/src/test/java/SimpleExample.java @@ -0,0 +1,36 @@ +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.apache.ibatis.submitted.basetest.Mapper; + +import java.io.IOException; + +/** + * @author: gaomz + * @date: 2020/6/25 + * @description: + */ + +public class SimpleExample { + //获取流 + String resource = "org/mybatis/example/mybatis-config.xml"; + //在构造者内部调用xmlconfig构造者解析流,将流解析后的数据创建为Configuration对象,将configuration对象赋值给工厂的Configuration属性,工厂只有这一个属性。 + SqlSessionFactory sqlSessionFactory; + + { + try { + sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource)); + SqlSession sqlSession = sqlSessionFactory.openSession(); + sqlSession.selectOne(new String()); + sqlSession.getMapper(Object.class); + + + } catch (IOException e) { + e.printStackTrace(); + } + } + + // + +}