A detailed comparison of Mybatis and Hibernate

http://blog.csdn.net/jiuqiyuliang/article/details/45378065

 

http://www.cnblogs.com/inspurhaitian/p/4647485.html

 

 

In this blog post, we focus on analyzing the difference between Mybatis and hibernate . Of course, in the previous blog post, we have deeply studied the principles of Mybatis and Hibernate.

Mybatis

  1. [Persistence Framework] Mybatis Introduction and Principle
  2. [Persistence framework] SpringMVC+Spring4+Mybatis3 integration, developing simple web projects + source code download

Hibernate

  1. [SSH Advanced Road] Hibernate Basic Principles (1)
  2. [SSH advanced road] Hibernate builds development environment + simple example (2)
  3. [SSH Advanced Road] Hibernate Basic Mapping (3)
  4. [SSH advanced road] Hibernate mapping - many-to-one one-way association mapping (4)
  5. [SSH advanced road] Hibernate mapping - one-to-one one-way association mapping (5)
  6. [SSH Advanced Road] Hibernate Mapping - One-to-One Bidirectional Association Mapping (6)
  7. [SSH Advanced Road] Hibernate Mapping - One-to-Many Association Mapping (7)
  8. [SSH Advanced Road] Hibernate Mapping - Many-to-Many Association Mapping (8)
  9. [SSH Advanced Road] Hibernate Series - Summary (9)

As a good programmer, knowing only one ORM framework is not enough. Before developing a project, the technical selection of the architecture plays a crucial role in the success of the project. We not only need to understand the principles and technical implementation of the same type of framework, but also have a deep understanding of their respective advantages and disadvantages, so that we can face fewer difficulties in the implementation of the project.

So in this blog post, we will make an in-depth comparison between Mybatis and Hibernate to deepen our understanding of the persistence framework. Let's start today's trip.

Comparison between Hibernate and Mybatis

1 Introduction

Hibernate: Hibernate is one of the most popular ORM frameworks and provides a relatively complete encapsulation of JDBC. Hibernate's O/R Mapping implements the mapping between POJOs and database tables, as well as the automatic generation and execution of SQL.

Mybatis: Mybatis is also a very popular ORM framework, the main focus is the mapping relationship between POJO and SQL. Then through the mapping configuration file, the parameters required by SQL and the returned result fields are mapped to the specified POJO. Compared with Hibernate "O/R", Mybatis is an ORM implementation of "Sql Mapping".

2. Development speed

  1. Degree of difficulty

    The real master of Hibernate is more difficult than Mybatis, and Hibernate is more heavyweight than Mybatis.

    The Mybatis framework is relatively simple and easy to use, but it is also relatively simple.

  2. development effort

    Mybatis requires us to manually write SQL statements and return to the most primitive way, so we can specify query fields as required to improve the query efficiency of the program.

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

3. Database portability

Mybatis has poor scalability and migration because all SQL is written on the database.

The specific association between Hibernate and the database is in XML, so HQL is not very concerned about the specific database used.

4. Comparison of caching mechanisms

  1. Same point

    In addition to using the default cache mechanism of the system, the second-level cache of Hibernate and Mybatis can completely override the cache behavior by implementing your own cache or creating adapters for other third-party cache solutions.

  2. difference

    Hibernate的二级缓存配置在SessionFactory生成的配置文件中进行详细配置,然后再在具体的表-对象映射中配置是那种缓存。

    MyBatis的二级缓存配置都是在每个具体的表-对象映射中进行详细配置,这样针对不同的表可以自定义不同的缓存机制。并且Mybatis可以在命名空间中共享相同的缓存配置和实例,通过Cache-ref来实现。

  3. 两者比较

    因为Hibernate对查询对象有着良好的管理机制,用户无需关心SQL。所以在使用二级缓存时如果出现脏数据,系统会报出错误并提示。而MyBatis在这一方面,使用二级缓存时需要特别小心。如果不能完全确定数据更新操作的波及范围,避免Cache的盲目使用。否则,脏数据的出现会给系统的正常运行带来很大的隐患。

5. 两者对比总结

两者相同点

  • Hibernate与MyBatis都可以是通过SessionFactoryBuider由XML配置文件生成SessionFactory,然后由SessionFactory 生成Session,最后由Session来开启执行事务和SQL语句。其中SessionFactoryBuider,SessionFactory,Session的生命周期都是差不多的。如下图所示:

    write picture description here

  • Hibernate和MyBatis都支持JDBC和JTA事务处理。

Hibernate优势

  • Hibernate的DAO层开发比MyBatis简单,Mybatis需要维护SQL和结果映射。

  • Hibernate对对象的维护和缓存要比MyBatis好,对增删改查的对象的维护要方便。

  • Hibernate数据库移植性很好,MyBatis的数据库移植性不好,不同的数据库需要写不同SQL。

  • Hibernate有更好的二级缓存机制,可以使用第三方缓存。MyBatis本身提供的缓存机制不佳。

Mybatis优势

  • MyBatis可以进行更为细致的SQL优化,可以减少查询字段。

  • MyBatis容易掌握,而Hibernate门槛较高。

一句话总结

At the end of the blog post, we summarize the differences between Mybatis and Hibernate in six words:

Mybatis : Small, Convenient, Efficient, Simple, Direct, Semi-Automated

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326858363&siteId=291194637