A detailed comparison of the "persistence framework" MyBatis with Hibernate

Source: Internet
Author: User

Objective

This blog post we focus on the difference between Mybatis and hibernate , of course, in the previous blog post we have thoroughly studied the principles of Mybatis and hibernate.

Mybatis

    1. Introduction and principle of "persistence framework" MyBatis
    2. "Persistence framework" SPRINGMVC+SPRING4+MYBATIS3 integration, development of simple Web project + source download

Hibernate

    1. "SSH Advanced Path" Hibernate fundamentals (i)
    2. "SSH Advanced path" hibernate build development environment + Simple example (ii)
    3. "SSH Advanced Path" Hibernate basic mapping (iii)
    4. "SSH Advanced path" hibernate mapping--multiple-to-one one-way correlation mapping (iv)
    5. "SSH Advanced path" hibernate mapping--one-to-one unidirectional correlation mapping (v)
    6. "SSH Advanced path" hibernate mapping--one-to-one bidirectional correlation mapping (vi)
    7. "SSH Advanced path" hibernate mapping--one-to-many association mappings (vii)
    8. "SSH Advanced path" hibernate mapping--many-to-many association mappings (eight)
    9. "SSH Advanced Road" Hibernate series--Summary (ix)

As a good programmer, it's not enough to know just one ORM framework. Before developing a project, the technical selection of the architecture plays a critical role in the success of the project. We need to understand not only the principles of the same type of framework and the technical implementation, but also the strengths and weaknesses of each, so that we can face fewer difficulties in the implementation of the project.

So this blog post we're going to make a deep contrast between mybatis and hibernate to deepen our understanding of the persistence framework. Let's start our trip today.

Hibernate vs. MyBatis 1. Brief introduction

Hibernate:hibernate is one of the most popular ORM frameworks available today, providing a more complete package for JDBC. Hibernate's O/R mapping implements the mapping between Pojo and database tables, as well as the automatic generation and execution of SQL.

Mybatis:mybatis is also a very popular ORM framework, and the main focus is on the mapping relationship between POJO and SQL. The parameters that SQL requires and the returned result fields are then mapped to the specified POJO by mapping the configuration file. Mybatis is an ORM implementation of "SQL Mapping" relative to Hibernate "O/R".

2. Development speed
    1. Ease of

      Hibernate's real mastery is more difficult than mybatis, and hibernate is more heavyweight than MyBatis.

      The MyBatis framework is relatively simple and easy to get started with, but also relatively rudimentary.

    2. Development effort

      MyBatis need us to manually write SQL statements, return to the most primitive way, so you can specify the fields of the query according to the requirements, improve the efficiency of the program query.

      Hibernate can also write its own SQL statement to specify the fields that need to be queried, but this destroys hibernate encapsulation and simplicity.

3. Database portability

MyBatis because all SQL is dependent on database writing, extensibility, migration is poor.

Hibernate is associated with the database in XML, so HQL is not very concerned about what database to use.

4. Caching mechanism comparison
    1. Same point

      Hibernate and MyBatis level two caches, in addition to using the system's default caching mechanism, can completely overwrite the caching behavior by implementing your own cache or by creating an adapter for other third-party caching scenarios.

    2. Different points

      Hibernate's Level Two cache configuration is configured in detail in the configuration file generated by the Sessionfactory, and then configured in the specific table-object map to be that cache.

      MyBatis's Level Two cache configuration is configured in detail in each specific table-object map, so that different caching mechanisms can be customized for different tables. And MyBatis can share the same cache configuration and instance in the namespace, implemented through Cache-ref.

    3. Comparison of the two

      Because Hibernate has a good management mechanism for querying objects, users don't have to worry about SQL. Therefore, if dirty data appears when using level two cache, the system will report an error and prompt. In this regard, MyBatis requires special care when using level two caches. Avoid the blind use of the cache if you cannot fully determine the scope of the data update operation. Otherwise, the appearance of dirty data will bring great hidden trouble to the normal operation of the system.

5. The comparison summarizes the similarities between the two
    • Both hibernate and MyBatis can be generated sessionfactory by Sessionfactorybuider from an XML configuration file, and then generated by the sessionfactory session, Finally, the session is opened to execute the transaction and SQL statements. The life cycle of the sessionfactorybuider,sessionfactory,session is similar. As shown in the following:

    • Both Hibernate and MyBatis support JDBC and JTA transaction processing.
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.

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.

A word summary

At the end of the blog post, we summarize the differences between MyBatis and hibernate from six words:

Mybatis: Small, convenient, efficient, simple, direct, semi-automatic

Hibernate: Powerful, convenient, efficient, complex, indirect, fully automated

A detailed comparison of the "persistence framework" MyBatis with 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.