Comparison of Hibernate and MyBatis

Comparison of Hibernate and MyBatis

 

introduce

 

Hibernate is currently the most popular O/R mapping framework. It was born in sf.net and has now become a part of Jboss.

Mybatis is another excellent O/R mapping framework. Currently belongs to a sub-project of apache.

 

MyBatis reference official website: http://www.mybatis.org/core/zh/index.html  

Hibernate reference: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/

 

Introduction to Hibernate

    Hibernate provides a relatively complete encapsulation of the database structure. Hibernate's O/R Mapping realizes the mapping between POJOs and database tables, as well as the automatic generation and execution of SQL. Programmers often only need to define the mapping relationship between POJOs and database tables, and then they can complete the persistence layer operations through the methods provided by Hibernate. Programmers do not even need to be proficient in SQL, Hibernate/OJB will automatically generate the corresponding SQL according to the specified storage logic and call the JDBC interface to execute it.

 

Introduction to MyBatis

    The focus of iBATIS lies in the mapping relationship between POJO and SQL. Then through the mapping configuration file, the parameters required by the SQL and the returned result fields are mapped to the specified POJO. Compared with Hibernate "O/R", iBATIS is an ORM implementation of "Sql Mapping".

 

 

 

Development comparison

 

development speed

      1. The real master of Hibernate is harder than Mybatis.

      2. 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

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

      2. 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.

 

 

 

System tuning comparison

 

Hibernate 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

      1. A reasonable Session management mechanism is also required.

      2. MyBatis also has a secondary cache mechanism. 

      3. MyBatis can carry out detailed SQL optimization design.

 

Common ground:

      1. Session management mechanism, MyBatis is consistent with Hibernate's Session life cycle in terms of Session

      2. L2 cache

 

SQL optimization aspects

 

      1. 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.

 

      2. Hibernate HQL语句的调优需要将SQL打印出来,而Hibernate的SQL被很多人嫌弃因为太丑了。MyBatis的SQL是自己手动写的所以调整方便。但Hibernate具有自己的日志统计。Mybatis本身不带日志统计,使用Log4j进行日志记录。

 

扩展性方面

 

      1. Hibernate与具体数据库的关联只需在XML文件中配置即可,所有的HQL语句与具体使用的数据库无关,移植性很好。

      2. MyBatis项目中所有的SQL语句都是依赖所用的数据库的,所以不同数据库类型的支持不好。

 

 

 

对象管理与抓取策略

 

对象管理

Hibernate 是完整的对象/关系映射解决方案,它提供了对象状态管理(state management)的功能,使开发者不再需要理会底层数据库系统的细节。也就是说,相对于常见的 JDBC/SQL 持久层方案中需要管理 SQL 语句,Hibernate采用了更自然的面向对象的视角来持久化 Java 应用中的数据。

 

换句话说,使用 Hibernate 的开发者应该总是关注对象的状态(state),不必考虑 SQL 语句的执行。这部分细节已经由 Hibernate 掌管妥当,只有开发者在进行系统性能调优的时候才需要进行了解。

 

而MyBatis在这一块没有文档说明,用户需要对对象自己进行详细的管理。

 

 

抓取策略

 

      1. Hibernate对实体关联对象的抓取有着良好的机制。对于每一个关联关系都可以详细地设置是否延迟加载,并且提供关联抓取、查询抓取、子查询抓取、批量抓取四种模式。 它是详细配置和处理的。

 

      2. Mybatis的延迟加载是全局配置的。

 

 

 

缓存机制对比

 

Hibernate缓存

Hibernate一级缓存是Session缓存,利用好一级缓存就需要对Session的生命周期进行管理好。建议在一个Action操作中使用一个Session。一级缓存需要对Session进行严格管理。

 

Hibernate二级缓存是SessionFactory级的缓存。 SessionFactory的缓存分为内置缓存和外置缓存。内置缓存中存放的是SessionFactory对象的一些集合属性包含的数据(映射元素据及预定SQL语句等),对于应用程序来说,它是只读的。外置缓存中存放的是数据库数据的副本,其作用和一级缓存类似.二级缓存除了以内存作为存储介质外,还可以选用硬盘等外部存储设备。二级缓存称为进程级缓存或SessionFactory级缓存,它可以被所有session共享,它的生命周期伴随着SessionFactory的生命周期存在和消亡。

 

 

