Comparison of Hibernate and Mybatis----Summary

The first aspect: the comparison of development speed

In terms of development speed, Hibernate is harder to master 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.

Compared with the development speed of the two, it is not only necessary to consider the characteristics and performance of the two, but also to consider which one is more suitable for project development according to the project requirements. For example, there are basically no complex queries used in a project, it is simple In this way, the efficiency of selecting hibernate is very fast, because the basic SQL statements have been encapsulated, and you do not need to write SQL statements at all, which saves a lot of time, but for a large project, complex statements If there are more, it is not a good choice to choose hibernate. Choosing mybatis will speed up a lot, and the management of statements will be more convenient.

The second aspect: the comparison of development workload

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.

The third aspect: sql optimization

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.

The fourth aspect: the comparison of 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 SQL statements. 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.
Fifth aspect: caching mechanism

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.

MyBatis cache

MyBatis includes a very powerful query caching feature that can be configured and customized very easily. Many improvements to the cache implementation in MyBatis 3 have been implemented, making it more powerful and easier to configure.

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

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

  1. 映射语句文件中的所有 select 语句将会被缓存。
  2. 映射语句文件中的所有 insert,update 和 delete 语句会刷新缓存。
  3. 缓存会使用 Least Recently Used(LRU,最近最少使用的)算法来收回。
  4. 根据时间表(比如 no Flush Interval,没有刷新间隔), 缓存不会以任何时间顺序 来刷新。
  5. 缓存会存储列表集合或对象(无论查询方法返回什么)的 1024 个引用。
  6. 缓存会被视为是 read/write(可读/可写)的缓存,意味着对象检索不是共享的,而 且可以安全地被调用者修改,而不干扰其他调用者或线程所做的潜在修改。

所有的这些属性都可以通过缓存元素的属性来修改。

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

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

  1. LRU – 最近最少使用的:移除最长时间不被使用的对象。
  2. FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
  3. SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。
  4. WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。

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

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

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

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

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

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

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

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

第六方面:总结

对于总结,大家可以到各大java论坛去看一看

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

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

Mybatis优势

  • MyBatis可以进行更为细致的SQL优化,可以减少查询字段。
  • MyBatis容易掌握,而Hibernate门槛较高。

Hibernate优势

  • Hibernate的DAO层开发比MyBatis简单,Mybatis需要维护SQL和结果映射。
  • Hibernate对对象的维护和缓存要比MyBatis好,对增删改查的对象的维护要方便。
  • Hibernate数据库移植性很好,MyBatis的数据库移植性不好,不同的数据库需要写不同SQL。
  • Hibernate有更好的二级缓存机制,可以使用第三方缓存。MyBatis本身提供的缓存机制不佳。

他人总结

  • Hibernate功能强大,数据库无关性好,O/R映射能力强,如果你对Hibernate相当精通,而且对Hibernate进行了适当的封装,那么你的项目整个持久层代码会相当简单,需要写的代码很少,开发速度很快,非常爽。
  • Hibernate的缺点就是学习门槛不低,要精通门槛更高,而且怎么设计O/R映射,在性能和对象模型之间如何权衡取得平衡,以及怎样用好Hibernate方面需要你的经验和能力都很强才行。
  • iBATIS入门简单,即学即用,提供了数据库查询的自动对象绑定功能,而且延续了很好的SQL使用经验,对于没有那么高的对象模型要求的项目来说,相当完美。
  • iBATIS的缺点就是框架还是比较简陋,功能尚有缺失,虽然简化了数据绑定代码,但是整个底层数据库查询实际还是要自己写的,工作量也比较大,而且不太容易适应快速数据库修改。

Guess you like

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