Summary of Java classic interview questions: mybatis


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

#{} is precompiled processing and ${} is character substitution. When #{} is used, mybatis will replace #{} in SQL with “?”, With the set method assignment of Preparedstatement, this can effectively prevent SQL injection and ensure the operation safety of the program.

2. How many paging methods does mybatis have?

Logical paging: use the rowbounds provided by mybatis for paging. It queries a lot of data at one time, and then retrieves it in the data.

Physical paging: write your own SQL paging or use the paging plug-in PageHelper to query the form of paging data with a specified number of entries in the database.

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

Logical paging is to query a lot of data at one time, and then retrieve the paged data in the result. The disadvantages of doing this are that it needs to consume a lot of memory, has the risk of memory overflow, and has great pressure on the database.

Physical paging is to query a specified number of data from the database, which makes up for the shortcomings of all the data found at one time, such as the need for a large amount of memory, great pressure on database query and so on.

4. Does mybatis support delayed loading? What is the principle of delayed loading?

Mybatis supports delayed loading. Set lazyloading enabled=true. The principle of delayed loading is to trigger loading when calling, rather than loading information when initializing. For example, call a. getb () Getname(), at this time, it is found that the value of A. getb() is null. At this time, the saved SQL of the associated B object will be triggered separately. First, query B, and then call a. setb (b), and then call a. getb() Getname () has a value, which is the basic principle of delayed loading

5. Talk about mybatis’ L1 cache and L2 cache?

First level cache: HashMap local cache based on perpetual cache. Its declaration cycle is consistent with sqlsession. There are multiple sqlsessions or database operations in distributed environment, and dirty data may appear. After the session flush or close, all caches in the session will be cleared, and the L1 cache is on by default.

L2 cache: it is also a local cache of HashMap based on perpetualcache. The difference is that its storage scope is mapper level. If multiple sqlsessions need to share cache, L2 cache needs to be used, and L2 cache can customize the storage source, such as ehcache. By default, L2 cache is not opened. To enable L2 cache, the serializable serialization interface (which can be used to save the state of objects) needs to be implemented when using L2 cache attribute classes. Start the L2 cache data query process: L2 cache – > L1 cache – > database. Cache update mechanism: when a scope (L1 cache session/ L2 cache mapper) performs c/u/d operations, the cache in all selections under this scope will be cleared by default.

6. What actuators does mybatis have?

Mybatis has three basic executor executors:

  • Simpleexecution: each time update or select is executed, a statement object will be opened, and the statement object will be closed immediately after use;
  • Reuseexecution: execute update or select, find the statement object with SQL as the key, use it if it exists, create it if it does not exist, and do not close the statement object after it is used up, but place it in the map for next use. In short, reuse the statement object;
  • Batchexecutor: execute update (no select, JDBC batch does not support select), add all SQL to the batch (addbatch()), and wait for unified execution (executebatch()), which caches multiple statement objects, each of which is addbatch(). After completion, wait for executebatch() batch to be executed one by one, the same as JDBC batch

7. What is the implementation 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 interception method of the plug-in, then rewrite the SQL, and add the corresponding physical paging statements and physical paging parameters according to the dialect of dialect.

8. How does mybatis return the primary key?

The core lies in the usegeneratedkeys and keyproperty properties

<mapper namespace="org.chench.test.mybatis.mapper">
    <!--  Insert data: return the record primary key ID value -- >
    <insert parameterType="org.chench.test.mybatis.model.Test" useGeneratedKeys="true" keyProperty="id" >
        insert into test(name,descr,url,create_time,update_time) 

9. In addition to the common select | insert | update | delete tags in the XML Mapping file, what other tags are there?

There are many other tags, such as SQL | cache, etc.. Plus the nine tags of dynamic SQL, trim | where | set | foreach | if | choose | when | otherwise | bind, etc., which are SQL fragment tags. SQL fragments are introduced through tags to generate policy tags for primary keys that do not support self increment.

10. What are the differences between mybatis and Hibernate?

  • Flexibility: mybatis is more flexible. It can write SQL statements by itself, which is more convenient to use.
  • Portability: mybatis has many SQL written by itself. Because the SQL of each database can be different, the portability is relatively poor.
  • Learning and use threshold: mybatis is relatively simple to get started, and the threshold for use is also lower.
  • L2 cache: Hibernate has a better L2 cache, and its L2 cache can be replaced by a third-party L2 cache.


That’s all for this article. I hope it can help you, and I hope you can pay more attention to developeppaer!