Java interview Asked about the comparison between Hibernate and MyBatis, here to summarize (turn)

Source: Internet
Author: User

Hibernate and MyBatis have studied, in the Java interview is also mentioned asked, in the project practice has also been used, now to hibernate and MyBatis to do a contrast, so that everyone better understanding and learning, so that they do more handy in the project.

The first aspect: comparison of development speed

In terms of development speed, Hibernate's real mastery is more difficult than mybatis. The MyBatis framework is relatively simple and easy to get started with, but also relatively rudimentary. Personally think to use good mybatis or first to understand good hibernate first.

Compared to the development speed of the two, not only to consider the characteristics and performance of the two, but also according to the needs of the project to consider which one is more suitable for project development, such as: a project used in the complex query basically no, is simple to delete and change, so choose hibernate efficiency quickly, Because the basic SQL statements have been encapsulated, there is no need for you to write SQL statements, which saves a lot of time, but for a large project, more complex statements, so to choose Hibernate is not a good choice, choose MyBatis will speed up a lot, And the management of the statement is also more convenient.

The second aspect: comparison of development workload

Both hibernate and MyBatis have corresponding code generation tools. A simple basic DAO layer method can be generated. For advanced queries, MyBatis needs to write SQL statements manually, as well as Resultmap. Hibernate has a good mapping mechanism, and developers don't have to worry about SQL generation and result mapping, and can focus more on business processes.

Third aspect: SQL optimization

Hibernate queries query all the fields in the table, which can be a performance drain. Hibernate can also write its own SQL to specify the fields that need to be queried, but this undermines the simplicity of hibernate development. The MyBatis SQL is written manually, so you can specify the fields of the query on demand.

The tuning of Hibernate HQL statements requires that SQL be printed out, and Hibernate's SQL is too ugly for many people to dislike. MyBatis's SQL is written manually, so it's easy to adjust. However, hibernate has its own log statistics. The mybatis itself does not have log statistics and uses log4j to log records.

The four aspects: the comparison of object management

Hibernate is a complete object/relational mapping solution that provides the functionality of object state Management (management), which eliminates the need for developers to heed the details of the underlying database system. That is, hibernate uses a more natural object-oriented perspective to persist data in Java applications relative to the common Jdbc/sql persistence layer scenarios in which SQL statements need to be managed.

In other words, developers using Hibernate should always focus on the state of the object, regardless of the execution of the SQL statement. This part of the details has been managed by Hibernate and only needs to be understood by the developer when it comes to tuning the system performance. And MyBatis in this one. There is no document stating that the user needs to manage the object in detail.
Part V: Caching mechanisms

Hibernate cache

Hibernate buffer is the session cache, the use of a good first-level cache will need to manage the session life cycle. We recommend that you use a session in an action action. First-level caching requires strict management of the session.

Hibernate level Two caches are sessionfactory-level caches. The sessionfactory cache is divided into built-in caches and external caches. The built-in cache holds the data contained in some of the collection properties of the Sessionfactory object (mapping elements and predetermined SQL statements, etc.), which is read-only for the application. The external cache holds a copy of the database data, which acts like a first-level cache. Level two cache in addition to memory as a storage medium, you can also choose the hard disk and other external storage devices. A secondary cache, called a process-level cache or a sessionfactory-level cache, can be shared by all sessions, and its life cycle is accompanied by the existence and extinction of the sessionfactory life cycle.

MyBatis Cache

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

By default, the cache is not turned on, and in addition to the local session cache, it is also necessary to enhance the monetization and handle the cyclic dependencies. To turn on level two caching, 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 map statement file will be cached.
    2. All insert,update and DELETE statements in the map statement file flush the cache.
    3. The cache is retracted using the Least recently used (LRU, least recently used) algorithm.
    4. Depending on the schedule (such as no flush Interval, there is no refresh interval), the cache is not refreshed in any chronological order.
    5. The cache stores 1024 references to a list collection or object, regardless of what the Query method returns.
    6. The cache is considered to be a read/write (readable/writable) cache, meaning that object retrieval is 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 the attributes of the element.

