In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The purpose of this article is to share with you what are the contents of the test questions that you often meet with MyBatis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. What is Mybatis?
Mybatis is a semi-ORM (object-relational mapping) framework, which encapsulates JDBC internally. When developing, you only need to pay attention to the SQL statement itself, and you don't need to spend energy to deal with the complicated process of loading driver, creating connection, creating statement and so on. Programmers directly write the original sql, can strictly control the performance of sql implementation, high flexibility.
MyBatis can use XML or annotations to configure and map native information, mapping POJO to records in the database, avoiding almost all JDBC code and manually setting parameters and getting result sets.
All kinds of statement to be executed are configured by xml file or annotation, and the final executed sql statement is generated by mapping the java object and the dynamic parameters of sql in statement. Finally, the mybatis framework executes sql and maps the result to java object and returns. (the process from executing sql to returning result).
2. What's the difference between # {} and ${}?
The correct answer is: # {} is a precompilation process, and ${} is a string replacement.
(1) when mybatis processes # {}, it will replace # {} in sql with? Number, call the set method of PreparedStatement to assign the value.
(2) when mybatis deals with ${}, it replaces ${} with the value of the variable.
(3) using # {} can effectively prevent SQL injection and improve the security of the system. The reason is: the precompilation mechanism. After precompilation, the structure of SQL has been fixed, even if users enter illegal parameters, it will not affect the structure of SQL, thus avoiding potential security risks.
(4) precompilation is to precompile the SQL statement in advance, and the parameters injected after that will not be compiled by SQL. We know that SQL injection occurs during compilation because some special characters are maliciously injected and eventually compiled into malicious operations. On the other hand, the precompilation mechanism can prevent SQL injection.
Finally, I would like to add one point:
The $symbol is generally used as a placeholder, and people who often use Linux scripts should have a better understanding of this. For example, $1 and so on represent placeholders for input parameters. Knowing this makes it easy to distinguish between $and #, so it's not easy to get it wrong.
3. How is Mybatis paginated? What is the principle of the paging plug-in?
Mybatis uses the RowBounds object for paging, which is memory paging for ResultSet result sets rather than physical paging. You can write parameters with physical paging directly within sql to complete physical paging, or you can use paging plug-ins to complete physical paging.
The basic principle of the paging plug-in is to use the plug-in interface provided by Mybatis to implement the 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 statements and physical paging parameters according to the dialect dialect.
4. What are the differences between MyBatis and Hibernate?
Unlike hibernate, Mybatis is not exactly an ORM framework, because MyBatis requires programmers to write their own Sql statements.
Mybatis directly writes the original sql, which can strictly control the execution performance of sql and has high flexibility. It is very suitable for software development that does not require high relational data models, because the requirements of this kind of software change frequently, but the demand changes require rapid output results. But the premise of flexibility is that mybatis can not achieve database independence. If you need to implement software that supports multiple databases, you need to customize multiple sets of sql mapping files, which is a heavy workload.
Hibernate has strong object / relational mapping ability and good database independence. For the software with high requirements of relational model, if you develop it with hibernate, you can save a lot of code and improve efficiency.
5. In the Xml mapping file of Mybatis, can id repeat different Xml mapping files?
For different Xml mapping files, id can be repeated if namespace is configured, and id cannot be repeated if namespace is not configured.
The reason is that namespace+id is used as the key of Map, if there is no namespace, only id is left, then id repetition will cause the data to overwrite each other. With namespace, the natural id can be repeated. If the namespace is different, the namespace+id is different.
6. How does Mybatis encapsulate the sql execution result as a target object and return it? What are the forms of mapping?
The first is to use tags to define the mapping 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 an object property name, such as T_NAME AS NAME, the object property name is generally name, lowercase, but the column name is not case-sensitive, Mybatis will ignore the column name case, intelligently find the corresponding object property name, you can even write as T_NAME AS NaMe,Mybatis can work normally.
After having the mapping relationship between column name and attribute name, Mybatis creates the object through reflection, and uses the properties reflected to the object to assign values one by one and return them. Those attributes that can not find the mapping relationship cannot complete the assignment.
7. In addition to the common | tags in the Xml mapping file, what other tags are there?
Note: this question is from the interviewer east of Beijing.
There are many other tags, plus nine tags of dynamic sql, trim | where | set | foreach | if | choose | when | otherwise | bind, etc., in which sql fragment tags are introduced into sql fragments through tags to generate policy tags for primary keys that do not support self-increment.
8. Briefly describe the plug-in operation principle of Mybatis, and how to write a plug-in.
Mybatis can only write plug-ins for ParameterHandler, ResultSetHandler, StatementHandler, and Executor interfaces. Mybatis uses JDK's dynamic proxy to generate proxy objects for interfaces that need to be intercepted to achieve the interface method interception function. Whenever the methods of these four interface objects are executed, they will enter the intercept method, specifically the invoke () method of InvocationHandler. Of course, it will only intercept those methods that you specify need to be intercepted.
Implement the Interceptor interface of Mybatis and override the intercept () method, and then annotate the plug-in, specifying which interface and which methods to intercept. Remember, you also need to configure the plug-in you wrote in the configuration file.
9. Primary and secondary caching
1) first-level cache: PerpetualCache-based HashMap local cache, whose storage scope is Session. After Session flush or close, all Cache in the Session will be emptied.
2) the mechanism of the second-level cache is the same as that of the first-level cache, except that the storage scope is Mapper (Namespace) and the storage source can be customized, such as Ehcache. To turn on secondary caching, you need to add a line to your SQL mapping file:
3) for the cache data update mechanism, when the C/U/D operation is performed in a certain scope (first-level cache Session/-second-level cache Namespaces), all caches in the scope will be clear by default.
10. Why is Mybatis a semi-automatic ORM mapping tool? What's the difference between it and full automation?
Hibernate is a fully automatic ORM mapping tool. When using Hibernate to query associated objects or associated collection objects, it can be obtained directly according to the object relational model, so it is fully automatic. When querying associated objects or associated collection objects, Mybatis needs to write sql manually, so it is called semi-automatic ORM mapping tool.
11. Does Mybatis support delayed loading? If so, what is the principle of its implementation?
Mybatis only supports delayed loading of association associated objects and collection associated collection objects. Association refers to one-to-one and collection refers to one-to-many queries. In the Mybatis configuration file, you can configure whether to enable deferred loading of lazyLoadingEnabled=true | false.
Its principle is to use CGLIB to create a proxy object of the target object, and when the target method is called, enter the interceptor method, such as calling a.getB (). GetName (), the interceptor invoke () method finds that a.getB () is a null value, then the sql of the pre-saved query associated B object is sent separately, the B query is uploaded, and then a.setB (b) is called, so that the object b property of a has a value. Then complete the call to the a.getB () .getName () method. This is the basic principle of delayed loading.
12. 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 in front of the A tag?
Although Mybatis parses the Xml mapping file sequentially, the referenced B tag can still be defined anywhere, and Mybatis can correctly identify it.
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 yet been parsed and does not yet exist. At this point, Mybatis marks the A tag as unresolved, and then continues to parse the remaining tags, including the B tag. When all the tags are parsed, Mybatis will re-parse those tags that are marked as unresolved. When the A tag is parsed again, the B tag already exists. The A tag can be parsed normally.
13. Briefly describe the mapping relationship between Mybatis's Xml mapping file and Mybatis internal data structure.
Mybatis encapsulates all Xml configuration information inside the All-In-One heavyweight object Configuration. In the Xml mapping file, the tag is resolved to a ParameterMap object, and each of its child elements is resolved to a ParameterMapping object. The tag is resolved to a ResultMap object, and each of its child elements is resolved to a ResultMapping object.
Each, tag will be resolved to a MappedStatement object, and the sql within the tag will be resolved to a BoundSql object.
14. In addition to the common | | updae | tags in the Xml mapping file, what other tags are there?
Answer:, add 9 tags of dynamic sql, among which are sql fragment tags. Introduce sql fragments through tags to generate policy tags for primary keys that do not support self-increment.
15. What are the advantages of Mybaits?
(1) programming based on SQL statements, quite flexible, will not cause any impact on the existing design of the application or database, SQL is written in XML, uncoupling sql and program code, facilitate unified management; provide XML tags, support for the preparation of dynamic SQL statements, and can be reused.
(2) compared with JDBC, it reduces the amount of code by more than 50%, eliminates a lot of redundant code in JDBC, and does not need to switch and connect manually.
(3) good compatibility with various databases (because MyBatis uses JDBC to connect to the database, so as long as the database MyBatis supported by JDBC supports it).
(4) be able to integrate with Spring
(5) provide mapping label to support ORM field mapping between object and database, and provide object-relational mapping label to support the maintenance of object-relational components.
Thank you for reading! This is the end of this article on "what are the test questions we often meet with MyBatis?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.