[Daily 3 minutes technological Dry | face questions + answers | MyBatis articles (a)]

What is the difference 1. MyBatis in # {} and {} $ is?

# {} Is a pre-compiler process, $ {} is the character replacement. When using the # {}, MyBatis will be in SQL #} {replace "?", Set with PreparedStatement method of assignment, which can effectively prevent SQL injection, to ensure the safe operation of the program.

2. MyBatis There are several ways pagination?

Pagination: logical page and physical page.

Logical page: using MyBatis RowBounds own paging, a lot of data which is a one-time query, and then retrieve the data.

Physical page: own handwriting or use SQL pagination pagination plug PageHelper, paged data to database queries in the form of a specified number of pieces.

3. RowBounds are all one-time query results? ** Why? **

RowBounds surface data is retrieved in the "all" data, it is not a one-time check out all the data, because the package is to jdbc MyBatis, there is disposed in a Fetch Size jdbc drive, which defines the maximum from the database query how many pieces of data, if you want to query more data, it will be when you execute next (), and to query more data. ATMs like to take than you 10,000 yuan, but cash machines can take a maximum of 2,500 yuan, so you have to take the money taken 4 times to finish. For just jdbc, when you call next () will automatically help you complete the query work. The benefit of this can effectively prevent memory overflow.

What is the difference 4. MyBatis logical page and physical page is?

Many logical page data is a one-time query, and then retrieve the data pages in the results. Disadvantages of doing so is the need to consume a lot of memory, there is the risk of memory overflow, the larger the database the pressure.

Physical page is a query from the database specified number of pieces, to make up for the shortcomings of all the data all at once isolated, such as the need a lot of memory, large database queries and other issues on the pressure.

5. MyBatis support lazy loading? ** What is the delay in loading the principle? **

MyBatis supports lazy loading, you can set lazyLoadingEnabled = true.

Delayed loading principle is called when the trigger is loaded, rather than at initialization to load the information. For example, call a. GetB (). GetName (), this time found a. GetB () value is null, this time alone will save the SQL trigger pre-B good association object, first check out B, and then call a. setB (b), but this time to call a. getB (). getName () will have value, and this is the basic principle lazy loading.

6. MyBatis said about the cache and secondary cache?

Cache: Based PerpetualCache the local cache HashMap, its lifecycle and SQLSession is the same, there are multiple SQLSession or distributed environment database operations, it may appear dirty data. When the Session flush or close, all the Session Cache will be cleared in the default cache is enabled.

Secondary cache: also based on the HashMap PerpetualCache local cache, it is stored in a different scope as Mapper level, if necessary among a plurality SQLSESSION shared cache, you need to use the secondary cache and the secondary cache memory source customizable such as Ehcache. The default secondary cache is not opened, to open the secondary cache, the secondary cache using the properties required to implement the Serializable class serialization interface (used to save the state of the object).

Open secondary cache data query process: secondary cache -> cache -> Database.

Cache update mechanism: when a certain scope (a cache Session / secondary cache Mapper) were C / U / D operation, the default cache select all this scope will be clear.

7. MyBatis and hibernate difference of what?

Flexibility: MyBatis more flexible, you can write SQL statements, more convenient to use.

Portability: MyBatis has a lot to write their own SQL, because each SQL database can be different, so portability is relatively poor.

Learn and use threshold: MyBatis entry is relatively simple, using the threshold is lower.

Secondary cache: Hibernate have a better secondary cache, it can be replaced as a secondary cache secondary cache of third parties.

What actuator (Executor) 8. MyBatis there?

There are three basic MyBatis Executor executor:

SimpleExecutor: Each time update or select to open a Statement object, run out immediately closed Statement object;

ReuseExecutor: execute update or select, SQL as a key to find the Statement object exists on the use, it does not exist to create, after use do not close the Statement object, but placed in the Map for the next time you use. In short, re-use the Statement object;

BatchExecutor: execute update (not select, jdbc batch does not support select), all SQL are added to the batch (addBatch ()), waiting for the uniform implementation (executeBatch ()), it caches multiple Statement objects, each Statement objects are the addBatch () after completion, the executeBatch executed one wait () batch, the batch jdbc same.

What The principle 9. MyBatis pagination plug-in is?

The basic principle is to use the plug-in tab MyBatis plugin interface provided, to achieve a custom plug-ins to intercept interception to be performed within the SQL insert method, and then rewrite the SQL, according to the dialect dialect, add statements and physical page corresponding to the physical paging parameters.

10. MyBatis how to write a custom plug-in?

Custom plug-in implementation principle

MyBatis custom plug-ins to intercept MyBatis for the four objects (Executor, StatementHandler, ParameterHandler, ResultSetHandler):

Executor: interceptor inner actuator, which is responsible for the operation invocation StatementHandler database, and the results set automatically by mapping ResultSetHandler, it also processing operation of the secondary cache;

StatementHandler: interception processing SQL syntax construction, it is MyBatis and database objects execute SQL scripts directly, while it also implements a cache of MyBatis;

ParameterHandler: interception processing parameters;

ResultSetHandler: treatment interception result set.

Custom plug-in implementation key

To implement Interceptor MyBatis plug, comprising an interface method, as follows:

public interface Interceptor {

Object intercept(Invocation invocation) throws Throwable;

Object plugin(Object target);

void setProperties(Properties properties);

}

setProperties method is time for a plug disposed in MyBatis can be configured for custom attributes, namely: object that implements the interface configuration parameter;

plugin plugin method is used to encapsulate the target object, by which we can return to the target object itself, it can also return a proxy, you can decide whether you want to intercept and then decide to return what kind of audience, provides an example of official .: return Plugin wrap (target, this);

intercept method is the time to intercept method to be executed.

Custom plug-implementation example

Official plug-in implementation:

@Intercepts({@Signature(type = Executor. class, method = "query",args = {MappedStatement. class, Object. class, RowBounds. class, ResultHandler. class})})public class TestInterceptor implements Interceptor {public Object intercept(Invocation invocation) throws Throwable {Object target = invocation. getTarget(); //被代理对象Method method = invocation. getMethod(); //代理方法Object[] args = invocation. getArgs(); //方法参数// do something . . . . . .  方法拦截前执行代码块Object result = invocation. proceed();// do something . . . . . . . 方法拦截后执行代码块return result;}public Object plugin(Object target) {return Plugin. wrap(target, this);}}复制代码

Epilogue

Take these words to encourage each other, encourage each other now. The more work, the more luck if you are not the official second-generation, rich second-generation, second generation red, remember: hard work is the only shortcut to change your destiny.

Feel free to leave your opinions in the comments section to discuss improving together. If today's article gives you new inspiration, new understanding to enhance the learning ability, welcome to forward share to more people.

Welcome to our readers to join programmer ** knowledge dock ** technology group , replies "No background in public plus group " can be.

![](data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="620" height="23"></svg>)

Guess you want to see

1. always ask interview jvm tuning in the end is going to do?

2. Programmers should have what kind of career planning? Worth thinking about!

3. Daily 3 minutes technological Dry | face questions + answers | jvm articles (a)

4. Daily 3 minutes technological Dry | face questions + answers | jvm articles (b)

5. Daily 3 minutes technological Dry | face questions + answers | Redis articles (a)

6. Daily 3 minutes technological Dry | face questions + answers | Mysql articles (a)

7. Daily 3 minutes technological Dry | face questions + answers | RabbitMQ articles (a)

8. Daily 3 minutes technological Dry | face questions + answers | Zookeeper articles (a)

9. Daily 3 minutes technological Dry | face questions + answers | Spring & SpringMVC articles (a)

10. Daily 3 minutes technological Dry | face questions + answers | SpringBoot articles (a)

11. Daily 3 minutes technological Dry | face questions + answers | SpringCloud articles (a)

12. Daily 3 minutes technological Dry | face questions + answers | multi-threaded articles (a)

Guess you like

Origin blog.51cto.com/14626895/2464290