[Five rats in Mybatis are in Tokyo]

During the Renzong period of the Northern Song Dynasty, there were famous Three Heroes and Five Righteousness on the rivers and lakes, among which the Five Righteousness is also known as the Five Rats: Lu Fang, the Gopher Rat, Han Zhang, the Gopher Rat, Xu Qing, the Mountain Rat, Jiang Ping, the River Rat, and Bai Yutang, the Jin Mao Rat ,

"Three Heroes" refers to the North Hero Ouyang Chun, Nanxia Zhanzhao, Ding's Double Heroes Ding Zhaolan and Ding Zhaohui as one Hero; Qing, Jiang Ping, Jiang Ping, and Bai Yutang, the golden-haired mouse, are the five-mouse brothers.

 

1. Introduction to the five mice in Mybatis

The operation of SqlSession on the database will be delegated to the executor Executor to complete, and the Executor consists of five mice, namely:

Simple mouse SimpleExecutor,

Reuse mouse ReuseExecutor,

Batch rat BatchExecutor,

Caching Mouse CachingExecutor,

Useless rat ClosedExecutor.

 

Second, the five rat family tree is as follows:



 

What skills does the five rats have to make a noise in Tokyo?

 

3. The skills of the five rats --> the eight immortals cross the sea and show their magical powers

1. Simple Executor: Every time update or select is executed, a Statement object is opened, and the Statement object is closed immediately after use. (can be a Statement or PrepareStatement object)

 

2. Reuse the mouse ReuseExecutor: execute update or select, use sql as the key to find the Statement object, use it if it exists, and create it if it does not exist. After use, the Statement object is not closed, but is placed in Map<String, Statement> for next time use. (can be a Statement or PrepareStatement object)

 

3. Batch mouse BatchExecutor: execute update (no select, JDBC batch does not support select), add all sql to the batch (addBatch()), wait for unified execution (executeBatch()), it caches multiple Statements Object, each Statement object is waiting to execute executeBatch() batches one by one after addBatch() is completed; BatchExecutor is equivalent to maintaining multiple buckets, and each bucket contains a lot of its own SQL, just like Apple Blue There are a lot of apples in it, a lot of tomatoes in the tomato blue, and finally, they are poured into the warehouse. (can be a Statement or PrepareStatement object)

 

4. CachingExecutor: A model of decorative design pattern, first get the query result from the cache, return it if it exists, and then delegate it to the Executor delegate to fetch it from the database. The delegate can be any of the above SimpleExecutor, ReuseExecutor, and BatchExecutor.

 

5. Useless Mouse ClosedExecutor: Useless , readers can view its source code by themselves, only as a kind of identification, which is equivalent to the Serializable tag interface.

 

Scope of action: The scope of action of the above five mice is strictly limited to the scope of the SqlSession life cycle.

 

Fourth, the introduction of design patterns in Mybatis

Mybatis has encountered at least the use of the following design patterns:

1)Builder模式,例如SqlSessionFactoryBuilder、XMLConfigBuilder、XMLMapperBuilder、XMLStatementBuilder、CacheBuilder;

2) Factory mode , such as SqlSessionFactory, ObjectFactory, MapperProxyFactory;

3) Singleton mode, such as ErrorContext and LogFactory;

4) Proxy mode , the core of Mybatis implementation, such as MapperProxy, ConnectionLogger, uses jdk's dynamic proxy; and the executor.loader package uses cglib or javassist to achieve the effect of delayed loading;

5) Combination mode , such as SqlNode and various subclasses ChooseSqlNode, etc.;

6) Template method patterns , such as BaseExecutor and SimpleExecutor, as well as BaseTypeHandler and all subclasses such as IntegerTypeHandler;

7) Adapter mode , such as Log's Mybatis interface and its adaptation to various log frameworks such as jdbc and log4j;

8) Decorator mode, such as the implementation of each decorator in the cache.decorators sub-package in the Cache package;

9) Iterator mode , such as iterator mode PropertyTokenizer;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529076&siteId=291194637