Common Mybatis interview questions

 

 

table of Contents

1 Introduction

2. Mybatis is pre-compiled to prevent SQL injection

3. Usually an Xml mapping file will have a Dao interface corresponding to it. What is the working principle of this Dao interface? When the method in Dao interface has different parameters, can the method be overloaded?

4. How does Mybatis pagination? What is the principle of the paging plugin?

5. How does Mybatis encapsulate the sql execution result as a target object and return it? What are the mapping forms?

6. In the Xml mapping file, besides the common select|insert|update|delete tags, what other tags are there?

7. Primary and secondary cache

8. Does Mybatis support delayed loading? If so, what is its implementation principle?

9. In the Mybatis mapping file, if the A tag refers to the content of the B tag through include, can the B tag be defined after the A tag, or must it be defined before the A tag?

10. Briefly describe the mapping relationship between Mybatis's Xml mapping file and Mybatis internal data structure?

11. Briefly describe the operation principle of Mybatis plug-in and how to write a plug-in


1 Introduction

The basic function of Mybatis : realize the mapping of database and java data object class.

Common scenarios :

  • Define the corresponding method in the Dao interface (there is a data class that the return value is written by yourself, and the parameter that needs to be updated in the library if there is no return value), and then write in the xml file of the Dao interface based on the database statement specification of Mybatis Corresponding addition, deletion, modification and checking function.
  • After having a method to manipulate data, the user initiates an application, and the program can call the relevant Dao interface method through the controller to realize the interaction with the data in the database.

2. Mybatis is pre-compiled to prevent SQL injection

Reference article: https://blog.csdn.net/Longtermevolution/article/details/107719649

to sum up:

  • #{}是预编译处理(井括号。防止编译阶段的SQL注入

When Mybatis processes #{}, it replaces #{} in sql with a?
Sign , and calls the set method of PreparedStatement to assign a value; when Mybatis processes it, it replaces {} with the value of the variable.
Using #{} can effectively prevent SQL injection and improve system security.

  • ${}是字符串替换(美元括号)。

不能防止SQL注入。


3. Usually an Xml mapping file will have a Dao interface corresponding to it. What is the working principle of this Dao interface? When the method in Dao interface has different parameters, can the method be overloaded?

Dao interface is the Mapper interface that people often call.

The fully qualified name of the interface is the value of namespace in the mapping file;

The method name of the interface is the id value of the MappedStatement in the mapping file;

The parameters in the interface method are the parameters passed to sql.

Locate a method in the Dao interface : The Mapper interface has no implementation class. When the interface method is called, the interface full name + method name splicing string as the key value can uniquely locate a MappedStatement.

For example: com.mybatis3.mappers.StudentDao.findStudentById, you can only find the MappedStatement whose namespace is com.mybatis3.mappers.StudentDao with id = findStudentById. In Mybatis, each <select>, <insert>, <update>, <delete> tag will be parsed as a MappedStatement object.

  • The method in the Dao interface cannot be overloaded, because it is the storage and search strategy of the fully qualified name + method name.
  • The working principle of the Dao interface is the JDK dynamic proxy . Mybatis will use the JDK dynamic proxy to generate a proxy proxy object for the Dao interface when running. The proxy object proxy will intercept the interface method and execute the SQL represented by the MappedStatement instead, and then return the SQL execution result.

4. How does Mybatis pagination? What is the principle of the paging plugin?

Mybatis uses RowBounds object for paging, which is memory paging for ResultSet result set, not physical paging. You can write parameters with physical paging directly in SQL to complete physical paging, or you can use paging plug-in to complete physical paging .
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 interception method, then rewrite the SQL, and add the corresponding physical paging statement and physical paging parameters according to the dialect dialect.

  • basic concepts

Physical paging and memory paging and logical paging

  • Mybatis pagination
  • Memory paging (logical paging, access to the database once, and then through the program logic paging, the database burden is small): RowBounds object. Memory paging performed on the result set.
  • Physical paging (access to the database multiple times) : You can directly write parameters with physical paging in SQL to complete the physical paging function, or you can use the paging plug-in to complete the physical paging.
  • Mybatis physical paging use 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 interception method, then rewrite the SQL, and add the corresponding physical paging statement and physical paging parameters according to the dialect dialect.


