In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Comparison between Hibernate and Mybatis
Today, my colleague told me that today's small companies rarely use hibernate, and most of them use mybatis. Usually also often contact these two aspects, just not very busy recently, check out other relevant technical documents on the Internet, sort out the comparison between Mybatis and Hibernate, and deepen our understanding of the persistence framework.
I. brief introduction
Hibernate
Hibernate is one of the most popular ORM frameworks at present, which provides a relatively complete package for JDBC. O _ Mapping of Hibernate implements the mapping between POJO and database tables, as well as the automatic generation and execution of SQL.
Mybatis
Mybatis is also a very popular ORM framework, focusing on the mapping between POJO and SQL. Then the parameters required by SQL and the returned result fields are mapped to the specified POJO through the mapping configuration file. Mybatis is an ORM implementation of "Sql Mapping" as opposed to hibernate "Obind R".
2. Speed of development
Degree of difficulty
The real mastery of Hibernate is more difficult than Mybatis, and Hibernate is a little more heavyweight than mybatis.
The Mybatis framework is relatively simple and easy to use, but it is also relatively crude.
Development workload
Mybatis requires us to write SQL statements manually to return to the original way, so we can specify the query fields according to the needs to improve the query efficiency of the program.
Hibernate can also write its own SQL statements to specify the fields to be queried, but this breaks the Hibernate encapsulation and simplicity.
III. Database portability
Mybatis is poor in scalability and migration because all SQL is written in a database.
The specific relationship between Hibernate and database is in XML, so HQL is not very concerned about what database is used.
IV. Comparison of caching mechanisms
Identical point
In addition to using the system default caching mechanism, Hibernate and Mybatis secondary caching can completely override caching behavior by implementing your own cache or creating adapters for other third-party caching schemes.
Differences
The secondary cache configuration of Hibernate is configured in detail in the configuration file generated by SessionFactory, and then that cache is configured in the specific table-object mapping.
The secondary cache configuration of MyBatis 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 instances 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 occurs when using the secondary cache, the system will report an error and prompt. In this respect, MyBatis needs to be very careful when using secondary caching. If you can not completely determine the scope of the data update operation, avoid the blind use of Cache. Otherwise, the appearance of dirty data will bring great hidden trouble to the normal operation of the system.
Fifth, a comparative summary of the two.
The similarities between the two
Both Hibernate and MyBatis can generate SessionFactory from the XML configuration file through SessionFactoryBuider, then generate Session from SessionFactory, and finally open the execution transaction and SQL statement by Session. Among them, the life cycle of SessionFactoryBuider,SessionFactory,Session is similar.
Both Hibernate and MyBatis support JDBC and JTA transactions.
Hibernate advantage
The development of the DAO layer of Hibernate is simpler than that of MyBatis, and Mybatis needs to maintain SQL and result mapping.
Hibernate maintains and caches objects better than MyBatis, and it is more convenient to maintain objects that are added, deleted, changed 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 two-tier caching mechanism and can use third-party caching. The caching mechanism provided by MyBatis itself is not good.
Mybatis advantage
MyBatis allows for more detailed SQL optimization, reducing the number of query fields.
MyBatis is easy to master, but the threshold of Hibernate is higher.
Summary in one sentence
At the end of the blog post, we summarize the differences between Mybatis and Hibernate in six words:
Mybatis: compact, convenient, efficient, simple, direct, semi-automatic
Hibernate: powerful, convenient, efficient, complex, indirect, fully automated
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.