In the JAVA interview, I asked about the comparison between HIBERNATE and MYBATIS. Here I will make a summary. hibernatemybatis

Source: Internet
Author: User
Tags time in milliseconds

In the JAVA interview, I asked about the comparison between HIBERNATE and MYBATIS. Here I will make a summary. hibernatemybatis

I am a java developer, I have studied hibernate and mybatis. I was mentioned during the java interview and applied it in project practice. Now I want to compare hibernate and mybatis, this makes it easier for everyone to better understand and learn, so that they can be more comfortable in project development. The first aspect: the comparison of development speed in terms of development speed, the real grasp of Hibernate is more difficult than Mybatis. The Mybatis framework is relatively simple and easy to use, but it is also relatively simple. I personally think that to use Mybatis well understood first. Compared with the development speed of the two, we should not only consider the characteristics and performance of the two, but also consider which one is more suitable for project development according to the project requirements, such: there is basically no complex query used in a project, that is, simple addition, deletion, modification, and query. In this way, the efficiency of selecting hibernate is very fast, because the basic SQL statements have been encapsulated, you do not need to write SQL statements at all, which saves a lot of time. However, for a large project, there are many complicated statements, so selecting hibernate is not a good choice, selecting mybatis is much faster and the statement management is more convenient. Aspect 2: comparison of development workload Hibernate and MyBatis both have corresponding code generation tools. You can generate simple and basic DAO layer methods. For advanced queries, Mybatis needs to manually write SQL statements and ResultMap. Hibernate has a good ing mechanism. Developers do not need to care about SQL generation and result ing, so they can focus more on business processes. Aspect 3: for SQL optimization, Hibernate queries all fields in the table, which consumes performance. Hibernate can also write SQL statements to specify the fields to be queried, but this compromises the simplicity of Hibernate development. Mybatis SQL is manually written, so you can specify the query fields as needed. The optimization of Hibernate HQL Statements requires printing out the SQL statements, and many people dislike the Hibernate SQL statements because it is too ugly. MyBatis SQL is manually written, so it is easy to adjust. However, Hibernate has its own log statistics. Mybatis uses Log4j to record logs without logging statistics. Aspect 4: Comparison of Object management Hibernate is a complete object/link ing solution, which provides the object state management function, so that developers do not need to pay attention to the details of the underlying database system. That is to say, compared with the common JDBC/SQL persistence layer solution, which requires SQL statement management, Hibernate adopts a more natural object-oriented perspective to persist data in Java applications. In other words, Hibernate developers should always pay attention to the object state without considering the execution of SQL statements. This part of the details has been properly managed by Hibernate. Developers only need to understand the details when performing system optimization. MyBatis does not provide instructions in this section. You need to manage the object in detail. Aspect 5: the cache mechanism Hibernate caches the Hibernate first-level cache as the Session cache. To use the first-level cache, you need to manage the Session lifecycle. We recommend that you use a Session in an Action. The Session must be strictly managed for the level-1 cache. Hibernate second-level cache is SessionFactory-level cache. SessionFactory caches are divided into built-in caches and external caches. The built-in cache stores data contained in some collection attributes of the SessionFactory object (ing element data and pre-defined SQL statements). For applications, it is read-only. The external cache stores copies of database data, which is similar to the level-1 cache. In addition to using memory as the storage medium, you can also use external storage devices such as hard disks. A second-level cache is called a process-level cache or SessionFactory-level cache. It can be shared by all sessions. Its life cycle is accompanied by the existence and extinction of the SessionFactory life cycle. MyBatis cache MyBatis contains a very powerful query cache feature, which can be easily configured and customized. Many improvements have been made to the cache implementation in MyBatis 3, making it more powerful and easy to configure. By default, cache is not enabled. In addition to the local session cache, it can enhance monetization and process circular dependencies. To enable Level 2 caching, you need to add a line in your SQL ing file: <cache/> literally. The effect of this simple statement is as follows: All select statements in the ing statement file will be cached. All insert, update, and delete statements in the ing statement file refresh the cache. The cache is reclaimed using the Least Recently Used (LRU, Least Recently Used) algorithm. The cache is not refreshed in any chronological order according to the schedule (such as no Flush Interval, no refresh Interval. The cache stores the 1024 references of the list set or object (no matter what the query method returns. The cache is regarded as a read/write (readable/writable) cache, meaning that object retrieval is not shared and can be safely modified by the caller, without interfering with the potential changes made by other callers or threads. All these attributes can be modified through the attributes of the cached element. For example: <cache eviction = "FIFO" flushInterval = "60000" size = "512" readOnly = "true"/> this more advanced configuration creates a FIFO cache, refresh every 60 seconds, save the number of result objects or 512 references to the list, and the returned objects are considered read-only. Therefore, changing them between callers in different threads may cause a conflict. Available revocation policies are: LRU-least recently used: objects that have been removed for the longest time are not used. FIFO-FIFO: removes objects in the cache order. SOFT-SOFT reference: removes Objects Based on the Garbage Collector status and SOFT reference rules. WEAK-WEAK reference: more actively removes Objects Based on the Garbage Collector status and WEAK reference rules. FlushInterval can be set to any positive integer, and they represent a reasonable period of time in milliseconds. By default, It is not set, that is, there is no refresh interval, and the cache is refreshed only when the statement is called. Size can be set to any positive integer. Remember 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) attribute can be set to true or false. The read-only cache returns the same instance of the slow storage object to all callers. Therefore, these objects cannot be modified. This provides an important performance advantage. A readable cache returns a copy of the cached object (serialized ). This is slow but secure, so the default value is false. Similarities: in addition to the default caching mechanism, the secondary cache of Hibernate and Mybatis can implement your own cache or create an adapter for other third-party caching schemes to completely overwrite the cache behavior. Difference: the second-level cache configuration of Hibernate is configured in the configuration file generated by SessionFactory, and then configured in the specific table-object ing. The second-level cache configuration of MyBatis is configured in detail in each specific table-object ing, so that different cache mechanisms can be customized for different tables. In addition, Mybatis can share the same Cache configuration and instance in the namespace, which is implemented through Cache-ref. Comparison between the two: Because Hibernate has a good management mechanism for query objects, users do not need to care about SQL. Therefore, if dirty data is generated when the second-level cache is used, an error is reported and a prompt is displayed. In this regard, MyBatis must be especially careful when using the second-level cache. If the scope of the data update operation cannot be fully determined, avoid blind use of the Cache. Otherwise, the appearance of dirty data will pose a great risk to the normal operation of the system. Aspect 6: To sum up, you can go to the major java forums to see the similarities: Both Hibernate and MyBatis can generate SessionFactory from the XML configuration file through SessionFactoryBuider, and then generate Session from SessionFactory, the Session is used to start the execution of transactions and SQL statements. SessionFactoryBuider, SessionFactory, and Session have similar lifecycles. Both Hibernate and MyBatis support JDBC and JTA transaction processing. Mybatis advantage MyBatis can perform more detailed SQL optimization and reduce query fields. MyBatis is easy to master, while Hibernate has a high threshold. Hibernate has advantages. The DAO layer of Hibernate is easier to develop than MyBatis, and Mybatis requires maintenance of SQL and result ing. Hibernate provides better object maintenance and caching than MyBatis. It is easier to maintain the added, deleted, modified, and queried objects. The Hibernate database has good portability and the MyBatis database has poor portability. Different databases need to write different SQL statements. Hibernate has a better level-2 caching mechanism and can use third-party caching. The cache mechanism provided by MyBatis is poor. Others summarize that Hibernate has powerful functions, good database independence, and strong O/R ing capabilities. If you are very proficient in Hibernate and have encapsulated Hibernate properly, the code for the entire persistence layer of your project will be quite simple, with few code to be written, fast development, and great. The disadvantage of Hibernate is that it has a low learning threshold and a higher level of proficiency. In addition, how can we design O/R ing and balance the performance and object models, and how to make good use of Hibernate requires your experience and capabilities. IBATIS is easy to learn and use. It provides the Automatic Object binding function for database queries and extends the good experience in SQL. For projects that do not have such high requirements on object models, perfect. The disadvantage of iBATIS is that the framework is still relatively simple and the functions are still missing. Although the data binding code is simplified, the entire underlying database query needs to be written by itself, and the workload is also large, it is not easy to adapt to quick database modification. I am a java developer, I have studied hibernate and mybatis. I was mentioned during the java interview and applied it in project practice. Now I want to compare hibernate and mybatis, this makes it easier for everyone to better understand and learn, so that they can be more comfortable in project development. The first aspect: the comparison of development speed in terms of development speed, the real grasp of Hibernate is more difficult than Mybatis. The Mybatis framework is relatively simple and easy to use, but it is also relatively simple. I personally think that to use Mybatis well understood first. Compared with the development speed of the two, we should not only consider the characteristics and performance of the two, but also consider which one is more suitable for project development according to the project requirements, such: there is basically no complex query used in a project, that is, simple addition, deletion, modification, and query. In this way, the efficiency of selecting hibernate is very fast, because the basic SQL statements have been encapsulated, you do not need to write SQL statements at all, which saves a lot of time. However, for a large project, there are many complicated statements, so selecting hibernate is not a good choice, selecting mybatis is much faster and the statement management is more convenient. Aspect 2: comparison of development workload Hibernate and MyBatis both have corresponding code generation tools. You can generate simple and basic DAO layer methods. For advanced queries, Mybatis needs to manually write SQL statements and ResultMap. Hibernate has a good ing mechanism. Developers do not need to care about SQL generation and result ing, so they can focus more on business processes. Aspect 3: for SQL optimization, Hibernate queries all fields in the table, which consumes performance. Hibernate can also write SQL statements to specify the fields to be queried, but this compromises the simplicity of Hibernate development. Mybatis SQL is manually written, so you can specify the query fields as needed. The optimization of Hibernate HQL Statements requires printing out the SQL statements, and many people dislike the Hibernate SQL statements because it is too ugly. MyBatis SQL is manually written, so it is easy to adjust. However, Hibernate has its own log statistics. Mybatis uses Log4j to record logs without logging statistics. Aspect 4: Comparison of Object management Hibernate is a complete object/link ing solution, which provides the object state management function, so that developers do not need to pay attention to the details of the underlying database system. That is to say, compared with the common JDBC/SQL persistence layer solution, which requires SQL statement management, Hibernate adopts a more natural object-oriented perspective to persist data in Java applications. In other words, Hibernate developers should always pay attention to the object state without considering the execution of SQL statements. This part of the details has been properly managed by Hibernate. Developers only need to understand the details when performing system optimization. MyBatis does not provide instructions in this section. You need to manage the object in detail. Aspect 5: the cache mechanism Hibernate caches the Hibernate first-level cache as the Session cache. To use the first-level cache, you need to manage the Session lifecycle. We recommend that you use a Session in an Action. The Session must be strictly managed for the level-1 cache. Hibernate second-level cache is SessionFactory-level cache. SessionFactory caches are divided into built-in caches and external caches. The built-in cache stores data contained in some collection attributes of the SessionFactory object (ing element data and pre-defined SQL statements). For applications, it is read-only. The external cache stores copies of database data, which is similar to the level-1 cache. In addition to using memory as the storage medium, you can also use external storage devices such as hard disks. A second-level cache is called a process-level cache or SessionFactory-level cache. It can be shared by all sessions. Its life cycle is accompanied by the existence and extinction of the SessionFactory life cycle. MyBatis cache MyBatis contains a very powerful query cache feature, which can be easily configured and customized. Many improvements have been made to the cache implementation in MyBatis 3, making it more powerful and easy to configure. By default, cache is not enabled. In addition to the local session cache, it can enhance monetization and process circular dependencies. To enable Level 2 caching, you need to add a line in your SQL ing file: <cache/> literally. The effect of this simple statement is as follows: All select statements in the ing statement file will be cached. All insert, update, and delete statements in the ing statement file refresh the cache. The cache is reclaimed using the Least Recently Used (LRU, Least Recently Used) algorithm. The cache is not refreshed in any chronological order according to the schedule (such as no Flush Interval, no refresh Interval. The cache stores the 1024 references of the list set or object (no matter what the query method returns. The cache is regarded as a read/write (readable/writable) cache, meaning that object retrieval is not shared and can be safely modified by the caller, without interfering with the potential changes made by other callers or threads. All these attributes can be modified through the attributes of the cached element. For example: <cache eviction = "FIFO" flushInterval = "60000" size = "512" readOnly = "true"/> this more advanced configuration creates a FIFO cache, refresh every 60 seconds, save the number of result objects or 512 references to the list, and the returned objects are considered read-only. Therefore, changing them between callers in different threads may cause a conflict. Available revocation policies are: LRU-least recently used: objects that have been removed for the longest time are not used. FIFO-FIFO: removes objects in the cache order. SOFT-SOFT reference: removes Objects Based on the Garbage Collector status and SOFT reference rules. WEAK-WEAK reference: more actively removes Objects Based on the Garbage Collector status and WEAK reference rules. FlushInterval can be set to any positive integer, and they represent a reasonable period of time in milliseconds. By default, It is not set, that is, there is no refresh interval, and the cache is refreshed only when the statement is called. Size can be set to any positive integer. Remember 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) attribute can be set to true or false. The read-only cache returns the same instance of the slow storage object to all callers. Therefore, these objects cannot be modified. This provides an important performance advantage. A readable cache returns a copy of the cached object (serialized ). This is slow but secure, so the default value is false. Similarities: in addition to the default caching mechanism, the secondary cache of Hibernate and Mybatis can implement your own cache or create an adapter for other third-party caching schemes to completely overwrite the cache behavior. Difference: the second-level cache configuration of Hibernate is configured in the configuration file generated by SessionFactory, and then configured in the specific table-object ing. The second-level cache configuration of MyBatis is configured in detail in each specific table-object ing, so that different cache mechanisms can be customized for different tables. In addition, Mybatis can share the same Cache configuration and instance in the namespace, which is implemented through Cache-ref. Comparison between the two: Because Hibernate has a good management mechanism for query objects, users do not need to care about SQL. Therefore, if dirty data is generated when the second-level cache is used, an error is reported and a prompt is displayed. In this regard, MyBatis must be especially careful when using the second-level cache. If the scope of the data update operation cannot be fully determined, avoid blind use of the Cache. Otherwise, the appearance of dirty data will pose a great risk to the normal operation of the system. Aspect 6: To sum up, you can go to the major java forums to see the similarities: Both Hibernate and MyBatis can generate SessionFactory from the XML configuration file through SessionFactoryBuider, and then generate Session from SessionFactory, the Session is used to start the execution of transactions and SQL statements. SessionFactoryBuider, SessionFactory, and Session have similar lifecycles. Both Hibernate and MyBatis support JDBC and JTA transaction processing. Mybatis advantage MyBatis can perform more detailed SQL optimization and reduce query fields. MyBatis is easy to master, while Hibernate has a high threshold. Hibernate has advantages. The DAO layer of Hibernate is easier to develop than MyBatis, and Mybatis requires maintenance of SQL and result ing. Hibernate provides better object maintenance and caching than MyBatis. It is easier to maintain the added, deleted, modified, and queried objects. The Hibernate database has good portability and the MyBatis database has poor portability. Different databases need to write different SQL statements. Hibernate has a better level-2 caching mechanism and can use third-party caching. The cache mechanism provided by MyBatis is poor. Others summarize that Hibernate has powerful functions, good database independence, and strong O/R ing capabilities. If you are very proficient in Hibernate and have encapsulated Hibernate properly, the code for the entire persistence layer of your project will be quite simple, with few code to be written, fast development, and great. The disadvantage of Hibernate is that it has a low learning threshold and a higher level of proficiency. In addition, how can we design O/R ing and balance the performance and object models, and how to make good use of Hibernate requires your experience and capabilities. IBATIS is easy to learn and use. It provides the Automatic Object binding function for database queries and extends the good experience in SQL. For projects that do not have such high requirements on object models, perfect. The disadvantage of iBATIS is that the framework is still relatively simple and the functions are still missing. Although the data binding code is simplified, the entire underlying database query needs to be written by itself, and the workload is also large, it is not easy to adapt to quick database modification.

 

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.