5. How does Mybatis encapsulate the sql execution result as a target object and return it? What are the mapping forms?

  • The first is to use the <resultMap> tag to define the mapping relationship between column names and object attribute names one by one.
  • The second is to use the alias function of the sql column to write the column alias as the object attribute name, such as T_NAME AS NAME, the object attribute name is generally name, lowercase, but the column name is not case sensitive, Mybatis will ignore the case of the column name. Intelligently find the corresponding object attribute name, you can even write T_NAME AS NaMe, Mybatis can work normally.

With the mapping relationship between column names and attribute names, Mybatis creates objects through reflection , and at the same time uses the attributes reflected to the objects to assign and return one by one . Those attributes that cannot find the mapping relationship cannot be assigned.


6. In the Xml mapping file, besides the common select|insert|update|delete tags, what other tags are there?


Note: This question comes from the JD interviewer.
There are many other tags, plus 9 tags for dynamic sql, trim|where|set|foreach|if|choose|when|otherwise|bind, etc., among which is the sql fragment tag, and the sql fragment is introduced through the tag, which is not supported The self-incrementing primary key generates a strategy label.

 Briefly describe the operation principle of Mybatis plug-in and how to write a plug-in.
Mybatis can only write plug-ins for the four interfaces of ParameterHandler, ResultSetHandler, StatementHandler, and Executor. Mybatis uses the dynamic proxy of the JDK to generate proxy objects for the interfaces that need to be intercepted to implement the interfaces. Method interception function, whenever the methods of these four interface objects are executed, it will enter the interception method, specifically the invoke() method of InvocationHandler. Of course, only those methods that you specify to intercept will be intercepted. Implement the Interceptor interface of Mybatis and override the intercept() method, and then write an annotation for the plug-in to specify which methods of which interface you want to intercept. Remember, you also need to configure the plug-in you wrote in the configuration file.

 


7. Primary and secondary cache


1) 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.
2) The mechanism of the second-level cache is the same as that of the first-level cache. PerpetualCache and HashMap storage are also used by default. The difference is that the storage scope is Mapper (Namespace), and the storage source can be customized, such as Ehcache. To enable the second-level cache, you need to add a line to your SQL mapping file: <cache/>
3) For the cache data update mechanism, when a certain scope (first-level cache Session/second-level cache Namespaces) is performed C After the /U/D operation, all caches in select under this scope will be cleared by default.

 


8. Does Mybatis support delayed loading? If so, what is its implementation principle?


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.

 


9. In the Mybatis mapping file, if the A tag refers to the content of the B tag through include, can the B tag be defined after the A tag, or must it be defined before the A tag?


Although Mybatis parses Xml mapping files in order, the quoted B tag can still be defined anywhere, and Mybatis can recognize it correctly .

Principle : Mybatis parses the A tag and finds that the A tag refers to the B tag, but the B tag has not been parsed and does not exist yet. At this time, Mybatis will mark the A tag as an unresolved state, and then continue to parse the remaining tags, including the B tag , After all tags are parsed, Mybatis will re-parse the tags that are marked as unresolved. At this time, when the A tag is parsed, the B tag already exists, and the A tag can be parsed normally.

 


10. Briefly describe the mapping relationship between Mybatis's Xml mapping file and Mybatis internal data structure?


Mybatis encapsulates all Xml configuration information into the All-In-One heavyweight object Configuration. In the Xml mapping file, the <parameterMap> tag will be parsed as a ParameterMap object, and each of its child elements will be parsed as a ParameterMapping object. The <resultMap> tag will be parsed as a ResultMap object, and each of its child elements will be parsed as a ResultMapping object. Each <select>, <insert>, <update>, <delete> tag will be parsed as a MappedStatement object, and the sql in the tag will be parsed as a BoundSql object.


11. Briefly describe the operation principle of Mybatis plug-in and how to write a plug-in


Mybatis can only write plug-ins for the four interfaces of ParameterHandler, ResultSetHandler, StatementHandler, and Executor. Mybatis uses the dynamic proxy of the JDK to generate proxy objects for the interfaces that need to be intercepted to implement the interface method interception function. Whenever these four interface objects are executed Method, it will enter the interception method, specifically the invoke() method of InvocationHandler. Of course, only those methods that you specify to be intercepted will be intercepted. Implement the Interceptor interface of Mybatis and override the intercept() method, and then write an annotation for the plug-in to specify which methods of which interface you want to intercept. Remember, you also need to configure the plug-in you wrote in the configuration file.

 

Guess you like

Origin blog.csdn.net/Longtermevolution/article/details/108563107