1. Compare development speed

Hibernate is harder to really master than Mybatis. The Mybatis framework is relatively simple and easy to use, but also relatively crude. Personally think to use Mybatis or first of all to understand Hibernate.

The development community

Hibernate and Mybatis are both popular persistence layer development frameworks, but Hibernate development community is relatively more lively, support more tools, update is fast, the current highest version 4.1.8. Mybatis, on the other hand, is relatively quiet, with fewer tools and currently the highest version 3.2.

Development workload

Hibernate and MyBatis both have code generation tools. Simple and basic DAO layer methods can be generated.

For advanced queries, Mybatis needs to manually write SQL statements, as well as ResultMap. Hibernate has a good mapping mechanism, so developers don’t need to worry about SQL generation and result mapping, and can focus more on business processes.

2. System tuning compared with Hibernate tuning scheme

Develop a reasonable cache strategy;

Use the lazy loading feature whenever possible;

Adopt reasonable Session management mechanism;

Using batch capture, set reasonable batch processing parameters (BATCH_size);

Carry out reasonable O/R mapping design

Mybatis tuning scheme

MyBatis is consistent with Hibernate’s Session life cycle in terms of Session, and also needs a reasonable Session management mechanism. MyBatis also has a level 2 caching mechanism. MyBatis can carry out detailed SQL optimization design.

SQL optimization

Hibernate queries will query all the fields in the table, which can have a performance cost. Hibernate can also write its own SQL to specify which fields to query, but this breaks the brevity of Hibernate development. Mybatis SQL is written manually, so you can specify the query fields as required.

Tuning Hibernate HQL statements requires printing out SQL, which many people dislike because it is too ugly. MyBatis SQL is written manually so easy to adjust. But Hibernate has its own logging statistics. Mybatis does not use logging statistics, Log4j is used for logging.

Extensibility

The association between Hibernate and a specific database only needs to be configured in an XML file. All HQL statements are independent of the specific database, and portability is very good. All SQL statements in MyBatis project are dependent on the database used, so different database types are not supported well.

3. Object management and capture policy Object management

Hibernate is a complete object/relational mapping solution that provides object state management capabilities, removing the need for developers to care about the details of the underlying database system. That is, Hibernate takes a more natural object-oriented approach to persisting data in Java applications, rather than managing SQL statements in the common JDBC/SQL persistence layer scheme.

In other words, developers using Hibernate should always focus on the state of the object, not the execution of SQL statements. These details are already taken care of by Hibernate and only need to be understood by developers when tuning system performance.

While MyBatis has no documentation in this section, users need to manage objects in detail.

Fetching strategy

Hibernate has a good mechanism for fetching entity associated objects. For each association relation can be set in detail whether to delay loading, and provide associative fetch, query fetch, sub-query fetch, batch fetch four modes. It is configured and handled in detail.

Lazy loading of Mybatis is globally configured.

4. Compare the caching mechanism to Hibernate caching

Hibernate level 1 cache is Session cache. To make good use of level 1 cache, it is necessary to manage the life cycle of Session. It is recommended to use a Session in an Action. Level 1 caching requires strict management of sessions.

The Hibernate second-level cache is the SessionFactory level cache. SessionFactory caches are divided into built-in caches and external caches. The built-in cache holds the data (mapping element data, scheduled SQL statements, etc.) contained in the collection properties of the SessionFactory object, which is read-only to the application. The external cache stores a copy of the database data, which is similar to the level 1 cache. In addition to using memory as storage medium, level-2 cache can also use external storage devices such as hard disks. The second level cache is called the process level cache or SessionFactory level cache. It can be shared by all sessions and its life cycle lives and dies with the life cycle of the SessionFactory.

5. Comparative advantages

Mybatis advantage

MyBatis can be more detailed SQL optimization, can reduce the query fields.

MyBatis is easy to master, while Hibernate has a higher threshold.

Hibernate advantage

Hibernate’s DAO layer development is simpler than MyBatis, which maintains SQL and result mappings.

Hibernate maintains and cache objects better than MyBatis, and it is more convenient to maintain objects that are added, deleted, changed and checked.

Hibernate database portability is very good, MyBatis database portability is not good, different databases need to write different SQL.

Hibernate has a better second-level caching mechanism and can use third-party caches. MyBatis itself provides a poor caching mechanism.

More detailed source code reference source: minglisoft.cn/technology welcome to study related technology, source code access request :2042849237