Home Java javaTutorial The difference between mybatis and hibernate

The difference between mybatis and hibernate

Jun 05, 2019 pm 02:28 PM
hibernate mybatis

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

iBatis vs. MyBatis: Which one is better for you? iBatis vs. MyBatis: Which one is better for you? Feb 19, 2024 pm 04:38 PM

iBatis vs. MyBatis: Which should you choose? Introduction: With the rapid development of the Java language, many persistence frameworks have emerged. iBatis and MyBatis are two popular persistence frameworks, both of which provide a simple and efficient data access solution. This article will introduce the features and advantages of iBatis and MyBatis, and give some specific code examples to help you choose the appropriate framework. Introduction to iBatis: iBatis is an open source persistence framework

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Comparative analysis of the functions and performance of JPA and MyBatis Comparative analysis of the functions and performance of JPA and MyBatis Feb 19, 2024 pm 05:43 PM

JPA and MyBatis: Function and Performance Comparative Analysis Introduction: In Java development, the persistence framework plays a very important role. Common persistence frameworks include JPA (JavaPersistenceAPI) and MyBatis. This article will conduct a comparative analysis of the functions and performance of the two frameworks and provide specific code examples. 1. Function comparison: JPA: JPA is part of JavaEE and provides an object-oriented data persistence solution. It is passed annotation or X

Various ways to implement batch deletion operations in MyBatis Various ways to implement batch deletion operations in MyBatis Feb 19, 2024 pm 07:31 PM

Several ways to implement batch deletion statements in MyBatis require specific code examples. In recent years, due to the increasing amount of data, batch operations have become an important part of database operations. In actual development, we often need to delete records in the database in batches. This article will focus on several ways to implement batch delete statements in MyBatis and provide corresponding code examples. Use the foreach tag to implement batch deletion. MyBatis provides the foreach tag, which can easily traverse a set.

Detailed explanation of how to use MyBatis batch delete statements Detailed explanation of how to use MyBatis batch delete statements Feb 20, 2024 am 08:31 AM

Detailed explanation of how to use MyBatis batch delete statements requires specific code examples. Introduction: MyBatis is an excellent persistence layer framework that provides rich SQL operation functions. In actual project development, we often encounter situations where data needs to be deleted in batches. This article will introduce in detail how to use MyBatis batch delete statements, and attach specific code examples. Usage scenario: When deleting a large amount of data in the database, it is inefficient to execute the delete statements one by one. At this point, you can use the batch deletion function of MyBatis

Detailed explanation of MyBatis cache mechanism: understand the cache storage principle in one article Detailed explanation of MyBatis cache mechanism: understand the cache storage principle in one article Feb 23, 2024 pm 04:09 PM

Detailed explanation of MyBatis caching mechanism: One article to understand the principle of cache storage Introduction When using MyBatis for database access, caching is a very important mechanism, which can effectively reduce access to the database and improve system performance. This article will introduce the caching mechanism of MyBatis in detail, including cache classification, storage principles and specific code examples. 1. Cache classification MyBatis cache is mainly divided into two types: first-level cache and second-level cache. The first-level cache is a SqlSession-level cache. When

Detailed explanation of MyBatis first-level cache: How to improve data access efficiency? Detailed explanation of MyBatis first-level cache: How to improve data access efficiency? Feb 23, 2024 pm 08:13 PM

Detailed explanation of MyBatis first-level cache: How to improve data access efficiency? During the development process, efficient data access has always been one of the focuses of programmers. For persistence layer frameworks like MyBatis, caching is one of the key methods to improve data access efficiency. MyBatis provides two caching mechanisms: first-level cache and second-level cache. The first-level cache is enabled by default. This article will introduce the mechanism of MyBatis first-level cache in detail and provide specific code examples to help readers better understand

In-depth understanding of the batch Insert implementation principle in MyBatis In-depth understanding of the batch Insert implementation principle in MyBatis Feb 21, 2024 pm 04:42 PM

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

See all articles