Comparison of hibernate and mybatis

The first aspect: the comparison of development speed

In terms of development speed, Hibernate is harder to master than Mybatis. The Mybatis framework is relatively simple and easy to use, but it is also relatively simple. Personally think that to use Mybatis well, you must first understand Hibernate.

Compared with the development speed of the two, it is not only necessary to consider the characteristics and performance of the two, but also to consider which one is more suitable for project development according to the project requirements. For example, there are basically no complex queries used in a project, it is simple In this way, the efficiency of selecting hibernate is very fast, because the basic SQL statements have been encapsulated, and you do not need to write SQL statements at all, which saves a lot of time, but for a large project, complex statements If there are more, it is not a good choice to choose hibernate. Choosing mybatis will speed up a lot, and the management of statements will be more convenient.

The second aspect: the comparison of development workload

Both Hibernate and MyBatis have corresponding code generation tools. Simple and basic DAO layer methods can be generated. For advanced queries, Mybatis needs to manually write SQL statements and ResultMap. Hibernate has a good mapping mechanism, and developers do not need to care about SQL generation and result mapping, and can focus more on business processes.

The third aspect: sql optimization

Hibernate's query will query all fields in the table, which will cost performance. Hibernate can also write its own SQL to specify the fields to be queried, but this destroys the simplicity of Hibernate development. The SQL of Mybatis is written manually, so the fields of the query can be specified as required.

The tuning of Hibernate HQL statements needs to print out the SQL, and Hibernate's SQL is disliked by many people because it is too ugly. MyBatis's SQL is written manually, so it is easy to adjust. But Hibernate has its own log statistics. Mybatis itself does not have log statistics and uses Log4j for logging.

The fourth aspect: the comparison of object management

Hibernate is a complete object/relational mapping solution, which provides object state management (state management) functions, so that developers no longer need to worry about 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.

In other words, developers using Hibernate should always focus on the state of the object, not the execution of SQL statements. This part of the details has been taken care of by Hibernate, and only developers need to understand when tuning system performance. However, MyBatis has no documentation in this section, and users need to manage the objects themselves in detail.
Fifth aspect: caching mechanism

Hibernate cache

Hibernate's first-level cache is a session cache. To make good use of the first-level cache, it is necessary to manage the life cycle of the session. It is recommended to use a Session in an Action operation. The first level cache needs to strictly manage the session.

The Hibernate second-level cache is a SessionFactory-level cache. SessionFactory's cache is divided into built-in cache and external cache. The built-in cache stores the data (mapping element data and predetermined SQL statements, etc.) contained in some collection properties of the SessionFactory object, which is read-only for the application. The external cache stores a copy of the database data, and its function is similar to that of the first-level cache. In addition to using the memory as the storage medium, the second-level cache can also use external storage devices such as hard disks. The second-level cache is called a process-level cache or a SessionFactory-level cache, which can be shared by all sessions, and its life cycle exists and dies with the life cycle of the SessionFactory.

MyBatis cache

MyBatis includes a very powerful query caching feature that can be configured and customized very easily. Many improvements to the cache implementation in MyBatis 3 have been implemented, making it more powerful and easier to configure.

By default, caching is not enabled, except for local session caching, which can enhance monetization and is also necessary to handle circular dependencies. To enable L2 cache, you need to add a line to your SQL mapping file: <cache/>

Literally that's it. The effect of this simple statement is as follows:

  1. All select statements in the mapped statement file will be cached.
  2. All insert, update and delete statements in the mapped statement file flush the cache.
  3. The cache will be reclaimed using the Least Recently Used (LRU, least recently used) algorithm.
  4. According to the schedule (eg no Flush Interval, no flush interval), the cache will not be flushed in any chronological order.
  5. The cache stores 1024 references to list collections or objects (whatever the query method returns).
  6. The cache is treated as a read/write cache, meaning that object retrievals are not shared and can be safely modified by the caller without interfering with potential modifications made by other callers or threads.

