Where is the advantage compared to MyBatis and hibernate?

Source: Internet
Author: User

1, development and comparison of development speed

Hibernate's real mastery is more difficult than mybatis. The MyBatis framework is relatively simple and easy to get started with, but also relatively rudimentary. Personally think to use good mybatis or first to understand good hibernate first.

Development Community

Hibernate and MyBatis are popular persistent layer development frameworks, but the hibernate development community is relatively lively, supported by a number of tools, updated quickly, the current highest version of 4.1.8. The mybatis is relatively calm, with fewer tools and currently the highest version of 3.2.

Development effort

Both hibernate and MyBatis have corresponding code generation tools. A simple basic DAO layer method can be generated.

For advanced queries, MyBatis needs to write SQL statements manually, as well as Resultmap. Hibernate has a good mapping mechanism, and developers don't have to worry about SQL generation and result mapping, and can focus more on business processes.

2, System tuning contrast Hibernate's tuning scheme

Develop a reasonable caching strategy;
Use lazy loading features as much as possible;
The use of reasonable session management mechanism;
Use batch fetch, set reasonable batch processing parameters (Batch_size);
Make a reasonable design of O/R mapping
MyBatis Tuning Solution

MyBatis in session and Hibernate session life cycle is consistent, also need a reasonable session management mechanism. The MyBatis also has a two-level caching mechanism. MyBatis can perform detailed SQL optimization design.

SQL optimization aspects

Hibernate queries query all the fields in the table, which can be a performance drain. Hibernate can also write its own SQL to specify the fields that need to be queried, but this undermines the simplicity of hibernate development. The MyBatis SQL is written manually, so you can specify the fields of the query on demand.

The tuning of Hibernate HQL statements requires that SQL be printed out, and Hibernate's SQL is too ugly for many people to dislike. MyBatis's SQL is written manually, so it's easy to adjust. However, hibernate has its own log statistics. The mybatis itself does not have log statistics and uses log4j to log records.

Extensibility aspects

Hibernate's association with a specific database can only be configured in an XML file, and all HQL statements are not relevant to the database in use and are well-ported. All SQL statements in the MyBatis project are dependent on the database used, so support for different database types is not good.

3. Object management and crawl policy object management

Hibernate is a complete object/relational mapping solution that provides the functionality of object state Management (management), which eliminates the need for developers to heed the details of the underlying database system. That is, hibernate uses a more natural object-oriented perspective to persist data in Java applications relative to the common Jdbc/sql persistence layer scenarios in which SQL statements need to be managed.

In other words, developers using Hibernate should always focus on the state of the object, regardless of the execution of the SQL statement. This part of the details has been managed by Hibernate and only needs to be understood by the developer when it comes to tuning the system performance.

And MyBatis in this one. There is no document stating that the user needs to manage the object in detail.

Crawl strategy

Hibernate has a good mechanism for capturing entity-related objects. For each correlation relationship can be set in detail whether delay loading, and provide related crawl, query fetch, subquery fetch, batch crawl four modes. It is configured and processed in detail.

The deferred load for MyBatis is globally configured.

4. Cache mechanism vs. hibernate cache

Hibernate buffer is the session cache, the use of a good first-level cache will need to manage the session life cycle. We recommend that you use a session in an action action. First-level caching requires strict management of the session.

Hibernate level Two caches are sessionfactory-level caches. The sessionfactory cache is divided into built-in caches and external caches. The built-in cache holds the data contained in some of the collection properties of the Sessionfactory object (mapping elements and predetermined SQL statements, etc.), which is read-only for the application. The external cache holds a copy of the database data, which acts like a first-level cache. Level two cache in addition to memory as a storage medium, you can also choose the hard disk and other external storage devices. A secondary cache, called a process-level cache or a sessionfactory-level cache, can be shared by all sessions, and its life cycle is accompanied by the existence and extinction of the sessionfactory life cycle.

5. Comparative advantage

MyBatis Advantages

MyBatis can perform more granular SQL optimizations and can reduce query fields.
The MyBatis is easy to master, while the hibernate threshold is higher.
Hibernate Advantage

Hibernate's DAO layer development is simpler than mybatis, and mybatis needs to maintain SQL and result mappings.
Hibernate to the object maintenance and caching is better than MyBatis, to delete and change the object of the maintenance to be convenient.
Hibernate database portability is very good, MyBatis database portability is bad, different databases need to write different SQL.
Hibernate has a better level two caching mechanism and can use third-party caches. The caching mechanism provided by the MyBatis itself is poor.

What are the advantages of

MyBatis versus hibernate?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.