Mybatis の common interview questions

Mybatis - Interview Questions


Recent ready to learn about the system Mybatis, only cursory read before the next election ten common interview questions

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

#{}是预编译处理,${}是字符串替换。
Mybatis when processing # {}, will be in sql number # {} with the call set assignment method of a PreparedStatement;?
Mybatis during processing, {} is to replace the value to a variable.
Use # {} can effectively prevent SQL injection, improve system security.

###### 2.Dao What is the working principle of the interface? Dao interfaces in the methods, parameters are different, the method can reload it?

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 passed to sql parameters. 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 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.

3. Mybatis How is paging? What is the principle pagination plug-in?

Mybatis RowBounds objects using paging, which is set for paging ResultSet execution result, rather than the physical page can be directly written with the parameters of the physical pages in the physical page is accomplished sql function may be used to complete the physical page tab widget .
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.

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

The first is to use 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.

5. Xml mapping file, in addition to the common select | insert | update | delete label outside, what label?

Note: This question comes from Jingdong interviewer.
There are many other labels, plus nine dynamic sql tag, trim | where | set | foreach | if | choose | when | otherwise | bind and so on, which is the sql fragment tag, label introduced by sql fragment, is not supported auto-increment primary key generation strategy labels.

6. The operating principle outlined Mybatis plug-ins, as well as how to write a plugin

Mybatis can only write for ParameterHandler, ResultSetHandler, StatementHandler, Executor of the four plug-in interface, Mybatis use JDK dynamic proxy to intercept function intercept interface generation needs to implement the interface proxy object method, whenever the implementation of the four interface objects when the method, will enter the intercept method, the concrete is InvocationHandler invoke () method, of course, you 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 that you also need to configure plug-ins written in the configuration file.

7. The L1 and L2 caches

1) a cache: a HashMap based PerpetualCache local cache, which stores a scope of Session, Session after the flush or close, all of the Cache will be cleared in the Session.
2) a secondary cache and cache the same mechanism, but also using default PerpetualCache, HashMap stored, it is stored in a different scope as Mapper (Namespace), and may be custom storage source, such as Ehcache. To enable a second level cache, you need to add one line to your SQL Mapping file:
3) For data cache update mechanism, when performing the C / U / D operation of a certain scope (a cache Session / secondary cache the Namespaces) after default select the scope of all the cache will be clear.

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

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.

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

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.

10. Description of the mapping relationship between the Mybatis Mybatis Xml mapping file and internal data structure?

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

Reference: https://zhuanlan.zhihu.com/p/61432692

Guess you like

Origin www.cnblogs.com/QuixoteY/p/10926640.html