MyBatis缓存

MyBatis 包含一个非常强大的查询缓存特性,它可以非常方便地配置和定制。MyBatis 3 中的缓存实现的很多改进都已经实现了,使得它更加强大而且易于配置。

 

默认情况下是没有开启缓存的,除了局部的 session 缓存,可以增强变现而且处理循环 依赖也是必须的。要开启二级缓存,你需要在你的 SQL 映射文件中添加一行:  <cache/>

 

字面上看就是这样。这个简单语句的效果如下:

 

    1. 映射语句文件中的所有 select 语句将会被缓存。

    2. 映射语句文件中的所有 insert,update 和 delete 语句会刷新缓存。

    3. 缓存会使用 Least Recently Used(LRU,最近最少使用的)算法来收回。

    4. 根据时间表(比如 no Flush Interval,没有刷新间隔), 缓存不会以任何时间顺序 来刷新。

    5. 缓存会存储列表集合或对象(无论查询方法返回什么)的 1024 个引用。

    6. 缓存会被视为是 read/write(可读/可写)的缓存,意味着对象检索不是共享的,而 且可以安全地被调用者修改,而不干扰其他调用者或线程所做的潜在修改。

 

 

MyBatis加缓存例子:

    比如: <cache  eviction="FIFO"  flushInterval="60000"  size="512"  readOnly="true"/>

 

这个更高级的配置创建了一个 FIFO 缓存,并每隔 60 秒刷新,存数结果对象或列表的 512 个引用,而且返回的对象被认为是只读的,因此在不同线程中的调用者之间修改它们会 导致冲突。

 

flushInterval(刷新间隔)可以被设置为任意的正整数,而且它们代表一个合理的毫秒 形式的时间段。默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新。

 

size(引用数目)可以被设置为任意正整数,要记住你缓存的对象数目和你运行环境的 可用内存资源数目。默认值是1024。

 

readOnly(只读)属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓 存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势。可读写的缓存 会返回缓存对象的拷贝(通过序列化) 。这会慢一些,但是安全,因此默认是 false。

 

 

MyBatis可用的收回策略有, 默认的是 LRU:

    1. LRU – 最近最少使用的:移除最长时间不被使用的对象。

    2. FIFO – 先进先出:按对象进入缓存的顺序来移除它们。

    3. SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。

    4. WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。

 

 

相同点

 

      Hibernate和Mybatis的二级缓存除了采用系统默认的缓存机制外,都可以通过实现你自己的缓存或为其他第三方缓存方案,创建适配器来完全覆盖缓存行为。

 

不同点

 

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

 

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

 

两者比较

 

      因为Hibernate对查询对象有着良好的管理机制,用户无需关心SQL。所以在使用二级缓存时如果出现脏数据,系统会报出错误并提示。

 

      而MyBatis在这一方面,使用二级缓存时需要特别小心。如果不能完全确定数据更新操作的波及范围,避免Cache的盲目使用。否则,脏数据的出现会给系统的正常运行带来很大的隐患。

 

 

 

Hibernate与Mybatis对比总结

 

两者相同点

 

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

 

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

 

 

Mybatis优势

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

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

 

Hibernate优势

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

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

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

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

 

 

他人总结

 

      1. Hibernate功能强大,数据库无关性好,O/R映射能力强,如果你对Hibernate相当精通,而且对Hibernate进行了适当的封装,那么你的项目整个持久层代码会相当简单,需要写的代码很少,开发速度很快,非常爽。 

 

      2. Hibernate的缺点就是学习门槛不低,要精通门槛更高,而且怎么设计O/R映射,在性能和对象模型之间如何权衡取得平衡,以及怎样用好Hibernate方面需要你的经验和能力都很强才行。 

 

      3. iBATIS入门简单,即学即用,提供了数据库查询的自动对象绑定功能,而且延续了很好的SQL使用经验,对于没有那么高的对象模型要求的项目来说,相当完美。 

      

      4. iBATIS的缺点就是框架还是比较简陋,功能尚有缺失,虽然简化了数据绑定代码,但是整个底层数据库查询实际还是要自己写的,工作量也比较大,而且不太容易适应快速数据库修改。

Guess you like

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