Java the most common interview questions: Module thirteen

Thirteen, Mybatis

What is the difference 125. mybatis in # {} and {} $ is?

  • # {} Is a pre-compiler process, the replacement string is $ {};

  • Mybatis when processing # {}, will be in sql number # {} with the call set assignment method PreparedStatement to?;

  • When processing Mybatis $ {}, {} $ is to replace the value of the variable;

  • Use # {} can effectively prevent SQL injection, improve system security.

 

126. mybatis There are several ways pagination?

  1. Array Page

  2. sql pagination

  3. Interceptor page

  4. RowBounds page

 

What is the difference 128. mybatis logical page and physical page is?

  • Speed ​​is not necessarily the physical page is faster than logical page, the logical page is not necessarily speed faster than physical pages.

  • Always superior physical page logical page: the database is not necessary to belong to the pressure imposed to the application end to end, even if there is an advantage of speed, but the other performance advantages sufficient to make it up.

 

129. mybatis support lazy loading? What is the principle of lazy loading?

Mybatis association only supports lazy loading the associated object collection and a collection of associated objects, association refers to one, collection refers to the many queries. In Mybatis configuration file, you can configure whether to enable lazy loading lazyLoadingEnabled = true | false.

 

Its principle is to use CGLIB create a proxy object of the target object when the target method call, enter the interceptor methods, such as call a.getB (). GetName (), interceptor invoke () method found a.getB () is null value, then it will separate the saved queries sent in advance sql associated object B, the query up B, then call a.setB (b), then there is a target attribute value b, and then completed a.getB ( ) call .getName () method. This is the basic principle lazy loading.

 

Of course, not only Mybatis, including almost all of Hibernate, lazy loading support for the principle is the same.

 

130. mybatis talk about the cache and secondary cache?

 

A cache: a HashMap based PerpetualCache local cache, which stores a scope of Session, Session after the flush or close, all of the Cache will be emptied in the Session, by default cache.

 

And a secondary cache which caches the same mechanism, but also using default PerpetualCache, HashMap stored, it is stored in a different scope as Mapper (Namespace), and may be custom storage source, 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), it may be disposed on mapping file <cache />;

 

For caching data update mechanism, when performing the C / U / D operation of a certain scope (a cache Session / secondary cache the Namespaces) after, select default cache scope all this will be clear.

 

131. mybatis and hibernate difference is what?

 

Different (1) Mybatis and hibernate, it is not exactly an ORM framework, because MyBatis require programmers to write their own Sql statement.

 

(2) Mybatis write directly to the original ecology sql, sql execution can be tightly controlled performance, high flexibility, ideal for less demanding on the relational data model of software development, such software because of frequent changes in demand, but demand a change called for the speedy achievement output . But that flexibility can not be done on the premise that mybatis database-independent, if the need to implement the software supports multiple databases, you need a custom map file sets sql heavy workload. 

 

(3) Hibernate object / relational mapping capabilities strong, independent of the database is good for high-relational model requires software, if hibernate developers can save a lot of code and improve efficiency. 

 

What actuator (Executor) 132. mybatis there?

 

There are three basic Mybatis actuator (Executor):

 

  1. SimpleExecutor : Each time update or select, just open a Statement object, run out immediately closed Statement object.

  2. ReuseExecutor : execute update or select, as a key to find sql 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 use. In short, re-use Statement objects.

  3. BatchExecutor : execute update (not select, JDBC does not support batch select), all sql are added to the batch (addBatch ()), waiting for the uniform implementation (executeBatch ()), it caches multiple Statement objects, each Statement objects are addBatch () after completion, waiting for execution one by one executeBatch () batch. And JDBC same batch.

 

What The principle 133. 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, sql intercept interception to be performed within the insert method, and then rewrite the sql, according dialect dialect, add statements and physical page corresponding to the physical paging parameters.

 

134. mybatis how to write a custom plug-in?

 

Mybatis custom plug-ins to intercept for Mybatis four objects (Executor, StatementHandler, ParameterHandler, ResultSetHandler), specific intercept way: 

  • Executor: Method interceptor actuator (log record) 

  • StatementHandler: interception Sql grammar construction process 

  • ParameterHandler: interception processing parameters 

  • Intercept processing result sets: ResultSetHandler 

 

Mybatis custom plug-in must implement Interceptor:

public interface Interceptor {
    Object intercept(Invocation invocation) throws Throwable;
    Object plugin(Object target);
    void setProperties(Properties properties);
}

 

intercept method: interceptors specific processing method of logic 

plugin Method: The signature generating dynamic proxy object signatureMap 

setProperties: Set Properties Property

Custom plug-in demo:

// ExamplePlugin.java
@Intercepts({@Signature(
  type= Executor.class,
  method = "update",
  args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
  public Object intercept(Invocation invocation) throws Throwable {
  Object target = invocation.getTarget(); //被代理对象
  Method method = invocation.getMethod(); //代理方法
  Object[] args = invocation.getArgs(); //Method parameters
   // do something before the block of code ...... intercept method 
  Object Result = invocation.proceed ();
   // After do something ....... block of code methods to knockdown 
  return Result; 
  } 
  public Object plugin (Object target) {
     return Plugin.wrap (target, the this ); 
  } 
  public  void the setProperties (the Properties Properties) { 
  } 
}

 

A plurality of configuration can @Intercepts @ Signature, @ Signature parameters are defined as follows: 

  • type: Class represents the interception, here is the Executor implementation class;

  • method: intercept representation, here it is the intercept Executor update method;

  • args: Representation parameters.

 

(Finish)

Guess you like

Origin www.cnblogs.com/xiaofengwang/p/11257378.html