In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the advantages of Hibernate and MyBatis". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the advantages of Hibernate and MyBatis"?
Hibernate is the very popular Oamp R mapping framework, which was born in sf.net and is now part of Jboss. Mybatis is another excellent Oamp R mapping framework. Currently belongs to a subproject of apache.
1.1 introduction to Hibernate
Hibernate provides a relatively complete encapsulation of the database structure. Hibernate's Omax R Mapping realizes the mapping between POJO and database tables, as well as the automatic generation and execution of SQL. Programmers often only need to define the mapping relationship between POJO and database tables, and can complete the persistence layer operation through the methods provided by Hibernate. Programmers do not even need to be proficient in SQL, Hibernate/OJB will automatically generate the corresponding SQL according to the storage logic and call the JDBC interface to execute it.
1.2 introduction to MyBatis
The focus of iBATIS lies in the mapping relationship between POJO and SQL. Then the parameters required by SQL and the returned result fields are mapped to the specified POJO through the mapping configuration file. IBATIS is an ORM implementation of "Sql Mapping" as opposed to hibernate "Obind R".
Development speed
The real mastery of Hibernate is more difficult than Mybatis. The Mybatis framework is relatively simple and easy to use, but it is also relatively crude. Personally, if you want to make good use of Mybatis, you must first understand Hibernate.
Development community
Both Hibernate and Mybatis are popular persistence layer development frameworks, but the Hibernate development community is relatively lively, supports more tools, and is updated quickly, with the current maximum version 4.1.8. Mybatis, on the other hand, is relatively calm and has fewer tools, with the current maximum version 3.2.
Development workload
Both Hibernate and MyBatis have corresponding code generation tools. You can generate simple and basic DAO layer methods.
For advanced queries, Mybatis needs to manually write SQL statements, as well as ResultMap. While Hibernate has a good mapping mechanism, developers do not need to care about SQL generation and result mapping, and can focus more on business processes.
Tuning Scheme of Hibernate
Develop a reasonable caching strategy
Try to use the delayed loading feature
Adopt reasonable Session management mechanism
Use batch fetching and set reasonable batch parameters (batch_size)
Carry on the reasonable Ohammer R mapping design.
Mybatis tuning scheme
MyBatis is consistent with Hibernate's Session life cycle in terms of Session, and a reasonable Session management mechanism is also needed. MyBatis also has a two-tier cache mechanism. MyBatis can carry out detailed SQL optimization design.
SQL optimization
Hibernate's query queries all the fields in the table, which has a performance penalty. Hibernate can also write its own SQL to specify the fields to be queried, but this undermines the simplicity of Hibernate development. Mybatis's SQL is written manually, so you can specify the fields of the query as needed.
Tuning the Hibernate HQL statement requires printing out the SQL, while the SQL of Hibernate is rejected by many people because it is too ugly. The SQL of MyBatis 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.
Scalability
The association between Hibernate and a specific database only needs to be configured in the XML file, and all HQL statements have nothing to do with the specific database, so it has good portability. All SQL statements in the MyBatis project depend on the database used, so different database types are not supported well.
Object management
Hibernate is a complete object / relational mapping solution, which provides the function of object state management (state management), so that developers no longer need to pay attention to the details of the underlying database system. In other words, compared with the common JDBC/SQL persistence layer scenarios that need to manage SQL statements, 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 objects, regardless of the execution of SQL statements. These details are already in the hands of Hibernate and need to be understood only when developers are tuning the performance of the system.
While MyBatis has no documentation in this section, users need to manage the objects themselves in detail.
Grasping strategy
Hibernate has a good mechanism for grasping entity-related objects. For each association relationship, whether to delay loading can be set in detail, and four modes of association fetching, query fetching, subquery fetching and batch fetching are provided. It is configured and handled in detail.
The delayed loading of Mybatis is configured globally.
Hibernate caching
Hibernate first-level cache is Session cache. To make good use of first-level cache, you need to manage the life cycle of Session. It is recommended that you use a Session in an Action operation. First-level caching requires strict management of Session.
The Hibernate secondary cache is a SessionFactory-level cache. The cache of SessionFactory is divided into built-in cache and external cache. The built-in cache stores the data contained in some collection properties of the SessionFactory object (mapping element data, scheduled SQL statements, etc.), which is read-only for the application. What is stored in the external cache is a copy of the database data, which is similar to the primary cache. In addition to using memory as the storage medium, the secondary cache can also choose external storage devices such as hard drives. The second-level cache is called process-level cache or SessionFactory-level cache, it can be shared by all session, and its life cycle exists and dies with the life cycle of SessionFactory.
MyBatis caching
MyBatis includes a very powerful query caching feature that can be easily configured and customized. Many improvements to the caching implementation in MyBatis 3 have been implemented, making it more powerful and easier to configure.
Caching is not turned on by default, in addition to local session caching, cashing can be enhanced and it is necessary to handle circular dependencies. To turn on secondary caching, you need to add a line to your SQL mapping file:
Literally, that's what it is. The effect of this simple statement is as follows:
All select statements in the mapping statement file will be cached.
All insert,update and delete statements in the mapping statement file flush the cache.
The cache is retrieved using the Least Recently Used (LRU, the 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 1024 references to a list collection or object, no matter what the query method returns.
The cache is treated as a read/write (readable / writable) cache, meaning that object retrieval is not shared and can be safely modified by callers without interfering with potential modifications made by other callers or threads.
All of these attributes can be modified by caching the properties of the element.
For example:
This more advanced configuration creates a FIFO cache and refreshes it every 60 seconds, saving 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 lead to conflicts. The available recall policies are, by default, LRU:
LRU-least recently used: remove objects that have not been used for the longest time.
FIFO-FIFO: removes objects in the order in which they enter the cache.
SOFT-soft reference: removes objects based on garbage collector state and soft reference rules.
WEAK-weak references: more actively remove objects based on garbage collector state and weak reference rules.
FlushInterval (refresh interval) can be set to any positive integer, and they represent a reasonable millisecond period of time. The default is not set, that is, there is no refresh interval, and 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 memory resources available in your runtime environment. The default value is 1024.
The readOnly (read-only) property can be set to true or false. The read-only cache returns the same instance of the cache object to all callers. Therefore, these objects cannot be modified. This provides an important performance advantage. The read-write cache returns a copy of the cache object (by serialization). This is slower, but secure, so the default is false.
Identical point
In addition to using the system default caching mechanism, Hibernate and Mybatis secondary caching can completely override caching behavior by implementing your own cache or creating adapters for other third-party caching schemes.
Differences
The secondary cache configuration of Hibernate is configured in detail in the configuration file generated by SessionFactory, and then that cache is configured in the specific table-object mapping.
The secondary 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 instances in the namespace, 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 occurs when using the secondary cache, the system will report an error and prompt.
In this respect, MyBatis needs to be very careful when using secondary caching. If you can not completely determine the scope of the data update operation, avoid the blind use of Cache. Otherwise, the appearance of dirty data will bring great hidden trouble to the normal operation of the system.
The similarities between the two
Both Hibernate and MyBatis can generate SessionFactory from the XML configuration file through SessionFactoryBuider, then generate Session from SessionFactory, and finally open the execution transaction and SQL statement by Session. The life cycle of SessionFactoryBuider,SessionFactory,Session is similar.
Both Hibernate and MyBatis support JDBC and JTA transactions.
Mybatis advantage
MyBatis allows for more detailed SQL optimization, reducing the number of query fields.
MyBatis is easy to master, but the threshold of Hibernate is higher.
Hibernate advantage
The development of the DAO layer of Hibernate is simpler than that of MyBatis, and Mybatis needs to maintain SQL and result mapping.
Hibernate maintains and caches 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 two-tier caching mechanism and can use third-party caching. The caching mechanism provided by MyBatis itself is not good.
At this point, I believe you have a deeper understanding of "what are the advantages of Hibernate and MyBatis?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 215
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.