search
HomeJavajavaTutorialThe difference between mybatis and hibernate

The difference between mybatis and hibernate

Jun 05, 2019 pm 02:28 PM
hibernatemybatis

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
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool