2019 mybatils high-frequency interview questions (java)

Preface ago

2019 will soon be over, with us is about to usher in the new year, the Spring Festival, once again usher in a new interview gold and three silver four seasons. Then, as the program you apes, really be prepared for it, also, or content with their jobs, continue to do the things at hand.

Of course, no matter how you choose, if you're really ready to quit after gold and three silver four, then as a Java engineer, may not be read. How in a few months time, rapid adequately prepare it for the upcoming interview?

First, what is MyBatis?

A: MyBatis is a persistence framework can custom SQL, stored procedures and advanced mappings.

Second, speaking under cache MyBatis

A: MyBatis cache into cache and secondary cache, cache session on the inside, there is a default on its secondary cache namespace, the default is not open, use the secondary cache attribute class needs implements serializable serialization interface (used to save the state of the object), it may be disposed on mapping file

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

A:
1, Mybatis use RowBounds objects page, you can also write directly sql implement paging, you can also use the pagination plugin Mybatis.
2, the principle pagination plug-in: implementation of the interface Mybatis provided, implement a custom plug-in, intercept sql to be executed within the intercept method plug-ins, and then rewrite the sql.
For example: select * from overwriting the student, to intercept sql: select t * from (select * from student) t limit 0,10.

Fourth, the operating principle outlined Mybatis plug-ins, as well as how to write a plugin?

A:
1, Mybatis only can write plug-ins for ParameterHandler, ResultSetHandler, StatementHandler, Executor of these four interfaces, Mybatis through 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.
2, to achieve Mybatis 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.

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

A:
1, Mybatis dynamic sql can let us in Xml mapping file, written in the form of dynamic sql tag to complete the logic and dynamic sql stitching function.
2, Mybatis offers nine dynamic sql Tags: trim | where | set | foreach | if | choose | when | otherwise | bind.
3, the 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.

Sixth, what is the difference between # {} and {} $ is?

A:
1, # {} is a pre-compiler process,

不能识别此Latex公式:
{}是字符串替换。
2、Mybatis在处理#{}时,会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值;
3、Mybatis在处理
When {}, {} $ is to replace the value to a variable.
4, using # {} can effectively prevent SQL injection, improve system security.

 

Seven, 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.

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

A:
1, 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.
2, 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 the null value, it will send a separate well sql stored beforehand associated with the query object B, the query up B, then call a.setB (b), then there is a target attribute value b, and then to complete a. getB (). call getName () method. This is the basic principle lazy loading.

Nine, MyBatis with Hibernate What are the differences?

A:
1, different Mybatis and hibernate, it is not exactly an ORM framework, because MyBatis require programmers to write their own Sql statement, but mybatis can be flexibly configured to run sql statement by way of XML or annotation, and java objects and sql statement sql final map generation performed, the final results will then map generation performed sql java object.
2, Mybatis low learning curve, easy to learn, programmers write directly to the original ecology sql, sql execution performance can be strictly controlled, high flexibility, ideal for less demanding on the relational data model of software development, such as Internet software, enterprise class operating software etc., because such software requirements change frequently, but the needs of a rapid change in output required outcomes. But that flexibility can not be done on the premise that mybatis database-independent, supports multiple databases need to implement if the software you need to customize sets of sql mapping file, the heavy workload.
3, Hibernate object / relational mapping capabilities strong, independent of the database is good for high-relational model requires software (such as fixed demand customized software) If you can save a lot of code development with hibernate and improve efficiency. But the disadvantage of Hibernate is a high threshold to learn, to master a higher threshold, and how design O / R mapping, how to balance between performance and object model, and how to make good use of Hibernate need to have strong experience and ability to the job.
In short, according to the needs of users in resource-limited settings can be made as long as maintenance, scalability good software architecture is good architecture, it is only suitable framework is the best.

Ten, what good is MyBatis?

A:
1, MyBatis the sql statement separate from the Java source code out, placed in a separate XML file is written to the maintenance program has brought great convenience.
2, MyBatis encapsulates details of the underlying JDBC API calls, and can automatically convert the result into a set of Java Bean objects, greatly simplifies the programming of the Java database duplication.
3, because MyBatis require the programmer to write the sql statement, the database programmer can combine the characteristics of its flexible control of sql statement, thus enabling more efficient than automatic query orm frameworks such as Hibernate, can perform complex queries.

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

答:Mybatis将所有Xml配置信息都封装到All-In-One重量级对象Configuration内部。在Xml映射文件中,<parameterMap>标签会被解析为ParameterMap对象,其每个子元素会被解析为ParameterMapping对象。<resultMap>标签会被解析为ResultMap对象,其每个子元素会被解析为ResultMapping对象。每一个<select>、<insert>、<update>、<delete>标签均会被解析为MappedStatement对象,标签内的sql会被解析为BoundSql对象。

XII What is MyBatis interface binding, and what are the benefits?

A: An interface map is to define the interface in any MyBatis, and then the inside of the interface methods SQL statements and bind, we can directly call the interface method, compared to the original method SqlSession so we can provide a more flexible options and settings .