All of these properties can be modified by caching element properties.

比如: <cache  eviction=”FIFO”  flushInterval=”60000″  size=”512″  readOnly=”true”/>

This more advanced configuration creates a FIFO buffer that is flushed every 60 seconds, holding 512 references to the result object or list, and the returned object is considered read-only, so between callers in different threads Modifying them will cause conflicts. The available eviction strategies are, the default is LRU:

  1. LRU - Least Recently Used: Removes objects that have not been used for the longest time.
  2. FIFO - First In First Out: Remove objects in the order in which they entered the cache.
  3. SOFT - Soft Reference: Remove objects based on garbage collector state and soft reference rules.
  4. WEAK - Weak References: More aggressive removal of objects based on garbage collector state and weak reference rules.

flushInterval can be set to any positive integer, and they represent a reasonable time period in milliseconds. The default is not set, that is, there is no refresh interval, the cache is only refreshed when the statement is called.

size (number of references) can be set to any positive integer, keeping in mind the number of objects you cache and the number of available memory resources in your runtime environment. The default value is 1024.

The readOnly (read-only) property can be set to true or false. A read-only cache will return the same instance of the cache object to all callers. Therefore these objects cannot be modified. This provides a significant performance advantage. Read-write caches return a copy of the cached object (via serialization). This is slower, but safe, so the default is false.

The same point: In addition to using the default cache mechanism of the system, the second-level cache of Hibernate and Mybatis can completely override the cache behavior by implementing your own cache or creating adapters for other third-party cache solutions.

The difference: Hibernate's second-level cache configuration is configured in detail in the configuration file generated by the SessionFactory, and then configured in the specific table-object mapping.

The second-level cache configuration of MyBatis 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 of the two: Because Hibernate has a good management mechanism for query objects, users do not need to care about SQL. Therefore, if there is dirty data when using the second-level cache, the system will report an error and prompt.

In this regard, MyBatis requires special care when using the second-level cache. If the scope of the data update operation cannot be completely determined, avoid the blind use of Cache. Otherwise, the appearance of dirty data will bring great hidden danger to the normal operation of the system.

Aspect 6: Summary

For the summary, you can go to the major java forums to have a look

The same point: Hibernate and MyBatis can generate SessionFactory from XML configuration file through SessionFactoryBuider, then SessionFactory generates Session, and finally Session starts to execute transactions and SQL statements. Among them, the life cycle of SessionFactoryBuider, SessionFactory, and Session is similar.

  • Both Hibernate and MyBatis support JDBC and JTA transactions.

Mybatis advantage

  • MyBatis can perform more detailed SQL optimization and reduce 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 needs to maintain SQL and result mapping.
  • Hibernate's maintenance and caching of objects is better than MyBatis, and it is more convenient to maintain objects that are added, deleted, 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 cache mechanism and can use third-party caches. The caching mechanism provided by MyBatis itself is not good.

others summarize

  • Hibernate is powerful, database-independent, and has strong O/R mapping capabilities. If you are quite proficient in Hibernate and have properly encapsulated Hibernate, the entire persistence layer code of your project will be quite simple, and you need to write very little code. , the development speed is very fast, very cool.
  • The disadvantage of Hibernate is that the learning threshold is not low, the threshold for mastering is higher, and how to design O/R mapping, how to balance performance and object model, and how to make good use of Hibernate requires your experience and ability are very strong Just do it.
  • iBATIS is easy to get started, and it can be used immediately after learning. It provides automatic object binding for database queries, and continues the good experience of using SQL. It is perfect for projects that do not have such high object model requirements.
  • The disadvantage of iBATIS is that the framework is still relatively simple and functions are still missing. Although the data binding code is simplified, the entire underlying database query actually needs to be written by itself, the workload is relatively large, and it is not easy to adapt to rapid database modification.

Reprinted from: http://www.cnblogs.com/inspurhaitian/p/4647485.html

Guess you like

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