In 2020, MyBatis common interview questions summary

Mybatis technology Insider blog series, from the perspective of the principle and source code, introduced its internal implementation details, whether it is good or bad writing, I really carefully written, because the article describes how to use Mybatis not, therefore, some of the parameters used details a little off, our goal is to introduce Mybatis technical architecture and an important part, as well as basic operating principles.

Blog writing is very hard, but not necessarily to write good-looking, called start very excited, very painful process to end unfortunately. Ask for much, as long as the reader from the family blog, learn to technical points that other blog that are not, as the author, I am very pleased, and I read the blog written by someone else, usually on their current research technology, is very helpful.

Although there are a lot of content can be written, but I think the write down has no meaning, any other small function point, are run under the basic framework and basic principles have been introduced, and only end in order to have a new beginning . Write blog also accumulated some experience, source code is feeling more copy paste, source less talk and think it is the principle of the future write blog, I hope, "Bowen refining", easy to read and easy to understand and beautiful to read not tired, longer I want to write a series of blog open source distributed framework principles.

The courage to come out of my face Mybatis few questions, see if you can answer it a few (all out of me, and not find online).

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

Note: This question is the interviewer interviewing my colleagues.

answer:

Properties file $ {} is the 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?

Note: This question was asked in the interview I Jingdong interviewer.

A: There are many other tags, <resultMap>, <parameterMap>, <sql>, <include>, <selectKey>, plus nine dynamic sql tag, trim | where | set | foreach | if | choose | when | otherwise | bind the like, wherein the tag is a sql fragments, fragments introduced sql <include> tags, <selectKey> does not support the primary key generation strategy increment of labels.

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?

Note: This question is asked in the interview I Jingdong interviewer.

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 <select>, <insert>, <update>, <delete> tag is resolved to a 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?

Note: I'm out.

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 student, intercepting sql rewritten as: 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.

Note: I'm out.

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?

Note: I'm out.

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?

Note: I'm out.

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?

Note: I'm out.

A: The first is to use <resultMap> tag, 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.

Note: I'm out.

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? The principle is that deduplication <resultMap> tag in <id> child tag, is assigned a unique id column to determine a record, Mybatis accomplished deduplication function 100 records a column value, <id> can have multiple, It represents the semantic joint primary key.

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?

Note: I'm out.

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?

Note: I'm out.

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, how Mybatis perform batch?

Note: I'm out.

A: Use BatchExecutor complete batch.

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

Note: I'm out

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`: ** perform 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 Map <String, Statement> inside, 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 the more Statement objects, each Statement object is 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?

Note: I'm out

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?

Note: I'm out

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 it 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?

Note: I'm out

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?

Note: I'm out

A: Mybatis Xml all configuration information is encapsulated into All-In-One Configuration heavyweight objects inside. In Xml mapping file, <parameterMap> tag will be resolved to ParameterMap objects, each sub-element will be parsed as ParameterMapping object. <ResultMap> tag will be resolved to ResultMap objects, each sub-element will be parsed as ResultMapping object. Each <select>, <insert>, <update>, <delete> tag will be resolved to MappedStatement objects, sql within the tag BoundSql are parsed into objects.

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

Note: I'm out

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.

 



Material from the network , if infringement please contact the author deleted

Guess you like

Origin www.cnblogs.com/QQ12538552/p/12335988.html