Comparison of Hibernate, MyBatis, spring data

Source: Internet
Author: User

Turn:

1. Concept:

Hibernate : Hibernate is an open source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that allows Java programmers to manipulate databases at will, using object programming thinking. Focus on the relationship between objects and objects.

Mybatis:mybatis is an open source project for Apache Ibatis, which was migrated to Google code by the Apache Software Foundation in 2010 and renamed Mybatis. MyBatis focuses on the mapping relationship between Pojo and SQL

Spring data:spring Data is an open-source framework for simplifying database access and supporting cloud services. Its main goal is to make access to data easy and fast, and to support map-reduce framework and cloud computing data Services.

3. Difficulty in development:

Hibernate is more difficult to develop than MyBatis, Spring Data. The main reason is that hibernate encapsulates a complete object-relational mapping mechanism, so that the internal implementation is complex, large, and the learning cycle is longer.

Mybatis relies primarily on SQL authoring and Resultmap mapping. Spring data is easy to use and simplifies query operations with naming conventions and annotation queries.

3. Query differences:

Simple query: Hibernate provides the basic query method, but also according to the specific business to write the corresponding SQL;

MyBatis need to write SQL statements manually, Spring Data inherits the underlying interface and can use the built-in additions and deletions method.

Advanced query: Hibernate through the object mapping mechanism, developers do not have to care about SQL generation and results mapping, focus on business processes; MyBatis need to manually write SQL statements in XML files and resultmap or annotations, Spring Data It is easier to write the desired SQL by providing a naming specification query and a note query.

4. Extensibility of the database:

Hibernate is associated with the database in XML, so HQL is not very concerned about what database to use.

MyBatis because all SQL is dependent on database writing, extensibility, migration is poor.

Spring data and data-specific associations can be queried by naming specifications, annotation queries, without concern for database differences, but by localizing SQL queries, it is not easy to expand.

5. Caching mechanism:

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.

The difference: Hibernate's Level Two cache configuration is configured in detail in the configuration file generated by 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.

Spring Data can configure caching behavior that meets its business needs through its own cache or third-party caching scheme.  
6. Enquiry Method: Hibernate query:

.1.HQL--->from admin as admin where Admin.name =:name use named parameters, use only with hiberante frame

2.Criteria----> Object query Criteria c = getsession (). Criteria (Admin.class)

C.add (Restrictions.eq ("Aname", name));//eq is equal to, GT is greater than, LT is less than, or is or
C.add (Restrictions.eq ("Apassword", password));

3.DetachedCriteria-----> Dynamic Separation Query

4. Example query-example.create (user). List ()

5.sql Query

Query q = S.createsqlquery ("SELECT * from User"). Addentity (User.class);

6. Named queries: Query q = getsession (). Getnamedquery ("Getuserbyid");


mybatis Query:

L define XML for example; usermapper.xml

L Define interface Usermapper definition related methods do not need to write an implementation class for an interface

L Parse SQL in XML file by MyBatis internal processing mechanism

L Calling stored procedure {Call stored procedure name}


Spring data query:

1. Named queries, you need to follow the spring data specification, such as Findbyuser, Deletebyid, etc. right-to-left parsing generates SQL

2. Annotation query: @Query ("QL statement")


7. Summary:

Hibernate provides a more complete package of databases, encapsulates basic DAO layer operations, and has better database portability

Mybatis can perform more granular SQL optimizations, query the necessary fields, but need to maintain the mapping of SQL and query result sets, and the poor portability of databases, writing different SQL for different databases,

Spring data JPA greatly simplifies database access and allows you to write SQL faster by naming specifications, annotations, and more.

The use of ORM frameworks requires the choice of a more appropriate technical solution based on the specific project business, rather than technology.



Comparison of Hibernate, MyBatis, spring data

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.