Chapter One. hibernate and MyBatis
Hibernate is currently the most popular O/R mapping Framework, born in Sf.net and now part of JBoss. Mybatis is another excellent O/R mapping framework. Currently belongs to a sub-project of Apache.
1.1 Hibernate Profile
Hibernate provides a more complete encapsulation of the database structure, and Hibernate's O/R mapping implements the mapping between Pojo and database tables, as well as the automatic generation and execution of SQL. Programmers often only need to define the Pojo to the database table mapping relationship, you can use Hibernate provides methods to complete the persistence layer operation. Programmers do not even need to be proficient in SQL, and HIBERNATE/OJB automatically generates the corresponding SQL and invokes the JDBC interface to execute according to the established storage logic.
1.2 MyBatis Introduction
The focus of IBATIS is the mapping relationship between Pojo and SQL. The parameters that SQL requires and the returned result fields are then mapped to the specified Pojo by mapping the configuration file. IBATIS is an ORM implementation of "SQL Mapping" relative to Hibernate "O/R".
Chapter Two. Development Comparison
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.
Development Community
Hibernate and MyBatis are popular persistent layer development frameworks, but the hibernate development community is relatively lively, supported by a number of tools, updated quickly, the current highest version of 4.1.8. The mybatis is relatively calm, with fewer tools and currently the highest version of 3.2.
Development effort
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.
Chapter Three .comparison of the system tuning
Hibernate's Tuning Solution
Develop a reasonable caching strategy;
Use lazy loading features as much as possible;
The use of reasonable session management mechanism;
Use batch fetch, set reasonable batch processing parameters (Batch_size);
Make a reasonable design of O/R mapping
MyBatis Tuning Solution
MyBatis in session and Hibernate session life cycle is consistent, also need a reasonable session management mechanism. The MyBatis also has a two-level caching mechanism. MyBatis can perform detailed SQL optimization design.
SQL optimization aspects
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.
Extensibility aspects
Hibernate's association with a specific database can only be configured in an XML file, and all HQL statements are not relevant to the database in use and are well-ported. All SQL statements in the MyBatis project are dependent on the database used, so support for different database types is not good.
Chapter Four. object management and grasping strategy
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.
Crawl strategy
Hibernate has a good mechanism for capturing entity-related objects. For each correlation relationship can be set in detail whether delay loading, and provide related crawl, query fetch, subquery fetch, batch crawl four modes. It is configured and processed in detail.
The deferred load for MyBatis is globally configured.
Chapter Five. cache mechanism comparison
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:
All SELECT statements in the map statement file 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 1024 references to a list collection or object, regardless of what the Query method returns.
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= "Up" 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:
lru– Least Recently used: Removes 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 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.
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 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 of the two
Because Hibernate has a good management mechanism for querying 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.
Sixth chapter comparison between Hibernate and MyBatis
The two are the same.
Both 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.
Qing Yong
Links: http://www.imooc.com/article/14693
Source: MU-Class Network
The difference between hibernate and Mybatis/ibatis, why choose? Go