Do you know these MyBatis interview questions? What else to learn Java

MyBatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manual setting of parameters and obtaining result sets.
In this article, the following will be mentioned:

  • MyBatis concept
  • MyBatis advantages and disadvantages
  • MyBatis cache
  • MyBatis storage
  • MyBatis mapping
  • Common MyBatis interview questions
  • Suitable for the crowd: Java programmer friends who are learning Java development and preparing for interviews.

Due to limited space, only part of the interview questions are displayed here. Those who need the full version and more relevant Java knowledge points and interview questions can get them for free by clicking the link below!
Link: 1103806531 Password: CSDN

Insert picture description here

1. In best practice, 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?

Answer: Dao interface is the Mapper interface that people often say. The fully qualified name of the interface is the value of the namespace in the mapping file, the method name of the interface is the id value of the MappedStatement in the mapping file, and the parameters in the interface method are passed Parameters to sql. The Mapper interface has no implementation class. When the interface method is called, the interface full name + method name splicing string is used as the key value to uniquely locate a MappedStatement, for example: com.mybatis3.mappers.StudentDao.findStudentById, the namespace can be uniquely found MappedStatement of findStudentById for com.mybatis3.mappers.StudentDao below id=. In Mybatis, each,,, and label 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.

2. How does Mybatis perform paging? What is the principle of the paging plugin?

Answer: Mybatis uses RowBounds object for paging. It is memory paging for ResultSet result set, not physical paging. You can write parameters with physical paging directly in SQL to complete the physical paging function, or you can use the paging plugin 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.

For example: select * from student, after intercepting sql, rewrite as: select t.* from (select * from student) t limit 0,10

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

Answer: 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, and whenever these four interfaces are executed When the method of the object, it will enter the interception method, specifically the invoke() method of the 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 to the plug-in, specifying which methods of which interface you want to intercept, remember, don't forget to configure the plug-in you wrote in the configuration file.

4. Mybatis executes batch insert, can it return the database primary key list?

Answer: Yes, JDBC can, of course Mybatis can.

5. What does Mybatis dynamic sql do? What are the dynamic SQL? Can you briefly describe the implementation principle of dynamic sql?

Answer: Mybatis dynamic sql allows us to write dynamic sql in the form of tags in the Xml mapping file, complete the function of logical judgment and dynamic splicing sql, Mybatis provides 9 kinds of dynamic sql tags trim|where|set|foreach|if| choose|when|otherwise|bind.

Its execution principle is to use OGNL to calculate the value of the expression from the sql parameter object, and dynamically splice sql according to the value of the expression to complete the function of dynamic sql.

Insert picture description here

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

Answer: The first is to use tags 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. You can only find the corresponding object attribute name, you can even write it as T_NAME AS NaMe, and Mybatis can work normally.

With the mapping relationship between column names and attribute names, Mybatis creates objects through reflection, and 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.

7. Can Mybatis perform one-to-one and one-to-many related queries? What are the implementation methods, and the differences between them.

Answer: Yes, Mybatis can not only perform one-to-one, one-to-many related queries, but also perform many-to-one, many-to-many related queries, and many-to-one queries, which are actually one-to-one queries. You only need to put selectOne( ) Can be modified to selectList(); many-to-many query is actually one-to-many query, only need to modify selectOne() to selectList().

Related object query, there are two ways to achieve, one is to send a separate SQL to query the associated object, assign it to the main object, and then return to the main object. The other is to use nested query. The meaning of nested query is to use join query. Part of the column is the attribute value of the A object, and the other part is the attribute value of the associated object B. The advantage is that only one SQL query can be used. The main object and its associated objects are detected.

So the question is, how to determine that the main object is 5 instead of 100 if 100 records are obtained by the join query? The principle of deduplication is that the subtag in the tag specifies the id column that uniquely determines a record. Mybatis completes the deduplication function for 100 records according to the column value. There can be multiple, representing the semantic meaning of the joint primary key.

Similarly, the associated objects of the main object are repeated according to this principle, although under normal circumstances, only the main object will have duplicate records, and the associated objects generally do not repeat.

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

Answer: Mybatis only supports 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 the principle of lazy loading.

9. In the Xml mapping file of Mybatis, can the id be repeated for different Xml mapping files?

Answer: For different Xml mapping files, if the namespace is configured, then the id can be repeated; if the namespace is not configured, then the id cannot be repeated; after all, the namespace is not required, it is just a best practice.

The reason is that namespace+id is used as the key of Map<String, MappedStatement>. If there is no namespace, there will be an id, so duplicate id will cause data to overwrite each other. With namespace, id can be repeated naturally, and namespace+id is naturally different if namespace is different.

10. How to execute batch processing in Mybatis?

Answer: Use BatchExecutor to complete batch processing.

Insert picture description here

11. What Executor executors does Mybatis have? What is the difference between them?

Answer: Mybatis has three basic Executor executors, SimpleExecutor, ReuseExecutor, BatchExecutor.

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

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 Map<String, Statement> for the next use. In short, the Statement object is reused.

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.

Scope of action: These characteristics of Executor are strictly limited to the scope of the SqlSession life cycle.

12. How to specify which type of Executor to use in Mybatis?

Answer: In the Mybatis configuration file, you can specify the default ExecutorType actuator type, or you can manually pass the ExecutorType type parameter to the method of creating SqlSession of DefaultSqlSessionFactory.

13. Can Mybatis map Enum enumeration class?

Answer: Mybatis can map enumeration classes, not only enumeration classes, but Mybatis can map any object to a column of the table. The mapping method is to customize a TypeHandler and implement the setParameter() and getResult() interface methods of TypeHandler. TypeHandler has two functions, one is to complete the conversion from javaType to jdbcType, and the other is to complete the conversion from jdbcType to javaType, which is embodied in two methods, setParameter() and getResult(), which respectively represent setting sql question mark placeholder parameters and obtaining columns search result.

14. 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?

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

The principle is that Mybatis parses the A tag and finds that the A tag refers to the B tag, but the B tag has not been resolved 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 B Labels. After all the labels are parsed, Mybatis will re-parse the labels that are marked as unresolved. At this time, when the A label is parsed, the B label already exists, and the A label can be resolved normally.

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

Answer: Mybatis encapsulates all Xml configuration information into the All-In-One heavyweight object Configuration. In the Xml mapping file, tags will be parsed as ParameterMap objects, and each of its child elements will be parsed as ParameterMapping objects. The label will be parsed as a ResultMap object, and each of its child elements will be parsed as a ResultMapping object. Each,,, and tag will be parsed as a MappedStatement object, and the sql in the tag will be parsed as a BoundSql object.

Insert picture description here

At last

Hope this article is helpful to everyone!

I have also compiled a complete set of video tutorials for architects and systematic materials on java, including java core knowledge points, interview topics, and the latest 20 years of Internet real questions and e-books. Friends in need can click the link below to get it for free!
Link: 1103806531 Password: CSDN

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48655626/article/details/108599444