Talking about the characteristics and comparison of MyBatis and Hibernate

MyBatis and Hibernate have something in common, both are persistence layer frameworks. But Hibernate can be said to be an old framework. The reason for this is that although Hibernate has better portability and better encapsulation of JDBC, it is easy to use SQL to operate, but because MySQL database is only suitable for small and medium-sized enterprises , so the use of Hibernate has certain limitations; compared with Hibernate, MyBatis's encapsulation of JDBC is not so deep, and it has higher requirements for developers, but it has strong optimization and adaptability to large databases, so it is suitable for for processing large amounts of data. The characteristics and comparison of MyBatis and Hibernate, the specific differences can be seen as follows:

First of all, in terms of development speed, Mybatis is actually better than Hibernate, because Hibernate is a deep encapsulation of JDBC, while Mybatis is more open and easy to learn. This is also the reason why Mybatis is more popular. Because of this, the development community of Mybatis has become active in recent years, and it is more convenient to download some tools that support development; Mybatis also has its own code generation tool, which can generate simple and basic DAO layer methods. For advanced queries, Mybatis needs to manually write SQL statements and ResultMap. While Hibernate has a good mapping mechanism, Shangxuetang • Baizhan programmer Mr. Chen pointed out that developers do not need to care about SQL generation and result mapping, and can focus more on business processes.

In terms of system tuning, Hibernate can formulate a reasonable caching strategy, handle delay loading better, have a more reasonable Session management mechanism, facilitate batch fetching, and have a reasonable O/R mapping design. In terms of tuning, Mybatis also has a Session mechanism and a second-level cache, and can also optimize the design of SQL; Hibernate generally queries all fields of the database. If you specify a field to query, the program is more cumbersome, while Mybatis's SQL is manually written. , so you can specify query fields as required. While Hibernate has its own log statistics, logging is also possible through Log4j.

In terms of object management, Hibernate is a complete object/relational mapping solution, and the object management function is relatively complete, so that developers no longer need to pay attention to the details of the underlying database system. That is to say, compared with the common JDBC/SQL persistence layer scheme that needs to manage SQL statements, Hibernate adopts a more natural object-oriented perspective to persist data in Java applications. MyBatis has no specific documentation in this regard, but it is also convenient for developers to use their own ideas to manage objects.

In terms of caching mechanism, as mentioned just now, Hibernate's first-level cache is the Session cache, and the first-level cache is related to the Session's life cycle. Its second-level cache is the SessionFactory-level cache, which can be divided into built-in cache and external cache. The built-in cache stores the data contained in some collection attributes of the SessionFactory object, including mapping element data and predetermined SQL statements, etc.; Mybatis can also be generally divided into second-level caches. The first-level cache is the SqlSession-level cache. The second-level cache is the mapper-level cache, which is shared by multiple SqlSessions, and its default state is to open the first-level cache, which is for developers. A convenience. However, some people pointed out that the caching mechanism of Mybatis limits the promotion of Mybatis to a certain extent. The above is the characteristics and comparison of MyBatis and Hibernate. Of course, there are many aspects that can be compared, and I will continue to share them with you in the future.
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325252208&siteId=291194637