In depth analysis of the differences and uses between mybatis and Hibernate
For a long time, I was unfamiliar with mybatis. I only knew that it was an ORM database framework like hibernate. With the increase of proficiency, it is found that it is very different from hibernate, which should be analyzed and selected in combination with different situations.
For a long time, I was unfamiliar with mybatis. I only knew that it was an ORM database framework like hibernate. With the increase of proficiency, it is found that it is very different from hibernate, which should be analyzed and selected in combination with different situations. Based on the experience so far, the following points are summarized:
1. Hibernate is fully automatic, while mybatis is semi-automatic
Hibernate can operate the database through the object relational model, and has a complete mapping structure between JavaBean objects and the database to automatically generate SQL. Mybatis only has basic field mapping, and object data and actual relationship still need to be implemented and managed by handwritten SQL.
2. The portability of Hibernate database is much greater than that of mybatis
Hibernate greatly reduces the coupling between objects and databases (Oracle, mysql, etc.) through its powerful mapping structure and HQL language. Since MySQL needs handwritten SQL, its coupling with the database directly depends on the way programmers write SQL. If SQL is not universal and uses many SQL statements with certain database characteristics, the portability will be greatly reduced, The cost is high.
3. Hibernate has a complete log system, but mybatis lacks some
Hibernate log system is very sound and covers a wide range, including SQL record, relationship exception, optimization warning, cache prompt, dirty data warning, etc; In addition to the basic recording function, mybatis has many weak functions.
4. Compared with hibernate, mybatis needs to care about many details
Hibernate configuration is much more complex than mybatis, and the learning cost is also higher than mybatis. But it is also because mybatis is easy to use that it cares more about technical details than hibernate. Since mybatis does not need to consider many details and the development mode is very different from traditional JDBC, it is easy to start and develop the project, but ignoring details will lead to more bugs in the early stage of the project, so it is very slow to develop relatively stable software, but very fast to develop software. Hibernate is just the opposite. However, if you are proficient in using hibernate, in fact, the development efficiency is no worse than or even better than mybatis.
5. In terms of direct SQL optimization, mybatis is much more convenient than hibernate
Since the SQL of mybatis is written in XML, optimizing SQL is much more convenient than hibernate. However, many hibernate SQL are generated automatically and cannot be maintained directly; Although there is HQL, its function is not as powerful as SQL. When you see abnormal needs such as reports, HQL also stops cooking, that is to say, HQL has limitations; Although hibernate also supports native SQL, its development mode is different from ORM, and it needs to change thinking, so it is not very convenient to use. In short, hibernate is less flexible than mybatis in writing SQL.
With the increasing use, I made a further summary:
Mybatis: compact, convenient, efficient, simple, direct and semi-automatic
Hibernate: powerful, convenient, efficient, complex, curved, fully automatic
mybatis:
1. It is easy to learn and use. It provides the automatic object binding function of database query, and continues the good SQL experience. It is quite perfect for projects that do not have such high object model requirements.
2. You can perform more detailed SQL optimization and reduce query fields.
3. The disadvantage is that the framework is still relatively simple and the function is still missing. Although the data binding code is simplified, 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 rapid database modification.
4. The L2 cache mechanism is poor.
hibernate:
1. Powerful function, good database independence and strong O / R mapping ability. If you are quite proficient in Hibernate and properly encapsulate hibernate, the whole persistence layer code of your project will be quite simple, there is little code to write, and the development speed is very fast and very cool.
2. There is a better L2 cache mechanism, and the third-party cache can be used.
3. The disadvantage is that the learning threshold is not low. You need to be proficient in the higher threshold. You need strong experience and ability in how to design o / R mapping, how to balance performance and object model, and how to make good use of hibernate.
Take a vivid metaphor:
Mybatis: mechanical tools are easy to use and can be used as soon as they are used, but the work still needs to be done by myself, but the tools are live. How to make them is up to me.
Hibernate: intelligent robot, but the cost of developing it (learning and proficiency) is very high. We can get rid of it, but it is limited to what it can do.
summary
The above is the difference and use of mybatis and Hibernate introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!