Summary of Java Classic Interview Questions: Mybatis

  • 2021-11-02 00:47:14
  • OfStack

Directory 1. What is the difference between # {} and ${} in MyBatis? 2. How many paging methods does MyBatis have? 3. What is the difference between MyBatis logical and physical paging? 4. Does MyBatis support delayed loading? What is the principle of delayed loading? 5. Say level 1 cache and level 2 cache of MyBatis under 1? 6. What actuators does MyBatis have (Executor)? 7. What is the implementation principle of MyBatis paging plug-in? 8. How does MyBatis return a primary key? 9. In addition to the common selectinsertupdatedelete tags in the Xml mapping file, what tags are there? 10. What are the differences between MyBatis and Hibernate? Summarize

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

# {} is precompile processing and ${} is character substitution. When using # {}, MyBatis replaces # {} in SQL with "?" This method can effectively prevent SQL injection and ensure the safety of the program.

2. How many paging methods does MyBatis have?

Logical paging: Use RowBounds that comes with MyBatis for paging, which queries a lot of data once, and then retrieves it in the data.

Physical paging: Your own handwritten SQL paging or using the paging plug-in PageHelper, to the database query specified number of paging data form.

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

Logical paging is to query a lot of data once, and then retrieve the paged data in the result. The drawback of this is that it needs to consume a lot of memory, which has the risk of memory overflow and great pressure on the database.

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

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

MyBatis supports delayed loading, so set lazyLoadingEnabled=true. The principle of delayed loading is to trigger loading at the time of call, rather than loading information at the time of initialization. For example, when a. getB (). getName () is called, the value of a. getB () is found to be null. At this time, SQL associated with B object saved in advance will be triggered separately, B will be queried first, then a. setB (b) will be called, and then a. getB () will be called again. getName () will have a value, which is the basic principle of delayed loading

5. Say level 1 cache and level 2 cache of MyBatis under 1?

Level 1 cache: HashMap local cache based on PerpetualCache, whose declaration period is the same as SQLSession 1. There are multiple SQLSession or database operations in distributed environment, and dirty data may appear. After Session, flush or close, all Cache in that Session are emptied and the default level 1 cache is turned on.

Level 2 cache: It is also HashMap local cache based on PerpetualCache, but its storage scope is Mapper level. If multiple SQLSession need to share cache, it needs to use level 2 cache, and level 2 cache can customize storage source, such as Ehcache. Level-2 caching is not turned on by default; To turn Level-2 caching on, using the Level-2 caching property class requires the implementation of the Serializable serialization interface (which can be used to save the state of the object). Start the data query process of level 2 cache: level 2 cache- > Level 1 cache- > Database. Cache update mechanism: When an C/U/D operation is performed in a certain scope (level 1 cache Session/level 2 cache Mapper), all caches in select in this scope will be clear by default.

6. What actuators does MyBatis have (Executor)?

The MyBatis has three basic Executor actuators:

SimpleExecutor: One Statement object is opened every time update or select is executed, and the Statement object is closed immediately after use; ReuseExecutor: Execute update or select, use SQL as key to find Statement object, use it if it exists, create it if it does not exist, and do not close Statement object after use, but place it in Map for the next use. In short, it is to reuse Statement objects; BatchExecutor: Execute update (no select, select is not supported in jdbc batch), add all SQL to the batch (addBatch ()), wait for executeBatch ()), cache multiple Statement objects, wait for executeBatch () batch to be executed one by one after addBatch () is finished, the same as jdbc batch

7. What is the implementation principle of MyBatis paging plug-in?

The basic principle of paging plug-in is to use the plug-in interface provided by MyBatis to realize custom plug-in, intercept SQL to be executed in the plug-in interception method, then rewrite SQL, and add corresponding physical paging statements and physical paging parameters according to dialect dialect.

8. How does MyBatis return a primary key?

The core lies in the useGeneratedKeys and keyProperty attributes


<mapper namespace="org.chench.test.mybatis.mapper">
    <!--  Insert data : Return to record primary key id Value  -->
    <insert id="insertOneTest" parameterType="org.chench.test.mybatis.model.Test" useGeneratedKeys="true" keyProperty="id" >
        insert into test(name,descr,url,create_time,update_time) 
        values(#{name},#{descr},#{url},now(),now())
    </insert>
</mapper>

9. In addition to the common selectinsertupdatedelete tags in the Xml mapping file, what tags are there?

There are many other tags, such as sqlcach, etc., plus 9 tags of dynamic sql, trimwheresetforeachifchoosewhenotherwisebind, etc., among which sql fragment tags 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 more flexible, you can write SQL statements, more convenient to use. Portability: MyBatis has many SQL written by itself, because the SQL of each database can be different, so the portability is poor. Learning and use threshold: MyBatis is relatively simple to get started, and the use threshold is lower. Level 2 cache: hibernate has a better level 2 cache, and its level 2 cache can be replaced by the level 2 cache of the third party by itself.

Summarize

This article is here, I hope to give you help, but also hope you can give more attention to this site more content!


Related articles: