I haven't used mybatis much before, I only know that it is an orm database framework like Hibernate. As the proficiency of using increases, I found that it is very different from hibernate. Interested friends can learn it through this article.
Taking advantage of this weekend, I took the time to sort out some commonly used technologies, and the editor will update the content from time to time.
First, let’s briefly introduce the concepts of the two:
Hibernate: Hibernate is the most popular ORM framework at present and provides a relatively complete encapsulation of the database structure.
Mybatis: Mybatis is also a very popular ORM framework, with the main focus 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 tools 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, so developers do not need to care about SQL generation and result mapping, and can focus more on business processes.
2. Comparison of development difficulty
Hibernate development is more difficult than Mybatis. Mainly because Hibernate is complex and large, and has a long learning cycle.
Mybatis is relatively simple, and Mybatis mainly relies on sql writing, making developers feel more familiar.
3. Comparison of sql writing
Mybatis' SQL is written manually, so you can specify the fields for query as needed. However, there is no log statistics of your own, so you need to use log4j to record logs.
Hibernate can also write SQL itself 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
Since all SQL is written on the database, Mybatis has poor scalability and migration.
The specific relationship between Hibernate and database is in XML, so HQL is not very concerned about which database it uses.
5. Comparison of caching mechanisms
Similarities: In addition to using the system's default caching mechanism, the second-level cache of Hibernate and Mybatis can completely overwrite the cache behavior by implementing your own cache or creating adapters for other third-party cache solutions.
Differences: Hibernate's secondary cache configuration is configured in detail in the configuration file generated by SessionFactory, and then it is configured in the specific table-object map which cache is configured.
MyBatis's secondary cache configuration is configured in detail in each specific table-object map, so that different cache mechanisms can be customized for different tables. And Mybatis can share the same cache configuration and instance in the namespace, which is implemented through Cache-ref.
Comparison of 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 secondary cache, the system will report an error and prompt.
In this regard, MyBatis requires special care when using L2 cache. If the scope of data update operations cannot be fully determined, avoid blind use of Cache. Otherwise, the appearance 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, full automatic
Hibernate and MyBatis can both generate SessionFactory from XML configuration file through SessionFactoryBuider, and then generate Session from SessionFactory, and finally execute transactions and SQL statements.
The advantage of MyBatis is that MyBatis can perform more detailed SQL optimization, reduce query fields, and be easy to master.
The advantage of Hibernate is that DAO layer development is simpler than MyBatis, which requires maintenance of SQL and result mapping. The database portability is very good, MyBatis database portability is not good, and different databases need to write different SQL. There is a better Level 2 caching mechanism, and third-party caching can be used. MyBatis itself provides poor caching mechanisms.
mybatis:
1. It is simple to get started, learn and use, and provides automatic object binding function for database queries, and continues a good experience in SQL usage. It is quite perfect for projects that do not have such high object model requirements.
2. More detailed SQL optimization can be performed, which can reduce query fields.
3. The disadvantage is that the framework is still relatively simple and the functions are still missing. Although the data binding code is simplified, the entire underlying database query actually has to be written by yourself, the workload is relatively large, and it is not easy to adapt to rapid database modification.
4. The secondary caching mechanism is poor.
hibernate:
1. Powerful functions, good database irrelevance, and strong O/R mapping capabilities. If you are quite proficient in Hibernate and properly encapsulated Hibernate, then the entire persistence layer code of your project will be quite simple, there is very little code to write, the development speed is very fast, and it is very cool.
2. There is a better secondary caching mechanism, and third-party caching can be used.
3. The disadvantage is that the learning threshold is not low, and you must be proficient in it. How to design O/R mapping, how to balance the performance and object model, and how to use Hibernate well requires your experience and ability to be strong.
Let me give you a vivid metaphor:
mybatis: Mechanical tools are easy to use and can be used as soon as possible, but the work still needs to be done by yourself, but the tools are alive, so how to make them depends on me.
hibernate: Intelligent robot, but it is very expensive to develop it (learning, proficiency) and work can get rid of it, but only what it can do.