I haven’t used mybatis much before. I only know that it is an ORM database framework like hibernate. As the proficiency of using it increases, I find that it is very different from hibernate. Interested friends should learn together through this article.
Taking advantage of this weekend, I will take the time to sort out some commonly used technologies. The editor will also Occasionally updated content.
First briefly introduce the concepts of the two:
Hibernate: Hibernate is currently the most popular ORM framework, providing a relatively complete encapsulation of the database structure.
Mybatis: Mybatis is also a very popular ORM framework. The main focus is on the mapping relationship between POJO and SQL.
Secondly, let’s talk about the differences between the two in several aspects:
1. The biggest difference between the two:
For simple logic, Hibernate and MyBatis have corresponding Code generation tool that can generate simple and basic DAO layer methods.
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.
2. Development difficulty comparison
Hibernate is more difficult to develop than Mybatis. Mainly because Hibernate is complex and large, and the learning cycle is long.
Mybatis is relatively simple, and Mybatis mainly relies on sql writing, making developers feel more familiar.
3.sql writing comparison
Mybatis’s SQL is written manually, so you can specify the query fields as needed. However, it does not have its own log statistics, so it needs to use log4j to record logs.
Hibernate can also write its own SQL to specify the fields that need to be queried, but this destroys the simplicity of Hibernate development. However, Hibernate has its own log statistics.
4. Comparison of database scalability
Mybatis has poor scalability and portability because all SQL is written in dependence on the database.
The specific relationship between Hibernate and the database is in XML, so HQL is not very concerned about the specific database used.
5. Comparison of caching mechanisms
Same points: In addition to using the system default caching mechanism, the second-level cache of Hibernate and Mybatis can be implemented by implementing your own cache or caching for other third parties. scenario, create an adapter to completely override caching behavior.
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 emergence of dirty data will bring great hidden dangers to the normal operation of the system.
6. Summary:
mybatis: small, convenient, efficient, simple, direct, semi-automatic
hibernate: powerful, convenient, efficient, complex, roundabout, fully automatic
Hibernate and MyBatis can use SessionFactoryBuider to generate a SessionFactory from an XML configuration file, then the SessionFactory generates a Session, and finally the Session starts executing transactions and SQL statements.
The advantage of MyBatis is that MyBatis can perform more detailed SQL optimization, reduce query fields, and is easy to master.
The advantage of Hibernate is that DAO layer development is simpler than MyBatis. Mybatis needs to maintain SQL and result mapping. The database portability is very good, but the database portability of MyBatis is not good. Different databases need to write different SQL. There is a better second-level caching mechanism that can use third-party caching. The caching mechanism provided by MyBatis itself is poor.
mybatis:
1. It’s easy to get started, you can learn and use it immediately. It provides automatic object binding function for database query, and continues the good SQL usage experience. For those who are not that advanced For projects requiring object models, it is quite perfect.
2. More detailed SQL optimization can be performed and query fields can be reduced.
3. The disadvantage is that the framework is still relatively simple and the functions are still missing. Although the data binding code has been simplified, the entire underlying database query still has to be written by yourself, and the workload is relatively large, and Not easily adaptable to rapid database modifications.
4. The second-level cache mechanism is not good.
hibernate:
1. Powerful functions, good database independence, and strong O/R mapping capabilities. If you are quite proficient in Hibernate and properly encapsulate Hibernate, then your The entire persistence layer code of the project will be quite simple. There is very little code to be written, and the development speed is very fast, which is very enjoyable.
2. There is a better second-level cache mechanism, and third-party cache can be used.
3. The disadvantage is that the threshold for learning is not low, and the threshold for mastery is even higher. Moreover, how to design O/R mapping, how to balance the trade-off between performance and object model, and how to make good use of Hibernate requires your experience and ability. They have to be very strong.
Let’s give a vivid metaphor:
mybatis: Mechanical tools are easy to use and can be used as soon as they are brought. However, you still have to do the work yourself, but the tools are alive and it is up to me to decide how to use them.
hibernate: Intelligent robot, but the cost of developing it (learning, proficiency) is very high, and the work can be rid of it, but it is limited to what it can do.
For more articles related to the difference between Mybatis and Hibernate, please pay attention to the PHP Chinese website!