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

Source: Internet
Author: User
Tags log4j

 I am a Java developer, hibernate, and MyBatis have studied, have been asked in the Java interview, also used in project practice, Now to hibernate and MyBatis to do a contrast, so that everyone better understanding and learning, so that they do in the project more handy. The first aspect: comparison of development speed in terms of development speed, Hibernate's true 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: the comparison of the development workload 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: The SQL Optimization hibernate query will query all the fields in the table, which will have 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: Object Management Comparison Hibernate is the complete object /relational mapping solution, which provides the functionality of object state Management (management), which eliminates the need for developers to heed the details of the underlying database system. In other words, relative to the common jdbc/SQL Persistence layer scenarios require management of SQL statements, and hibernate uses 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, 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. On the other side: Caching mechanism Hibernate cache Hibernate buffer is the session cache, the use of a good first-level cache will need to manage the session's 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. The MyBatis cache MyBatis contains a very powerful query caching feature, which can be easily configured and customized. MyBatis3Many of the improvements in the cache implementation in are already 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: all of the mappings in the statement fileSelectThe statement will be cached. All insert,update and DELETE statements in the map statement file flush the cache. The cache is retracted using the Least recently used (LRU, least recently used) algorithm. Depending on the schedule (such as no flush Interval, there is no refresh interval), the cache is not refreshed in any chronological order. The cache stores the list collection or objects (regardless of what the Query method returns).1024x768a reference. The cache will be treated as 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= " +″readonly= "true"/>This more advanced configuration creates a FIFO cache, and every -Seconds to refresh, the number of saved result objects or lists +references, and the returned objects are considered read-only, so modifying them between callers in different threads can cause a conflict. The available retract policies are, by default, the least recently used by lru:lru–: Remove objects that are not used for the longest time. fifo– FIFO: Removes them by the order in which they are entered in the cache. soft– Soft Reference: Removes objects based on the garbage collector state and soft reference rules. 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 totrueOrfalse。 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 secure, so the default isfalse. 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. Different points: Hibernate's Level Two cache configuration is configured in detail in the configuration file generated by the sessionfactory, and then in the specific table-The configuration in the object map is the kind of cache. MyBatis's Level Two cache configuration is in each specific table-The object map is configured in detail so that different caching mechanisms can be customized for different tables. And MyBatis can share the same cache configuration and instance in the namespace by cache-ref to implement. 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. Six aspects: summary for the summary, you can go to the major Java forum to see the same point: Hibernate and MyBatis can be sessionfactorybuider by the XML configuration file generated Sessionfactory, The session is then generated by Sessionfactory and the session is then 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. The MyBatis Advantage MyBatis allows for more granular SQL optimizations that can reduce query fields. The MyBatis is easy to master, while the hibernate threshold is higher. The DAO layer development of Hibernate advantage Hibernate is simpler than MyBatis, mybatis need to maintain SQL and result mapping. 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 summarize hibernate is powerful, the database is independent of good, O/R mapping Ability, if you are quite proficient in hibernate, and the appropriate encapsulation of hibernate, then your project the entire persistence layer code will be quite simple, need to write the code is very small, rapid development, 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 performance against the object model, and how to use hibernate well requires your experience and ability to be 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. I am a Java developer, hibernate and MyBatis have studied, in the Java interview has also been mentioned asked, in the project practice has also been applied, now to hibernate and MyBatis to do a contrast, so that everyone better understanding and learning, Make yourself more handy in doing projects. The first aspect: comparison of development speed in terms of development speed, Hibernate's true 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: the comparison of the development workload 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: The SQL Optimization hibernate query will query all the fields in the table, which will have 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. Sq of MyBatisL was written manually, so it was 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 the complete object/relational mapping solution, which provides the functionality of object state Management (management), which eliminates the need for developers to heed the details of the underlying database system. In other words, relative to the common jdbc/SQL Persistence layer scenarios require management of SQL statements, and hibernate uses 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, 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. On the other side: Caching mechanism Hibernate cache Hibernate buffer is the session cache, the use of a good first-level cache will need to manage the session's 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. The MyBatis cache MyBatis contains a very powerful query caching feature, which can be easily configured and customized. MyBatis3Many of the improvements in the cache implementation in are already 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: all of the mappings in the statement fileSelectThe statement will be cached. All insert,update and DELETE statements in the map statement file flush the cache. The cache is retracted using the Least recently used (LRU, least recently used) algorithm. Depending on the schedule (such as no flush Interval, there is no refresh interval), the cache is not refreshed in any chronological order. The cache stores the list collection or objects (regardless of what the Query method returns).1024x768a reference. The cache will be treated as 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= " +″readonly= "true"/>This more advanced configuration creates a FIFO cache, and every -Seconds to refresh, the number of saved result objects or lists +references, and the returned objects are considered read-only, so modifying them between callers in different threads can cause a conflict. The available retract policies are, by default, the least recently used by lru:lru–: Remove objects that are not used for the longest time. fifo– FIFO: Removes them by the order in which they are entered in the cache. soft– Soft Reference: Removes objects based on the garbage collector state and soft reference rules. 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 totrueOrfalse。 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 secure, so the default isfalse. 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. Different points: Hibernate's Level Two cache configuration is configured in detail in the configuration file generated by the sessionfactory, and then in the specific table-The configuration in the object map is the kind of cache. MyBatis's Level Two cache configuration is in each specific table-The object map is configured in detail so that different caching mechanisms can be customized for different tables. And MyBatis can share the same cache configuration and instance in the namespace by cache-ref to implement. 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. Six aspects: summary for the summary, you can go to the major Java forum to see the same point: Hibernate and MyBatis can be sessionfactorybuider by the XML configuration file generated Sessionfactory, The session is then generated by Sessionfactory and the session is then 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. The MyBatis Advantage MyBatis allows for more granular SQL optimizations that can reduce query fields. The MyBatis is easy to master, while the hibernate threshold is higher. The DAO layer development of Hibernate advantage Hibernate is simpler than MyBatis, mybatis need to maintain SQL and result mapping. 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 summarize hibernate is powerful, the database is independent of good, O/R mapping Ability, if you are quite proficient in hibernate, and the appropriate encapsulation of hibernate, then your project the entire persistence layer code will be quite simple, need to write the code is very small, rapid development, 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 performance against the object model, and how to use hibernate well requires your experience and ability to be 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. 

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

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.