[MyBatis] Executor and lazy loading

Executor

Mybatis There are three basic Executor SimpleExecutorexecutor, ReuseExecutor, , BatchExecutor.

  • SimpleExecutor: Every time an update or select is executed, a Statement object is opened, and the Statement object is closed immediately after use.

  • ReuseExecutor: performing update or select, as a key to search sql Statementobject exists on the use, there is created, after use, is not closed Statement object, but placed in the Map <String, Statement> within, for the next use. In short, it is to reuse the Statement object.

  • BatchExecutor: Execute update (no select, JDBC batch processing does not support select), add all sql to the batch (addBatch()), wait for unified execution (executeBatch()), it caches multiple Statement objects, each Statement objects are all after the addBatch() is completed, waiting for the executeBatch() batch processing to be executed one by one. Same as JDBC batch processing.

Scope of action: These characteristics of Executor are strictly limited within the life cycle of SqlSession.

How to specify which type of Executor to use in Mybatis?

In Mybatis configuration file you may specify the default setting (Settings) ExecutorTypethe actuator type, to be manually DefaultSqlSessionFactorycreated SqlSession way to pass ExecutorType type parameters, such as

SqlSession       openSession(ExecutorType execType)。

Configure the default actuator. SIMPLE is an ordinary executor; REUSE executor will reuse prepared statements; BATCH executor will reuse statements and perform batch updates.

MyBatis lazy loading

Does MyBatis support lazy loading

Mybatis only supports associationassociated objects and collectionlazy loading associated collection of objects, associationrefers to one, collectionrefers to the many queries. In the Mybatis configuration file, you can configure whether to enable delayed loading

lazyLoadingEnabled=true|false。

principle

Its principle is to use CGLIB to create the proxy object of the target object. When the target method is called, enter the interceptor method, such as calling a.getB().getName(), and the interceptor invoke() method finds that a.getB() is null value, then it will separately send the previously saved sql query associated with the B object, query B up, and then call a.setB(b), then the object b attribute of a has a value, and then complete a.getB( ).getName() method call. This is the basic principle of lazy loading.

Of course, not only Mybatis, but almost all, including Hibernate, support lazy loading in the same principle.

Guess you like

Origin blog.csdn.net/Black_Customer/article/details/107418026