Home  >  Article  >  Java  >  The difference between mybatis and hibernate

The difference between mybatis and hibernate

(*-*)浩
(*-*)浩Original
2019-06-05 14:28:293459browse

The two mainstream framework combinations we use in java development are SSM, Spring, SpringMVC, MyBatis and SSH, Struts2, Spring, and Hibernate. Today we will first take a look at the differences between the two database frameworks. .

The difference between mybatis and hibernate

First aspect: Comparison of development speed

In terms of development speed, it is more difficult to truly master Hibernate than Mybatis some. The Mybatis framework is relatively simple and easy to use, but it is also relatively crude. Personally, I think that to use Mybatis well, you must first understand Hibernate. (Recommended learning: Java Video Tutorial)

Compared with the development speed of the two, not only the characteristics and performance of the two must be considered, but also which one should be considered based on the project needs. It is more suitable for project development. For example, there are basically no complex queries used in a project, just simple additions, deletions, modifications and queries. In this way, the efficiency of choosing hibernate is very fast, because the basic SQL statements have been encapsulated, and you do not need to go there at all. Writing SQL statements saves a lot of time, but for a large project, there are many complex statements, so choosing hibernate is not a good choice. Choosing mybatis will speed up a lot, and the management of statements is also more convenient. .

Second aspect: Comparison of development workload

Hibernate and MyBatis both have corresponding code generation tools. Simple and basic DAO layer methods can be generated. For advanced queries, Mybatis requires manual writing of SQL statements and ResultMap. Hibernate has a good mapping mechanism. 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 query will query all fields in the table, which will cause performance consumption. Hibernate can also write its own SQL to specify the fields that need to be queried, but this destroys the simplicity of Hibernate development. The SQL of Mybatis is written manually, so the query fields can be specified as needed.

The tuning of Hibernate HQL statements requires printing out the SQL, and Hibernate's SQL is disliked by many people because it is too ugly. The SQL of MyBatis is written manually by myself, 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 the function of object state management (state management). Developers no longer need to worry about the details of the underlying database system. In other words, compared to the common JDBC/SQL persistence layer solution 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 and do not have to consider the execution of SQL statements. These details are already taken care of by Hibernate, and only developers need to understand them when tuning system performance. MyBatis has no documentation in this area, and users need to manage the objects themselves in detail.

The fifth aspect: caching mechanism

Hibernate cache

Hibernate first-level cache is the Session cache, make good use of the first-level cache Caching needs to manage the life cycle of Session well. It is recommended to use a Session in an Action operation. The first-level cache requires strict management of Session.

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 data contained in some collection attributes of the SessionFactory object (mapping element data and scheduled SQL statements, etc.), which is read-only for applications. The external cache stores a copy of the database data, and its function is similar to the first-level cache. In addition to using 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 process-level cache or SessionFactory-level cache. It can be shared by all sessions. Its life cycle exists and dies along with the life cycle of 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.

By default, caching is not enabled. In addition to local session caching, it can enhance monetization and is also necessary to handle circular dependencies. To enable the second level cache, you need to add a line to your SQL mapping file:

Literally like this. The effect of this simple statement is as follows:

All select statements in the mapping statement file will be cached.

All insert, update and delete statements in the mapping statement file will refresh the cache.

The cache will use the Least Recently Used (LRU, least recently used) algorithm to recover.

According to the schedule (such as no Flush Interval, no refresh interval), the cache will not be refreshed in any time order.

The cache stores 1024 references to the list collection or object (regardless of what the query method returns).

The cache will be treated as a read/write (readable/writable) cache, which means that object retrieval is not shared and can be safely modified by the caller without interfering with what other callers or threads are doing. Potential Modifications.

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

For example:

This more advanced configuration creates a FIFO cache , and refreshes every 60 seconds, saving 512 references to the result object or list, and the returned objects are considered read-only, so modifying them between callers in different threads can cause conflicts. The available eviction strategies are, the default is LRU:

LRU - Least Recently Used: Remove objects that have not been used for the longest time.

FIFO – First in, first out: Remove objects in the order they enter the cache.

SOFT – Soft Reference: Removes objects based on garbage collector status and soft reference rules.

WEAK - Weak References: More aggressively remove objects based on garbage collector status and weak reference rules.

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

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

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

Similar points: In addition to using the system's default caching mechanism, 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 caching solutions.

Difference: Hibernate's second-level cache configuration is configured in detail in the configuration file generated by SessionFactory, and then the type of cache is configured in the specific table-object mapping.

MyBatis's second-level 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 between 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 second-level cache, the system will report an error and prompt.

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

For more Java-related technical articles, please visit the Java Development Tutorial column to learn!

The above is the detailed content of The difference between mybatis and hibernate. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:MapReduce principleNext article:MapReduce principle