Mybatis Questions and Answers

Mybatis advantages and disadvantages of
the advantages

Compared with traditional database access technology, ORM has the following advantages:

Based on SQL statement programming, it is quite flexible and will not have any impact on the existing design of the application or database. SQL is written in XML, which releases the coupling between sql and program code and facilitates unified management; XML tags are provided to support the writing of dynamic SQL statements , And reusable.
Compared with JDBC, it reduces the amount of code by more than 50%, eliminates a large amount of redundant code in JDBC, and does not need to manually switch the connection. It is compatible 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, support object-to-database ORM field relationship mapping; provide object-relational mapping tags, support object-relational component maintenance,
can be well integrated with Spring
Disadvantages
The workload of writing SQL statements is large , Especially when there are many fields and many associated tables, there are certain requirements for developers to write SQL statements

SQL statements depend on the database, resulting in poor database portability, and the database cannot be replaced at will

The difference between Hibernate and MyBatis and the
same points
are the encapsulation of jdbc, both are the framework of the persistence layer, and both are used for the development of the dao layer.
Differences
Mapping relationship
MyBatis is a semi-automatic mapping framework that configures the correspondence between Java objects and the execution results of sql statements, and the configuration of multi-table association relationships is simple.
Hibernate is a full-table mapping framework that configures the correspondence between Java objects and database tables. Table association relationship configuration complex
SQL optimization and portability
Hibernate encapsulates SQL statements, provides log, cache, cascade (cascade is more powerful than MyBatis) and other features, and also provides HQL (Hibernate Query Language) operation database, database independence support Good, but it will consume more performance. If the project needs to support multiple databases, the amount of code development is small, but SQL statement optimization is difficult.
MyBatis needs to manually write SQL, support dynamic SQL, process lists, dynamically generate table names, and support stored procedures. The development workload is relatively large. Directly use SQL statements to operate the database, does not support database independence, but sql statement optimization is easy.
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 (POJO). Simply put, ORM automatically persists the objects in the program to the relational database by using metadata describing the mapping between the object and the database.
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 Hibernate is used to query related objects or related collection objects, it can be obtained directly according to the object-relational model, so it is fully automated.
When Mybatis queries related objects or related collection objects, you need to manually write sql to complete, so it is called a semi-automatic
ORM mapping tool.

Guess you like

Origin blog.csdn.net/qq_42918433/article/details/113913923