XIII interface is bound to realize there are several ways, namely how to achieve?

A: The interface binding implemented in two ways, one is through binding annotations, is to add @ Select @ Update which contains notes and other Sql statement in the above method to bind the interface, another is to write SQL by xml inside to bind, in this case, to specify the xml mapping file inside the namespace must be a full path name of the interface.

Fourth, under what circumstances bind with annotations, use xml binding under what circumstances?

A: When Sql statement is simple when using annotations bind; when more complex SQL statements when using xml binding, usually with xml binding more.

Fifteen, MyBatis There are several ways to achieve one? Specifically how to operate?

A: There are joint inquiry and nested query, the union query several tables joint inquiry, query only once, by configuring the association node configuration resultMap inside one class can be completed. Nested query is to check a table, according to the results of the foreign key table id inside, and then further to a table inside the data query, but also by association configuration, but another table through a select attribute query configuration.

Sixteen, Mybatis can perform relational query one to one, one to many of you? What are the implementation, and the difference 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.

XVII dynamic Sql MyBatis which is how the set? What syntax?

A: MyBatis inside the dynamic Sql is generally achieved through if the node is realized by OGNL syntax, but if you write a complete, must cooperate where, trim nodes, where a node is to determine the content that contains the node is inserted where, otherwise, do not insert , trim node is used to determine if the statement is dynamic and or or start, and then automatically or this or take down.

Eighteen, 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.

Nineteen, 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.

XX, when the field names in the entity class attribute name and the table is not the same, if the result of the query package to the specified pojo?

A:
1, by defining field names in the sql statement query alias.
2, one to one relationship by mapping entity class field names and attribute names.

XXI fuzzy query like how to write the statement?

A:
1, in java splicing wildcard, by assignment} # {
2 in splicing Sql statement wildcard (Sql cause unsafe injection)

XXII, usually a Xml mapping file, will write a corresponding interface to Dao, Dao's works, can overload?

A: You can not reload, because looking Xml corresponding sql by Dao when the fully qualified name + method name of saving and finding strategies. Interface works for the jdk dynamic proxy principle, generates runtime dao proxy, the proxy object will intercept interface methods, to perform the corresponding sql return data.

Twenty-three, 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.

Twenty-four, 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 used as a key Map, if no namespace, leaving id, then, id will lead to duplicate data overwrite each other. With namespace, natural id can be repeated, different namespace, namespace + id naturally different.

XXV, how Mybatis perform batch?

A: Use BatchExecutor complete batch.

Twenty-six, Mybatis what are the Executor executor? What is the difference between them?

A: Mybatis There are three basic Executor executor, SimpleExecutor, ReuseExecutor, BatchExecutor. 1) SimpleExecutor: Each time update or select, just open a Statement object, run out immediately closed Statement object. 2) 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 Map3) BatchExecutor: complete batch.

Xxvii, 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.

Twenty-eight, Mybatis perform bulk insert, you can return the database primary key list it?

A: Yes, JDBC can, Mybatis certainly can.

Twenty-nine, 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.

Thirty, how to get the automatically generated (master) key?

A: The configuration file is set to true usegeneratedkeys

Xxxi, how to pass multiple parameters in a mapper in?

A:
1, passed directly in the process parameters, xml file {0} # {# 1} to obtain
2, using @param Note: this may be by direct xml file name # {} to obtain

XXXII, the difference resultType resultMap?

A:
1, the class name and the same database, the parameter may be provided directly to resultType Pojo type
2, if different, the results need to set resultMap name and first name conversion Pojo

33 or what is required when using the MyBatis mapper interface call?

A:
1, each of id Mapper sql names and interface methods defined in the same mapper.xml
2, the same type parameterType sql each input parameter type and mapper.xml Mapper interface methods defined in
3, Mapper interface method sql same type resultType each type and output parameters defined mapper.xml
4, Mapper.xml file namespace i.e. classpath mapper interface.

Thirty - four, larger than what Mybatis IBatis several improvement?

A:
1, has an interface bindings, including binding annotations and xml binding sql Sql
2, node configuration from the original dynamic sql become OGNL Expression 3) in one to one, one to many, when the introduction of the association, in when introducing a collection of many nodes, but inside are arranged in resultMap

Thirty-five, IBatis and what MyBatis respectively in the core processing class?

A: IBatis inside the core processing class pay SqlMapClient, MyBatis inside the core processing class called SqlSession.

Thirty-six, IBatis and MyBatis differ in the details of what?

A:
1, in which the variable name sql have the original variable # # # {variable} becomes
2, the original variables into a $ {variable}
3, in the original node inside sql class names have changed pay type
. 4, the original queryForObject queryForList become selectOne selectList5) in the original alias mapping file placed inside the core configuration file.

 

Public concern ape Society program number , reply the keyword " interview " to get face questions zk, dubbo, springboot, springcloud, mybatils, apollo and other technologies.

 

No public: program ape Society

Real-time updates weekly

Guess you like

Origin www.cnblogs.com/myworked/p/12122853.html