For example: <cache eviction= "FIFO" flushinterval= "60000″size=" 512″readonly= "true"/>

This more advanced configuration creates a FIFO cache, refreshes every 60 seconds, saves 512 references to the resulting object or list, and the returned objects are considered read-only, so modifying them between callers in different threads can cause conflicts. The available retract policies are, by default, LRU:

    1. lru– Least Recently used: Removes objects that are not used for the longest time.
    2. fifo– FIFO: Removes them by the order in which they are entered in the cache.
    3. soft– Soft Reference: Removes objects based on the garbage collector state and soft reference rules.
    4. weak– Weak references: More aggressively remove objects based on the garbage collector state and weak reference rules.

The Flushinterval (refresh interval) can be set to any positive integer, and they represent a reasonable millisecond in the form of a time period. The default is not set, that is, there is no refresh interval, and the cache simply refreshes when the statement is invoked.

The 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 for the environment you are running. The default value is 1024.

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

The same point: Hibernate and MyBatis Level two caches, in addition to using the system's default caching mechanism, can completely overwrite the caching behavior by implementing your own cache or by creating an adapter for other third-party caching scenarios.

The difference: Hibernate's Level Two cache configuration is configured in detail in the configuration file generated by Sessionfactory, and then configured in the specific table-object map to be that cache.

MyBatis's Level Two cache configuration is configured in detail in each specific table-object map, so that different caching mechanisms can be customized for different tables. And MyBatis can share the same cache configuration and instance in the namespace, implemented through Cache-ref.

Comparison: Because Hibernate has a good management mechanism for query objects, users don't have to worry about SQL. Therefore, if dirty data appears when using level two cache, the system will report an error and prompt.

In this regard, MyBatis requires special care when using level two caches. Avoid the blind use of the cache if you cannot fully determine the scope of the data update operation. Otherwise, the appearance of dirty data will bring great hidden trouble to the normal operation of the system.

Part VI: summary

For the summary, you can go to the major Java forum to see

The same point: Hibernate and MyBatis can be generated sessionfactory by Sessionfactorybuider from an XML configuration file and then generated by the sessionfactory session, Finally, the session is opened to execute the transaction and SQL statements. The life cycle of the sessionfactorybuider,sessionfactory,session is similar.
    • Both Hibernate and MyBatis support JDBC and JTA transaction processing.
MyBatis Advantages
    • MyBatis can perform more granular SQL optimizations and can reduce query fields.
    • The MyBatis is easy to master, while the hibernate threshold is higher.
Hibernate Advantage
    • Hibernate's DAO layer development is simpler than mybatis, and mybatis needs to maintain SQL and result mappings.
    • Hibernate to the object maintenance and caching is better than MyBatis, to delete and change the object of the maintenance to be convenient.
    • Hibernate database portability is very good, MyBatis database portability is bad, different databases need to write different SQL.
    • Hibernate has a better level two caching mechanism and can use third-party caches. The caching mechanism provided by the MyBatis itself is poor.
Others summary
      • Hibernate is powerful, database Independent, O/R mapping is strong, if you are very proficient in hibernate, and hibernate is properly encapsulated, then your project the entire persistence layer code will be quite simple, need to write very little code, development speed, very cool.
      • Hibernate's disadvantage is that the learning threshold is not low, to master the threshold is higher, and how to design O/R mapping, how to balance between performance and object model, and how to use good hibernate needs your experience and ability are very strong.
      • Ibatis is simple to learn, which provides automatic object binding for database queries, and a good experience in SQL usage, perfect for projects that don't have that high object model requirement.
      • Ibatis The disadvantage is that the framework is still relatively humble, the function is still missing, although simplifying the data binding code, but the entire underlying database query is actually to write their own, the workload is relatively large, and it is not easy to adapt to rapid database modification.

Http://www.cnblogs.com/inspurhaitian/p/4647485.html

Java interview Asked about the comparison between Hibernate and MyBatis, here to summarize (turn)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.