The difference between Mybatis and Hibernate

  • 2020-04-01 04:37:41
  • OfStack

I haven't used mybatis much before, except that it is an orm database framework like hibernate. With the increase of proficiency, it is very interesting to find that it is different from hibernate to learn together with this article

Take the time this weekend to sort out some of the most common techniques, and the site will be updated irregularly.

Firstly, the two concepts are briefly introduced:

Hibernate: Hibernate is the most popular ORM framework, which provides a relatively complete encapsulation of database structure.

Mybatis: Mybatis is also a very popular ORM framework, focusing on the mapping between pojos and SQL.

Secondly, specific from several aspects of the difference between the two:

1. The biggest difference between the two:

For simple logic, Hibernate and MyBatis have corresponding code generation tools, which can generate simple and basic DAO layer methods.

For advanced queries, Mybatis requires manual writing of SQL statements, as well as a ResultMap. Hibernate has a good mapping mechanism, so developers don't need to worry about SQL generation and result mapping, and can focus more on business processes.

2. Development difficulty comparison

Hibernate development is more difficult than Mybatis. Mainly because Hibernate is more complex and large, the learning cycle is longer.

Mybatis is relatively simple, and Mybatis mainly relies on the writing of SQL, so that developers feel more familiar.

3. SQL writing comparison

The SQL for Mybatis is written manually, so you can specify the fields of the query as required. However, you don't have your own log statistics, so log with log4j.

Hibernate can also write its own SQL to specify the fields to query, but this breaks the simplicity of Hibernate development. Hibernate, however, has its own log statistics.

4. Database scalability comparison

Mybatis because all SQL is written on the database, so the scalability, migration is poor.

Hibernate and database-specific associations are all in XML, so HQL doesn't care much about which database is used.

5. Cache mechanism comparison

Similarities: both Hibernate and Mybatis can fully override caching behavior by implementing your own caching or creating adapters for other third-party caching schemes, in addition to using the system's default caching mechanism.

Difference: Hibernate's second-level cache configuration is configured in detail in a configuration file generated by the SessionFactory and then configured in a specific table-object map to be that kind of cache.

The secondary cache configuration of MyBatis is configured in detail in each specific table-object map 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. So if dirty data appears when using a level 2 cache, the system will report an error and prompt.

And MyBatis in this respect, the use of the secondary cache needs to be particularly careful. Avoid blind use of the Cache if you are not completely sure of the extent of the data update operation. Otherwise, the appearance of dirty data will bring great hidden trouble to the normal operation of the system.

6. Conclusion:

Mybatis: compact, convenient, efficient, simple, direct, semi-automatic
Hibernate: powerful, convenient, efficient, complex, roundabout, fully automatic

Both Hibernate and MyBatis can generate SessionFactory by SessionFactoryBuider from the XML configuration file, then generate Session by SessionFactory, and finally start the execution of transactions and SQL statements by Session.

And the advantage of MyBatis is that MyBatis can be more detailed SQL optimization, can reduce the query field, and easy to grasp.

The advantage of Hibernate is that DAO layer development is simpler than MyBatis, which requires maintenance of SQL and result mapping. Database portability is good, the database portability of MyBatis is not good, different databases need to write different SQL. There is a better second-level caching mechanism and third-party caching can be used. MyBatis itself provides a poor caching mechanism.

Mybatis:

1. Simple to get started and ready to use, it provides the automatic object binding function of database query, and continues the good experience of using SQL, which is perfect for projects with less high requirements of the object model.

2. More detailed SQL optimization can be done to reduce query fields.

3. The disadvantage is that the framework is still relatively simple and the function is still missing. Although it simplifies the data binding code, the whole underlying database query actually needs to be written by itself, the workload is relatively large, and it is not easy to adapt to the rapid database modification.

4. Poor second-level caching mechanism.

Hibernate:

1. Powerful functions, good database independence, strong O/R mapping ability, if you are quite proficient in Hibernate, and Hibernate for the appropriate encapsulation, then your project the entire persistence layer code will be quite simple, need to write very little code, development speed is very fast, very cool.

2. There is a better second-level caching mechanism, and third-party caching can be used.

3. The disadvantage is that the learning threshold is not low, to master the threshold is higher, and how to design O/R mapping, how to strike a balance between performance and object model, and how to use Hibernate needs your experience and ability to be very strong.
Here's a metaphor:

Mybatis: mechanical tools, easy to use, use, but the work or to do their own, but the tools are alive, how to make up to me.

Hibernate: smart robot, but the cost of developing it (learning, proficiency) is so high that work can be done without it, but only for what it can do. The & # 65279; The & # 65279;


Related articles: