What is MyBatis?
MyBatis is an excellent persistence layer framework, a semi-ORM (object-relational mapping) framework, which supports custom SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manually setting parameters and getting result sets. MyBatis can use simple XML or annotations to configure and map native type interfaces and Java POIO (Plain Old Java Objects, plain old lava objects) as records in the database.
Two What is ORM
ORM (Object Relational Mapping), object-relational mapping, is a technology to solve the mapping relationship between relational database data and simple Java objects (POIO). Simply put, ORM automatically persists the objects in the program to the relational database by using the metadata describing the mapping between the object and the database.
Three Why is Mybatis a semi-automatic ORM mapping tool? What is the difference between it and fully automatic?
Hibernate is a fully automatic ORM mapping tool. When using Hibernate to query associated objects or associated collection objects, it can be directly obtained according to the object relationship model, so it is fully automatic.
When Mybatis queries associated objects or associated collection objects, it needs to manually write sq to complete, so it is called a semi-automatic ORM mapping tool.
Four problems in traditional JDBC development
-
Frequent creation and release of database connection objects can easily cause waste of system resources and affect system performance. This problem can be solved by using a connection pool. But using jdbc requires you to implement the connection pool yourself.
-
There are hard codes in sql statement definition and parameter setting result set processing. In the actual project, the sql statement is more likely to change. Once there is a change, the java code needs to be modified, and the system needs to be recompiled and republished. Not easy to maintain.
-
There is hard coding in using preparedstatement to pass parameters to the occupancy symbol, because the where condition of the sql statement is not necessarily certain, there may be more or less, and the code needs to be modified to modify the sql, so the system is not easy to maintain.
-
There are duplicate codes in the result set processing, which is troublesome. It would be more convenient if it can be mapped to a Java object.
5. What are the shortcomings of JDBC programming, and how does MyBatis solve these problems?
- Frequent creation and release of database links will cause waste of system resources and affect system performance. If you use database connection pool, you can solve this problem.
Solution: Configure the data link pool in mybatis-config.xml, and use the connection pool to manage database connections - The Sq statement is written in the code, which makes the code difficult to maintain. The actual application of sq may change a lot, and the change of sq| needs to change the java code.
Solution: Configure the Sq sentence in the ××××mappe.×ml file and separate it from the java code. - It is troublesome to pass parameters to the SQL statement, because the where condition of the SQL statement is not necessarily, there may be more or less, and the placeholders need to correspond to the parameters one by one.
Solution: Mybatis automatically maps java pairs to sql statements. - Parsing the result set is troublesome, the change of sql leads to the change of the parsing code, and the parsing needs to be experienced before parsing, if the database records can be encapsulated into pojo objects, parsing is more convenient.
Solution: Mybatis automatically maps sql execution results to java objects
Six Mybatis advantages and disadvantages
Advantages
Compared with traditional database access technology, ORM has the following advantages.
- Programming based on SQL statement. It is quite flexible and will not have any impact on the existing design of the application program or database. SQL is written in xml, which decouples sq| from program code and facilitates unified management; provides XML tags, supports writing dynamic SQL statements, and can be reused
- Compared with JDBC, it reduces the amount of code by more than 50%, eliminates a lot of redundant code of JDBC, and does not need to manually switch the connection
- Very good compatibility with various databases (because MyBatis uses JDBC to connect to the database, so as long as the database supported by JDBC is supported by MyBatis)
- Provide mapping tags to support ORM field relationship mapping between objects and databases; provide object-relational mapping tags to support object-relational component maintenance
- Can be well integrated with Spring
shortcoming
- The workload of writing SQL statements is relatively large, especially when there are many fields and tables, there are certain requirements for developers to write SQL statements
- SQL cannot change the database at will
Seven MyBatis Framework Applicable Scenarios
- MyBatis focuses on SQL itself and is a flexible enough DAO layer solution
- MyBatis will be a good choice for projects that have high performance requirements or require more changes, such as Internet projects.
What are the eight MyBatis programming steps?
Nine, please talk about the working principle of MyBatis
Ten What is the functional architecture of MyBatis?
The JDBC API allows users to access any form of tabular data, especially data stored in relational databases.
Eleven What is the framework design of MyBatis?
Twelve Why do you need precompilation
Database Management System (Database Management System)
Thirteen What Executors does Mybatis have? What is the difference between them?
Fourteen How to specify which Executor to use in Mybatis?
Fifteen Does Mybatis support lazy loading? If supported, how is it implemented?
The difference between #{} and ${}
17 How to write fuzzy query like statement
Eighteen How to pass multiple parameters in mapper
Nineteen Mybatis how to perform batch operations
20 How to get the generated primary key
21. What should I do when the attribute name in the entity class is different from the field name in the table?
22. What are the ways to write Mapper?
Twenty-three What is MyBatis interface binding? What implementations are there?
24. What are the requirements when using the mapper interface of MyBatis?
Twenty-five best practices, usually an Xml mapping file, will write a Dao interface corresponding to it, what is the working principle of this Dao interface? Can the method in the Dao interface be overloaded when the parameters are different?
Twenty-six In the Xml mapping file of Mybatis, can the id of different Xml mapping files be repeated?
Twenty-seven briefly describe the mapping relationship between Mybatis's Xml mapping file and Mybatis' internal data structure?
Twenty-eight How does Mybatis encapsulate the sql execution result as a target object and return it? What are the mapping forms?
Twenty-nine In the Xml mapping file, besides the common select|insert|updae|delete tags, what other tags are there?
Thirty In the Mybatis mapping file, if the A tag references 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?
31 MyBatis implements one-to-one and one-to-many in several ways, how to operate?
Thirty-two Can Mybatis map the Enum enumeration class?
Thirty-three What does Mybatis dynamic sql do? What dynamic sql are there? Can you briefly describe the execution principle of dynamic sql?
Thirty-four How does Mybatis paginate? What is the principle of the pagination plugin?
Thirty-five briefly describe the operating principle of Mybatis plug-ins and how to write a plug-in.
Thirty-six Mybatis's first and second level cache
crud refers to the increase (Create), read (Retrieve), update (Update) and delete (Delete) when doing calculation processing