Look here for the interview! The information is sorted out, what are you waiting for? Remember! 2020 latest Java collection of common interview questions + detailed answers (11)

2020 latest Java collection of common interview questions + detailed answers (11)

Continue to update Java related materials. Recently, I have spent 7 days consulting the latest interview news of the big guys in an effort to collect more comprehensive interview information. If you want to see the first few collections, you can go to my homepage to find them.

Some of the answers are summarized by myself, and some are collected on the Internet. Don't panic after watching these interviews! If you have more experience, you can share it in the comments. If you have any mistakes, you are welcome to point it out. Please let me know, thank you~

13. Mybatis

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

  • #{} is pre-compilation processing, ${} is string replacement;

  • When Mybatis processes #{}, it replaces #{} in sql with a? Sign and calls the set method of PreparedStatement to assign;

  • When Mybatis processes ${}, it replaces ${} with the value of the variable;

  • Using #{} can effectively prevent SQL injection and improve system security.

 

105. How many paging methods does mybatis have?

 

  1. Number group page

  2. sql paging

  3. Interceptor paging

  4. RowBounds pagination

 

106. What is the difference between mybatis logical paging and physical paging?

 

  • Physical paging is not necessarily faster than logical paging, and logical paging is not necessarily faster than physical paging.

  • Physical paging is always better than logical paging: there is no need to put pressure from the database side on the application side, even if there is an advantage in speed, but other performance advantages are enough to make up for this shortcoming.

It will be more effective if you look at the self-check of the question and then the detailed answer.

107. Does mybatis support lazy loading? What is the principle of lazy loading?

 

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

 

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), so 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.

 

108. Tell me about the first level cache and the second level cache of mybatis?

 

Level 1 cache: HashMap local cache based on PerpetualCache. Its storage scope is Session. After Session flush or close, all Caches in the Session will be emptied, and Level 1 cache is turned on by default.

 

The mechanism of the second-level cache is the same as that of the first-level cache. By default, PerpetualCache and HashMap storage are also used. The difference is that the storage scope is Mapper (Namespace), and the storage source can be customized, such as Ehcache. The second-level cache is not turned on by default. To enable the second-level cache, the use of the second-level cache attribute class needs to implement the Serializable serialization interface (which can be used to save the state of the object), which can be configured in its mapping file <cache/>;

 

For the cache data update mechanism, when a C/U/D operation is performed in a certain scope (first-level cache Session/second-level cache Namespaces), all caches in select under this scope will be cleared by default.

 

109. What are the differences between mybatis and hibernate?

 

(1) Mybatis is different from hibernate, it is not exactly an ORM framework, because MyBatis requires programmers to write Sql statements.

 

(2) Mybatis directly writes the original ecological SQL, which can strictly control the execution performance of SQL, and has high flexibility. It is very suitable for software development that does not require high requirements for relational data models, because the requirements of this type of software change frequently, and once the requirements change requires rapid output. . But the premise of flexibility is that mybatis cannot achieve database independence. If you need to implement software that supports multiple databases, you need to customize multiple sets of sql mapping files, which is a lot of work. 

 

(3) Hibernate has strong object/relational mapping capabilities and good database independence. For software with high requirements on relational models, if you develop with hibernate, you can save a lot of code and improve efficiency. 

110. What executors (Executor) does mybatis have?

 

Mybatis has three basic executors (Executor):

 

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

  2. 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 using it, the Statement object is not closed, but placed in the Map for the next use. In short, it is to reuse the Statement object.

  3. 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 one by one. Same as JDBC batch processing.

 

111. What is the realization principle of mybatis paging plug-in?

 

The basic principle of the paging plug-in is to use the plug-in interface provided by Mybatis to implement a custom plug-in, intercept the SQL to be executed in the plug-in's interception method, and then rewrite the SQL, according to the dialect dialect, add the corresponding physical paging statement and physical paging parameters.

 

112. How does mybatis write a custom plug-in?

 

Transfer from: blog.csdn.net/qq_30051265/article/details/80266434

 

Mybatis custom plug-in intercepts the four major objects of Mybatis (Executor, StatementHandler, ParameterHandler, ResultSetHandler). The specific interception method is: 

  • Executor: method of intercepting executor (log record) 

  • StatementHandler: intercept the processing of Sql syntax construction 

  • ParameterHandler: intercept parameter processing 

  • ResultSetHandler: intercept the processing of the result set 

 

Mybatis custom plug-in must implement the Interceptor interface:

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

intercept method: the interceptor specific processing logic method 

plugin method: generate dynamic proxy object based on signature map 

setProperties method: set the 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(); //方法参数
  // do something ...... 方法拦截前执行代码块
  Object result = invocation.proceed();
  // do something .......方法拦截后执行代码块
  return result;
  }
  public Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }
  public void setProperties(Properties properties) {
  }
}

One @Intercepts can be configured with multiple @Signatures. The parameters in @Signature are defined as follows: 

  • type: indicates the intercepted class, here is the implementation class of Executor;

  • method: indicates the method of interception, here is the update method of intercepting Executor;

  • args: Represents method parameters.

    At last

    The content of the interview questions is over here, there will be more updates in the follow-up, I hope it will be helpful to everyone.

    Finally, I want to say something to you. I have worked for so many years and have interviewed some people for others. Whether it is from the perspective of the interviewer or the leader, in addition to interview skills and experience, great technology and project experience are also their trump cards and confidence. Core technology sharing of first-tier manufacturers

     It took me a long time to sort out some learning materials. What I posted above is the tip of the iceberg in the materials. I hope I can help you! Click to learn the secret code together: csdn

                             

      I will share more pure dry goods articles in the follow-up, and hope to really help you. Your support is my biggest motivation! Welcome to follow and like!

                                                           

Guess you like

Origin blog.csdn.net/weixin_50333534/article/details/108967338