Advantages and disadvantages of jdbc,mybatis,hibernate and their differences

Source: Internet
Author: User

jdbc:    We usually use JDBC to program, we need the following steps:   1, the use of JDBC programming needs to connect to the database, registration drive and database information    2, Operation Connection, Open Statement Object    3, execute SQL from statement object, return result to ResultSet object    4, use resultset to read data, then translate code into specific Pojo object    5, shutting down database-related resources   JDBC disadvantage:  One: The workload is relatively large, need to connect, and then handle the JDBC underlying transaction, processing the data type, also need to operate connection, Statement objects and ResultSet objects to get the data and close them.     II: We capture the exceptions that JDBC programming can generate and properly close resources      because of the flaws in JDBC, we seldom program directly with JDBC in practice, Using the ORM Object Relational model to manipulate the database, Hibernate is an ORM model      Hibernate:    Hibernate is built on several pojo that are mapped to database tables through XML mapping files (or annotations) that are provided by the rules. We can manipulate database data directly through Pojo, and he provides a model of full table mapping. Hibernate's encapsulation of JDBC is relatively high, and we don't need to write SQL, as long as the HQL language is available.     Programming with Hibernate has the following benefits:    1, which eliminates the code mapping rules, which are all separated into XML or annotations to configure.     2, no need to manage the database connection, it is also configured in the XML.     3, there is no need to manipulate multiple objects in a session, only the session object needs to be manipulated.     4, close the resource only need to close one session.     This is the advantage of hibernate, after configuring the mapping files and database connection files, hibernate can be done through the session, very easy to eliminate the large number of JDBC code, greatly improving the simplicity and readability of the program. Hibernate alsoProvides cascading, caching, mapping, one-to-many functions. Hibernate is a full-table mapping, which operates Pojo and then operates the database data through HQL.      Hibernate disadvantage:    1, full table mapping brings inconvenience, such as the need to send all fields when updating.     2, unable to assemble different SQL according to different conditions.     3, poor support for multi-table associations and complex SQL queries, you need to write your own SQL, after returning, you need to encapsulate the data as Pojo.     4, cannot effectively support stored procedures.     5, although there are hql, but poor performance, large-scale Internet systems often need to optimize SQL, and hibernate do not.     Mybatis:    In order to solve the lack of hibernate, Mybatis appeared, Mybatis is a semi-automatic framework. It is called semi-automatic because it requires manual matching to provide pojo,sql and mapping relationships, whereas hibernate with full table mappings only needs to provide pojo and mappings.    mybatis needs to provide a mapping file that contains three parts: SQL, mapping Rules, Pojo. In MyBatis you need to write your own SQL, although more than hibernate configuration, but mybatis can configure dynamic SQL, to solve the hibernate table name according to time changes, different conditions below the same problem, and you can also optimize SQL, The configuration determines your SQL mapping rules and can also support stored procedures, so it is more convenient for some complex and SQL queries that need to optimize performance. MyBatis can do almost anything that JDBC can do.   When to use hibernate,mybatis    hibernate as a left-java orm framework, it is really easy to program and requires us to provide mapping rules that can be generated entirely through the IDE, At the same time, there is no need to write SQL to develop more efficiently than mybatis. Hibernate also provides caching, logging, cascading and other powerful features, but Hibernate's flaws are also very obvious, multi-table associated complex SQL, Data System permissions restrictions, according to the conditions of the change of SQL, stored procedures and other scenarios using Hibernate is very inconvenient, While performance is difficult to optimize by SQL, it is destined for hibernate to be used only when the scene is not too complex and requires less demanding performance.     If you need a flexible framework that dynamically generates mapping relationships, then MyBatis is really the best option. It can almost replace JDBC, has dynamic columns, dynamic table names, stored procedure support, and provides easy caching, logging, cascading. But its flaw is that you need to provide mapping rules and SQL, so the development effort is larger than hibernate.   jdbc,mybatis,hibernate difference 1) from the level of view, JDBC is the lower layer of the persistence of the operation mode, Hibernate and MyBatis are encapsulated on a JDBC basis to make it easier for programmers to work with persistent layers. 2) From the functional perspective, JDBC is simply to establish a database connection, and then create a statement, the SQL statement to statement to execute, if there is a return result of the query statement, the query results will be placed in the ResultSet object, It is difficult to modify and optimize SQL statements by iterating over the ResultSet objects, and Hibernate is the Java object that maps the data tables in the database to persistent layers; MyBatis is the mapping of input and output parameters in SQL statements to Java objects , SQL modification and optimization is convenient. 3) from the perspective of use, if the low-level programming, and the performance requirements are very high, you should use the JDBC approach, if you want to control the integrity of the database, hibernate is recommended, if you want to use the flexible SQL statement, it is recommended to adopt the MyBatis framework 。

Jdbc,mybatis,hibernate respective pros and cons and differences

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.