What are the advantages of MyBatis compared to Hibernate?

Author: Ulala
Link : https://www.zhihu.com/question/21104468/answer/58579295
Source: Zhihu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

1. Development vs. development speed

The real master of Hibernate is harder than Mybatis. The Mybatis framework is relatively simple and easy to use, but it is also relatively simple. Personally think that to use Mybatis well, you must first understand Hibernate.

development community

Both Hibernate and Mybatis are popular persistence layer development frameworks, but the Hibernate development community is relatively more lively, supports many tools, and updates quickly. The current highest version is 4.1.8. Mybatis is relatively calm, with fewer tools, and the current highest version is 3.2.

development effort

Both Hibernate and MyBatis have corresponding code generation tools. Simple and basic DAO layer methods can be generated.

For advanced queries, Mybatis needs to manually write SQL statements and ResultMap. Hibernate has a good mapping mechanism, and developers do not need to care about SQL generation and result mapping, and can focus more on business processes.

2. System tuning compared to Hibernate's tuning scheme
  1. Develop a reasonable caching strategy;
  2. Try to use the lazy loading feature;
  3. Adopt a reasonable Session management mechanism;
  4. Use batch fetching and set reasonable batch parameters (batch_size);
  5. Make a reasonable O/R mapping design
Mybatis Tuning Solution

MyBatis is consistent with Hibernate's Session life cycle in terms of Session, and also requires a reasonable Session management mechanism. MyBatis also has a secondary cache mechanism. MyBatis can perform detailed SQL optimization design.

SQL optimization aspects

Hibernate's query will query all fields in the table, which will cost performance. Hibernate can also write its own SQL to specify the fields to be queried, but this destroys the simplicity of Hibernate development. The SQL of Mybatis is written manually, so the fields of the query can be specified as required.

The tuning of Hibernate HQL statements needs to print out the SQL, and Hibernate's SQL is disliked by many people because it is too ugly. MyBatis's SQL is written manually, so it is easy to adjust. But Hibernate has its own log statistics. Mybatis itself does not have log statistics and uses Log4j for logging.

Extensibility

The association between Hibernate and the specific database only needs to be configured in the XML file. All HQL statements have nothing to do with the specific database used, and the portability is very good. All SQL statements in the MyBatis project depend on the database used, so the support of different database types is not good.

3. Object management and grabbing strategy object management

Hibernate is a complete object/relational mapping solution, which provides object state management (state management) functions, so that developers no longer need to worry about the details of the underlying database system. That is to say, compared with the common JDBC/SQL persistence layer scheme that needs to manage SQL statements, Hibernate adopts a more natural object-oriented perspective to persist data in Java applications.

In other words, developers using Hibernate should always focus on the state of the object, not the execution of the SQL statement. This part of the details has been taken care of by Hibernate, and only developers need to understand when tuning system performance.

However, MyBatis has no documentation in this section, and users need to manage the objects themselves in detail.

Crawl strategy

Hibernate has a good mechanism for fetching entity-related objects. For each association relationship, you can set whether to delay loading in detail, and provide four modes: association crawling, query crawling, sub-query crawling, and batch crawling. It is configured and handled in detail.

The lazy loading of Mybatis is configured globally.

4. Cache mechanism compared to Hibernate cache

Hibernate's first-level cache is a session cache. To make good use of the first-level cache, it is necessary to manage the life cycle of the session. It is recommended to use a Session in an Action operation. The first level cache needs to strictly manage the session.

The Hibernate second-level cache is a SessionFactory-level cache. SessionFactory's cache is divided into built-in cache and external cache. The built-in cache stores the data (mapping element data and predetermined SQL statements, etc.) contained in some collection properties of the SessionFactory object, which is read-only for the application. The external cache stores a copy of the database data, and its function is similar to that of the first-level cache. In addition to using the memory as the storage medium, the second-level cache can also use external storage devices such as hard disks. The second-level cache is called a process-level cache or a SessionFactory-level cache, which can be shared by all sessions, and its life cycle exists and dies with the life cycle of the SessionFactory.

5. Comparison of advantages

Mybatis advantage
  • MyBatis can perform more detailed SQL optimization and reduce query fields.
  • MyBatis is easy to master, while Hibernate has a higher threshold.
Hibernate advantage
  • Hibernate's DAO layer development is simpler than MyBatis, which needs to maintain SQL and result mapping.
  • Hibernate's maintenance and caching of objects is better than MyBatis, and it is more convenient to maintain objects that are added, deleted, and checked.
  • Hibernate database portability is very good, MyBatis database portability is not good, different databases need to write different SQL.
  • Hibernate has a better second-level cache mechanism and can use third-party caches. The caching mechanism provided by MyBatis itself is not good

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326166769&siteId=291194637