I've never used mybatis before, but I know it's an ORM database framework just like hibernate. With the increase in the use of proficiency, found that it differs from Hibernate is very large interested friends through this article to study together
Take advantage of this weekend, the time to put some commonly used techniques to sort out, small series will also update the content of the occasional.
First, briefly introduce the concepts of the two:
Hibernate:hibernate is currently the most popular ORM framework, providing a more complete encapsulation of the database structure.
Mybatis:mybatis is also a very popular ORM framework, the main focus is the Pojo and SQL mapping relationship.
Second, specifically from a few aspects of the difference between the two:
1. The biggest difference between the two:
For simple logic, hibernate and MyBatis have corresponding code generation tools that generate simple, basic DAO layer methods.
For advanced queries, MyBatis needs to write SQL statements manually, as well as Resultmap. And hibernate has a good mapping mechanism, developers need not care about SQL generation and result mapping, can be more focused on business processes.
2. Development difficulty Comparison
Hibernate is more difficult to develop than mybatis. Mainly because the hibernate is more complex and large, the learning cycle is longer.
MyBatis is relatively simple, and MyBatis relies heavily on SQL writing to make developers feel more familiar.
3.sql Writing comparison
MyBatis SQL is written manually, so you can specify the fields of the query as required. However, there is no log statistics, so the use of log4j to log.
Hibernate can also write their own SQL to specify the fields that need to be queried, but this undermines the simplicity of hibernate development. However, hibernate has its own log statistics.
4. Comparison of database extensibility
MyBatis because all SQL is dependent on the database to write, so extensibility, poor mobility.
Hibernate's specific association with the database is in XML, so HQL is not very concerned about exactly what database to use.
5. Caching mechanism comparison
The same point: Hibernate and MyBatis's level two cache in addition to the system default caching mechanism, you can create an adapter to completely overwrite the cache behavior by implementing your own cache or other Third-party caching schemes.
different points: The hibernate level two cache configuration is configured in Sessionfactory generated configuration files, and then configured in the specific table-object mappings as the cache.
The two-level cache configuration for 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 namespaces, implemented through Cache-ref.
comparison: because Hibernate has a good management mechanism for query objects, users need not care about SQL. So if dirty data appears when using level two caching, the system will report an error and prompt.
and MyBatis In this regard, the use of level two caching requires special care. If you can not fully determine the scope of the data update operation, to avoid the blind use of cache. Otherwise, the appearance of dirty data will bring great hidden trouble to the normal operation of the system.
6. Summary:
MyBatis: Compact, convenient, efficient, simple, direct, semi-automatic
Hibernate: Powerful, convenient, efficient, complex, mealy, fully automatic
Both hibernate and MyBatis can be generated by the Sessionfactorybuider XML configuration file Sessionfactory, and then the session is generated by Sessionfactory. Finally, the session opens the execution transaction and the SQL statement.
The advantage of MyBatis is that MyBatis can perform more granular SQL optimizations, reduce query fields, and be easy to master.
The advantage of hibernate is that DAO layer development is simpler than mybatis, and mybatis needs to maintain SQL and result mappings. Database portability is very good, MyBatis database portability is not good, different databases need to write different SQL. There is a better level two caching mechanism that can be used with third-party caching. MyBatis itself provides a poor caching mechanism.
MyBatis:
1. Easy to get started, that is, learning is used to provide database query automatic object binding function, but also the continuation of a good experience in SQL, for less than the high object model requirements of the project, is quite perfect.
2. More granular SQL optimization can be done to reduce the query field.
3. The disadvantage is that the framework is still relatively simple, the function is still missing, although the data binding code is simplified, but the entire underlying database query is actually to write their own, the workload is relatively large, and is not easy to adapt to rapid database modification.
4. The level two caching mechanism is poor.
Hibernate:
1. Powerful, database-independent, O/R mapping ability, if you are quite proficient in the hibernate, and the hibernate for the appropriate encapsulation, then your project the entire persistence layer code will be quite simple, need to write a little code, the development speed, very cool.
2. There is a better level two caching mechanism, which can be used by Third-party caching.
3. The disadvantage is that the learning threshold is not low, to master the threshold is higher, and how to design O/R mapping, the performance and object model how to balance between balancing, and how to use good hibernate needs your experience and ability are very strong.
To give an image of the metaphor:
MyBatis: Mechanical tools, easy to use, bring to use, but the work is still to do their own, but the tool is alive, how to make the decision by me.
Hibernate: Intelligent robot, but the cost of developing it (learning, proficiency) is very high, the work can be rid of him, but it is limited to what it can do.