2020 Spring recruit 18 Mybatis interview questions must know

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

answer: Yes P r O p e r t i e s Culture s q l {} Is the Properties file placeholder variable, which can be a tag property value and the internal sql, a static text replacement, such as {Driver} will be replaced with a static com.mysql.jdbc.Driver. # {} Is sql parameter placeholder, Mybatis will in sql # {} with the? Number, parameter setting method will be used before the PreparedStatement sql executed sequentially to the sql? Parameter set number placeholder such ps.setInt (0, parameterValue), # {item.name} way using a reflection values ​​acquired item name attribute value of the object from the object parameter corresponds param.getItem (). getName ().

2, Xml mapping file, in addition to the common select | insert | updae | than delete the label, what label?

A: There are many other label ,,,,,, plus nine dynamic sql tag, trim | where | set | foreach | if | choose | when | otherwise | bind and so on, which is the sql fragment tag by tag sql fragment is introduced, the label generation strategy is not supported increment primary key.

3, best practice, usually a Xml mapping file, will write a corresponding interface to Dao, may I ask, what is the working principle of the interface is Dao? Dao interfaces in the methods, parameters are different, the method can reload it?

Answer: Dao interface is often said Mapper interface fully qualified name of the interface is the map file value namespace, and interface method name is the id mapping file MappedStatement values, parameters within the interface method is to transfer the parameters to sql. Mapper not interface implementation class, when a method call interface, the interface name + fully qualified method name as a string concatenation key value, may be a uniquely positioned MappedStatement, for example: com.mybatis3.mappers.StudentDao.findStudentById, can be found a unique namespace id = findStudentById com.mybatis3.mappers.StudentDao is below the MappedStatement. In Mybatis, each ,,, tag will be resolved to an MappedStatement object.

Dao in the interface method is not overloaded, because the policy is to preserve and to find the fully qualified name + method name.

Dao is the interface works JDK dynamic proxy will be used Mybatis running JDK dynamic proxy interface generation Dao proxy proxy object, the proxy object proxy intercepts interface methods in favor of the implementation of sql MappedStatement represented, then the sql execution results returned.

4, Mybatis how to page through? What is the principle pagination plug-in?

A: Mybatis RowBounds objects using paging, it is the memory paging ResultSet result set for execution, rather than physical pages, you can write directly with a physical paging parameters in the sql to complete the physical paging function can also be used to complete the pagination plug-in physical page.

The basic principle is to use the plug-in tab Mybatis plugin interface provided, to achieve a custom plug-ins, sql intercept interception to be performed within the insert method, and then rewrite the sql, according dialect dialect, add statements and physical page corresponding to the physical paging parameters.

For example: select * from overwriting the student, to intercept sql: select t * from (select * from student) t limit 0,10.

5, operating principles outlined Mybatis plug-ins, as well as how to write a plugin.

A: Mybatis only be written for ParameterHandler, ResultSetHandler, StatementHandler, Executor of these four plug-in interface, Mybatis use JDK dynamic proxy for the proxy object interface generation need to intercept interface methods to achieve blocking feature, whenever the implementation of the four interfaces when the method of the object, will enter the intercept method, the concrete is InvocationHandler invoke () method, of course, will only need to intercept those you specify interception.

Mybatis achieve the Interceptor interfaces and replication intercept () method, and then write notes to the plug-in, specify which interface to which methods to intercept, remember, do not forget to write your configuration in the configuration file plugin.

6, Mybatis perform bulk insert, can return the database primary key list it?

A: Yes, JDBC can, Mybatis certainly can.

7, Mybatis dynamic sql is doing what? What are dynamic sql? Can briefly principle of dynamic sql execution is not?

A: Mybatis dynamic sql can let us in Xml mapping file, write dynamic sql in the form of labels, complete logic and dynamic splicing sql functions, Mybatis offers nine dynamic sql tag trim | where | set | foreach | if | choose | when | otherwise | bind.

Principle of its execution, the value calculated from an expression using OGNL sql parameter object, according to the value of the dynamic splicing sql expression, in order to complete the dynamic sql function.

8, Mybatis is how to execute sql package is the result of the target object and returns? What are mapping form?

A: The first is to use labels, of defining the mapping between the name of the column names and object properties. The second function is to use an alias sql column, the column alias name is written as object properties, such as T_NAME AS NAME, General object properties are name, lowercase, but the column names are case insensitive, Mybatis ignores the column name case, smart finds the corresponding object property name, you can even write T_NAME aS naMe, as Mybatis can work properly.

Once you have a column mapping between names and attribute names, Mybatis creating objects through reflection, use reflection individually assigned to the properties of the object and returns, those properties can not find the mapping relationship, it is unable to complete the assignment.

9, Mybatis can perform relational query one to one, one to many of you? What are the implementation, as well as the differences between them.

A: Yes, Mybatis not only can perform one to one, one to many related query, you can also perform relational query many-to-many, and many-to-query, the query is actually one on one, just need to selectOne ( ) modified to selectList () can be; many to many inquiries, in fact, many queries, just need selectOne () modified to selectList () can be.

Query associated object, implemented in two ways, one is transmitting a single query to sql associated object, the object is assigned to the master, and then returns to the main object. Another is to use a nested query, the meaning is the use of nested query join query, a portion of the column A is the attribute value of the object, and part of the column is the attribute value associated with object B, a benefit is only made sql query, it can be primary object and its associated object is found out.

So the question is, join check out 100 records, how to determine the primary target is 5, instead of 100? Deduplication principle which is a child tag within the tag, is assigned a unique id column to determine a record, Mybatis accomplished deduplication function 100 records a column value, there may be a plurality, representing the primary key of the semantics.

Also associated with the main target object, according to this principle is to be repeated, although under normal circumstances, only the primary target will duplicate records, generally associated objects will not be repeated.

Example: The following join query records out of six, one, two Teacher is an object column, the third column as the Student object, after Mybatis deduplication process as a result of teacher student 6, instead of six teacher student 6 .

   t_id    t_name           s_id

| 1 | teacher | 38 |

| 1 | teacher | 39 |

| 1 | teacher | 40 |

| 1 | teacher | 41 |

| 1 | teacher | 42 |

| 1 | teacher | 43 |

10, Mybatis support lazy loading? If so, what is its principle is to achieve?

A: Mybatis association only supports lazy loading the associated object collection and a collection of associated objects, association refers to one, collection refers to the many queries. In Mybatis configuration file, you can configure whether to enable lazy loading lazyLoadingEnabled = true | false.

Its principle is to use CGLIB create a proxy object of the target object when the target method call, enter the interceptor methods, such as call a.getB (). GetName (), interceptor invoke () method found a.getB () is null value, then it will separate the saved queries sent in advance sql associated object B, the query up B, then call a.setB (b), then there is a target attribute value b, and then completed a.getB ( ) call .getName () method. This is the basic principle lazy loading.

Of course, not only Mybatis, including almost all of Hibernate, lazy loading support for the principle is the same.

11, Mybatis of Xml mapping file, different Xml mapping file, id if you can repeat?

A: Different Xml mapping file, if a configuration namespace, then the id can be repeated; if there is no namespace configured, id can not be repeated; after all namespace is not required, but the best practices it.

The reason is that namespace + id is as if there is no namespace, leaving id Map <String, MappedStatement> use the key, then, id will lead to duplicate data overwrite each other. With namespace, natural id can be repeated, different namespace, namespace + id naturally different.

12, Mybatis how to perform batch processing?

A: Use BatchExecutor complete batch.

13, Mybatis what are the Executor executor? What is the difference between them?

A: Mybatis There are three basic Executor executor, SimpleExecutor, ReuseExecutor, BatchExecutor.

SimpleExecutor: Each time update or select, just open a Statement object, run out immediately closed Statement object.

ReuseExecutor: execute update or select, as a key to find sql Statement object exists on the use, it does not exist to create, after use, do not close the Statement object, but placed in the Map, for the next use. In short, re-use Statement objects.

BatchExecutor: execute update (not select, JDBC does not support batch select), all sql are added to the batch (addBatch ()), waiting for the uniform implementation (executeBatch ()), it caches multiple Statement objects, each Statement objects are addBatch () after completion, waiting for execution one by one executeBatch () batch. And JDBC same batch.

Scope: Executor of these features are strictly limited in scope SqlSession life cycle.

14, how Mybatis specify which one to use Executor executor?

A: Mybatis configuration file, you can specify the default ExecutorType actuator type, you can manually create a method to transfer ExecutorType SqlSession of DefaultSqlSessionFactory type parameters.

15, Mybatis whether Enum can be mapped enumeration class?

A: Mybatis can map an enumeration class that not only can map enumerated classes, Mybatis can be mapped to any object on a table. A custom mapping mode TypeHandler, achieve setParameter TypeHandler () and the getResult () interface method. TypeHandler has two functions, one is to complete the javaType jdbcType conversion is complete two to jdbcType javaType conversion method reflects the setParameter two () and the getResult (), representing the placeholder parameter set sql question mark and obtain column search result.

16, Mybatis mapping file, if the A label by reference to the content of B include labels, may I ask, Can I define B label at the back of the label A, or that A must be defined in the previous label?

A: Although Mybatis parsing Xml map file is parsed in the order, however, B label is still referenced can be defined anywhere, Mybatis can be identified correctly.

The principle is, Mybatis A label parsing, references B found A label tag, but the tag has not been resolved to B, does not exist yet, at this time, labeled A-label will Mybatis unresolved, and parse the remainder of the label, containing B labels, all labels to be resolved is completed, Mybatis re-render those marked as unresolved tag, then re-parsing time label a, B tag already exists, it can normally resolve a label done.

17, the mapping between the brief Mybatis of Xml mapping file and Mybatis internal data structures?

A: Mybatis Xml all configuration information is encapsulated into All-In-One Configuration heavyweight objects inside. In Xml mapping file, the tag will be parsed as ParameterMap objects, each of its child elements are parsed as ParameterMapping object. ResultMap labels are parsed into objects, each sub-element will be parsed as ResultMapping object. Each ,,, tag will be parsed as MappedStatement objects, sql within the tag will be parsed as BoundSql object.

18, why Mybatis semi-automatic ORM mapping tools? Where it differs is in automatic?

Answer: Hibernate ORM is fully automatic mapping tools, the use of Hibernate query associated object or collection objects, can be directly obtained based on the object relational model, so it is fully automatic. And Mybatis when querying the associated object or collection of objects associated with the need to manually write sql to complete, so called semi-automatic ORM mapping tools.

Face questions may seem very simple, but want to be able to answer it correctly, it must be studied in depth and the source of people, not just people who use or familiar with, above all face questions and answers related to the content in my blog series Mybatis in principle have detailed explanations and analysis.

Published 78 original articles · won praise 9 · views 6188

Guess you like

Origin blog.csdn.net/WANXT1024/article/details/104402110