I. Introduction to the framework
Hibernate is an excellent ORM framework (object relationship implicit), which highly encapsulates the relationship between SQL statements and tables. Developers only need to configure the tables and pojo classes. Relations are implicit, and there are many-to-many, many-to-one, and one-to-one relationships between tables and tables, and classes and classes. Developers do not need to write SQL statements, and focus on business logic.
Mybatis is also an excellent ORM framework, but it is relatively rudimentary compared to hibernate. It only encapsulates jdbc, and SQL statements and table relationships have to be mapped one by one.
Second, the use of the framework
1) After configuring the relationship, hibernate can directly operate on the object, allowing the use of the session object method to implement crud operations on the database, while mybatis is You need to manually write SQL statements. In this comparison, the development speed of hibernate is better than that of mybatis, but at the same time there are problems. In the face of some advanced queries, hibernate seems a little powerless, and hibernate’s sql statements are hard to write. Sometimes we do not need to query or modify some fields, but they encapsulate all of them. Although we can manually set the fields, it destroys the simplicity of hibernate. And mybatis can manually write sql for optimization, and has the function of dynamic sql, which can optimize sql.
2) Hibernate has its own caching mechanism, but mybatis does not, and most of them now use the first Three-party caching, so this advantage has been erased.
3) Hibernate is relatively heavyweight, with a high entry barrier, while mybatis is a lightweight package jdbc, which can be used now.
Three. Summary
1) Hibernate is suitable for operating on a single table, cannot optimize SQL, is not suitable for the development of high-concurrency projects, and is suitable for the development of OA.CRM and other projects.
2) The flexibility of mybatis sql statement is suitable for the development of Internet projects.
—————————————- ————————————————– ————————————————– ————————————
The advantages of Hibernate:
1. Hibernate is fully automatic. Hibernate can completely realize the operation of the database through the object-relational model. It has a complete mapping structure between JavaBean objects and the database to automatically generate sql.
2. Powerful functions, good database independence, strong O/R mapping ability, very little code to be written, and fast development speed.
3. There is a better second-level cache mechanism, and third-party caches can be used.
4. Good database portability.
5. Hibernate has a complete log system. The hibernate log system is very sound and covers a wide range of areas, including sql records, relationship abnormalities, optimization warnings, cache hints, dirty data warnings, etc.
Hibernate Disadvantages:
1. The threshold of learning is high, the threshold of proficiency is higher, how programmers design O/R mapping, how to balance between performance and object model, and how to make good use of Hibernate. And the ability is very strong.
2. Many of hibernate’s SQL are automatically generated and cannot be directly maintained. Although there are hql queries, the functions are still not as powerful as SQL. When you see abnormal requirements such as reports , Hql query should be virtual, that is to say, hql query is limited; although hibernate also supports native sql query, but the development model is different from orm, it needs to change the thinking, so it is somewhat inconvenient to use. In short, hibernate is not as flexible as mybatis in writing sql.
The advantages of Mybatis:
1. It is easy to use and master, provides automatic object binding function for database query, and continues a good SQL use experience. The object model required by the project is quite perfect.
2, sql is written in xml, which is convenient for unified management and optimization, and releases the coupling between sql and program code.
3. Provide mapping tags to support the orm field relationship mapping between objects and databases
4. Provide object relationship mapping tags to support the establishment and maintenance of object relationships
5 , Provide xml tags, support writing dynamic sql.
6. The speed is faster than Hibernate.
Disadvantages of Mybatis:
1. When there are many related tables and many fields, the sql workload is very heavy. Big.
2, sql depends on the database, resulting in poor database portability.
3. Because the tag id in xml must be unique, the method in DAO does not support method overloading.
4. Object relationship mapping tags and field mapping tags are only descriptions of the mapping relationship, and the specific implementation still depends on sql.
5. The DAO layer is too simple, and the workload of object assembly is relatively large.
6. Cascade update and cascade delete are not supported.
7. In addition to the basic recording function, the Mybatis log is much weaker in other functions.
8. It is not convenient to debug when writing dynamic sql, especially when the logic is complicated.
9. The provided xml tag function for writing dynamic sql is simple, writing dynamic sql is still limited, and the readability is low.