[Mybatis] features and advantages of mybatis

What is MyBatis

First of all, what mybatisare we talking about:

MyBatis is an excellent persistence layer framework, one and a half ORM (Object Relational Mapping)Framework, which supports customized
SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manual setting of parameters
and obtaining result sets. MyBatis can use simple XML or annotations to configure and map native types, interfaces and
Java POJOs (Plain Old Java Objects) as records in the database.

What is ORM

In the introduction mybatis, ORM was mentioned, then what is
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 uses
metadata describing the mapping between the object and the database to automatically persist the objects in the program to the relational database.

Why is Mybatis a semi-automatic ORM mapping tool

HibernateIt is a fully automatic ORM mapping tool. When using Hibernate to query related objects or related collection objects
, you canObtain directly according to the object relational model, So it is fully automatic.
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.

For traditional JDBC, what are the advantages of MyBatis

Problems in traditional JDBC development

  • Frequent creation and release of database connection objects can easily cause waste of system resources and affect system performance. You can use connection pooling to solve this problem. But to use jdbc, you need to implement the connection pool yourself.
  • There are hard-coded sql statement definitions, parameter settings, and result set processing. In the actual project, the possibility of sql statement changes is greater.
    Once a change occurs, the java code needs to be modified, and the system needs to be recompiled and republished. Not easy to maintain.
  • Using preparedStatement to pass parameters to occupied position symbols is hard-coded, because the where conditions of the SQL statement are not
    certain, and may be more or less. Modifying SQL requires modifying the code, and the system is not easy to maintain.
  • There are duplicate codes in the result set processing, which is troublesome. It will be more convenient if it can be mapped to a Java object.

MyBatis solves the shortcomings of JDBC programming

1. Frequent database link creation and release cause waste of system resources and affect system performance. If you use a 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.

2. Sql statement written in the code makes the code difficult to maintain, and the actual application of sql may change greatly, and the change of sql needs to
change the java code.

Solution: Separate the Sql statement configuration from the java code in the XXXXmapper.xml file.

3. 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 objects to sql statements.

4. It is troublesome to parse the result set. SQL changes lead to changes in the parsing code, and it needs to be traversed before
parsing. It is more convenient to encapsulate database records into pojo objects for parsing.

Solution: Mybatis automatically maps sql execution results to java objects.

Pros and cons of MyBatis

advantage

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

  • Programming based on SQL statements is quite flexible and will not have any impact on the existing design of applications or databases.
  • SQL is written in XML, decoupling sql and program code, facilitating unified management.
  • Provides XML tags, supports the writing of dynamic SQL statements, and can be reused. 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 require manual switch connections. It is compatible with various databases ( Because MyBatis uses JDBC to connect to the database, so long as the database supported by JDBC is supported by MyBatis)

two:

  • Provides mapping tags to support the mapping of ORM fields between objects and databases.
  • Provide object-relational mapping tags, support object-relational component maintenance, and integrate well with Spring

Disadvantage

  • The workload of writing SQL statements is relatively large, especially when there are many fields and 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.

MyBatis framework applicable scenarios

MyBatis focuses on SQL itself and is a sufficiently flexible DAO layer solution.
For projects that have high performance requirements or changes in demand, such as Internet projects, MyBatis will be a good choice.

The difference between Hibernate and MyBatis

Same point

Both are the encapsulation of jdbc, the framework of the persistence layer, and the development of the dao layer.

difference

Mapping relations

  • 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, which configures the correspondence between Java objects and database tables, and the configuration of multi-table associations is
    complicated

SQL optimization and portability

  • Hibernate encapsulates SQL statements and provides features such as logging, caching, and cascading (cascading is more powerful than MyBatis). In addition, it also provides HQL (Hibernate Query Language) to operate the database, which has good database independence, 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.

Development difficulty and learning cost

  • Hibernate is a heavyweight framework with a high barrier to learning and use. It is suitable for small and medium-sized projects with relatively stable needs, such as
    office automation systems.
  • MyBatis is a lightweight framework with a low learning threshold and is suitable for large-scale projects with frequent changes in demand, such as
    Internet e-commerce systems.

end

To sum up,
MyBatis is a small , convenient , efficient , simple , direct and semi-automatic persistence layer framework.
Hibernate is a powerful , convenient , efficient , complex , indirect , and fully automated persistence layer framework.

Guess you like

Origin blog.csdn.net/Black_Customer/article/details/107403819