Comparison of Hibernate and Mybatis

Comparison of Hibernate and Mybatis

First aspect: Comparison of development speed

As far as the speed of development is concerned, Hibernate is more difficult to master than Mybatis. The Mybatis framework is relatively simple and easy to use, but it is also relatively simple. Personally think that if you want to use Mybatis well, you must first understand Hibernate.

Compared with the development speed of the two, not only must we consider the characteristics and performance of the two, but also to consider which one is more suitable for project development according to project needs. For example: there is basically no complicated query used in a project, it is simple The addition, deletion and modification of the search, so the efficiency of choosing hibernate is very fast, because the basic SQL statement has been encapsulated, you do not need to write SQL statement at all, which saves a lot of time, but for a large project, complex statements There are many, so choosing hibernate is not a good choice. Mybatis will speed up a lot, and the management of statements is also more convenient.

Second aspect: Comparison of development workload

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. And Hibernate has a good mapping mechanism, developers do not need to care about SQL generation and result mapping, you can focus more on business processes.

The third aspect: SQL optimization

Hibernate query will query all the fields in the table, which will consume performance. Hibernate can also write SQL to specify the fields that need to be queried, but this will destroy the simplicity of Hibernate development. The SQL of Mybatis is written manually, so you can specify the query fields as required.

The tuning of Hibernate HQL statement needs to print out SQL, and Hibernate SQL is rejected by many people because it is too ugly. The SQL of MyBatis 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.

Fourth aspect: comparison of object management

Hibernate is a complete object / relational mapping solution, which provides object state management (state management), so that developers no longer need to pay attention to the details of the underlying database system. In other words, compared to the common JDBC / SQL persistence layer scheme that needs to manage SQL statements, Hibernate uses a more natural object-oriented perspective to persist data in Java applications.

In other words, developers using Hibernate should always pay attention to the state of the object, without having to consider the execution of SQL statements. This part of the details has been properly managed by Hibernate, and only developers need to understand it when tuning system performance. MyBatis has no documentation in this section, and users need to manage the objects themselves in detail.
Fifth aspect: cache mechanism

Hibernate cache

Hibernate first-level cache is the session cache, and the use of the first-level cache requires the management of the session life cycle. It is recommended to use a Session in an Action operation. The first-level cache requires strict management of the session.

Hibernate second level cache is SessionFactory level cache. SessionFactory cache is divided into built-in cache and external cache. The built-in cache stores data contained in some collection attributes of the SessionFactory object (mapping element data and scheduled SQL statements, etc.). For applications, it is read-only. The external cache stores a copy of the database data, and its role is similar to the primary cache. In addition to the memory as the storage medium, the secondary cache can also use external storage devices such as hard disks. The second-level cache is called the process-level cache or the SessionFactory-level cache, and it can be shared by all sessions. Its life cycle is accompanied by the existence and demise of the life cycle of the SessionFactory.

MyBatis cache

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

By default, the cache is not enabled. In addition to the local session cache, it can enhance the realization and deal with circular dependencies is also necessary. To enable the secondary cache, you need to add a line to your SQL mapping file: <cache />

This is literally the case. The effect of this simple statement is as follows:

  1. All select statements in the mapping statement file will be cached.
  2. All insert, update, and delete statements in the mapping statement file will refresh the cache.
  3. The cache will be recovered using the Least Recently Used (LRU, least recently used) algorithm.
  4. According to the schedule (such as no Flush Interval, no refresh interval), the cache will not be refreshed in any chronological order.
  5. The cache stores 1024 references to list collections or objects (regardless of what the query method returns).
  6. The cache will be regarded as a read / write cache, which means that object retrieval is not shared and can be safely modified by the caller without interfering with potential changes made by other callers or threads.

All of these attributes can be modified through the attributes of the cache element.

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

This more advanced configuration creates a FIFO buffer and refreshes it every 60 seconds, storing 512 references to the result object or list, and the returned object is considered read-only, so between callers in different threads Modifying them will cause conflicts. Available recovery strategies are, the default is LRU:

  1. LRU-least recently used: remove objects that have not been used for the longest time.
  2. FIFO – first-in-first-out: remove objects in the order they enter the cache.
  3. SOFT-Soft reference: remove objects based on garbage collector status and soft reference rules.
  4. WEAK-Weak Reference: More aggressively remove objects based on garbage collector state and weak reference rules.

flushInterval (refresh interval) can be set to any positive integer, and they represent a reasonable time period in the form of milliseconds. By default, it is not set, that is, there is no refresh interval, and the cache is refreshed only when the statement is called.

size (number of references) can be set to any positive integer, remember the number of objects you cache and the number of available memory resources in your running environment. The default value is 1024.

The readOnly attribute can be set to true or false. A read-only cache will return the same instance of the cache object to all callers. Therefore these objects cannot be modified. This provides a very important performance advantage. The read-write cache will return a copy of the cached object (through serialization). This will be slower, but safe, so the default is false.

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

The difference: Hibernate's secondary cache configuration is configured in detail in the configuration file generated by SessionFactory, and then configured in the specific table-object mapping is the kind of cache.

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

Comparison of the two: Because Hibernate has a good management mechanism for query objects, users do not need to care about SQL. Therefore, if dirty data appears when using the secondary cache, the system will report an error and prompt.

In this respect, MyBatis needs to be especially careful when using the second-level cache. If you cannot fully determine the scope of the data update operation, avoid the blind use of Cache. Otherwise, the appearance of dirty data will bring great hidden dangers to the normal operation of the system.

Sixth aspect: summary

For the summary, you can go to the major java forums to take a look

Similarities: Both Hibernate and MyBatis can generate a SessionFactory from the XML configuration file through the SessionFactoryBuider, then a SessionFactory generates a Session, and finally the Session starts the execution of transactions and SQL statements. The life cycle of SessionFactoryBuider, SessionFactory, and Session are similar.

  • Both Hibernate and MyBatis support JDBC and JTA transaction processing.

Mybatis advantage

  • MyBatis can perform more detailed SQL optimization and reduce query fields.
  • MyBatis is easy to master, and Hibernate has a higher threshold.

Hibernate advantages

  • Hibernate's DAO layer development is simpler than MyBatis, and Mybatis needs to maintain SQL and result mapping.
  • Hibernate maintains and caches objects better than MyBatis, and it is convenient to maintain objects that are added, deleted, modified, and checked.
  • Hibernate database portability is very good, MyBatis database portability is not good, different databases need to write different SQL.
  • Hibernate has a better second-level cache mechanism, you can use third-party cache. The cache mechanism provided by MyBatis itself is not good.

Summary of others

  • Hibernate has powerful functions, good database independence, and strong O / R mapping ability. If you are quite proficient in Hibernate and properly encapsulate Hibernate, then the entire persistence layer code of your project will be quite simple, and very little code needs to be written , The development speed is very fast, very cool.
  • The disadvantage of Hibernate is that the learning threshold is not low, you have to master the threshold higher, and how to design O / R mapping, how to balance the performance and object model, and how to use Hibernate well, you need strong experience and ability. Just fine.
  • iBATIS is easy to get started, is ready to use, provides automatic object binding for database queries, and continues good SQL usage experience. It is perfect for projects that do not have such high object model requirements.
  • The disadvantage of iBATIS is that the framework is still relatively primitive, and the functions are still missing. Although the data binding code is simplified, the entire underlying database query is actually written by itself, the workload is relatively large, and it is not easy to adapt to rapid database modification.
Published 12 original articles · won 9 · 10 thousand views

Guess you like

Origin blog.csdn.net/finaly_yu/article/details/84998933