The disadvantage of traditional JDBC and the solution of MyBatis

Source: Internet
Author: User
Tags connection pooling

The problem with traditional JDBC is as follows:

1. Database connection creation, release frequently caused the waste of West Rong resources, thus affecting system performance, using database connection pool can solve the problem.

2.sql statements in code hard-coded, resulting in code maintenance, the actual application of SQL changes may be large, SQL code and Java code is not separated to maintain inconvenient.

3. Using PreparedStatement to pass parameters to a placeholder there is a hard-coding problem because the condition of the WHERE clause in SQL is not deterministic, and it is also inconvenient to modify

4. There is a hard coding problem in the result set parsing, the change of SQL leads to the change of parsing code and the system maintenance is inconvenient.

MyBatis's solution to traditional JDBC

1, the database connection creation, the release frequently causes the system resources to waste thus affects the system performance, if uses the database connection pool solves this problem.

Workaround: Configure the data connection pool in Sqlmapconfig.xml and use connection pooling to manage database links.

2, SQL statements written in the code cause code is not easy to maintain, the actual application of SQL changes may be large, SQL changes need to change the Java code.

WORKAROUND: Configure the SQL statement to be detached from the Java code in the Xxxxmapper.xml file.

3. It is troublesome to pass arguments to SQL statements because the WHERE condition of the SQL statement is not necessarily, possibly more or less, and the placeholder needs to correspond to parameter one by one.

Resolution: MyBatis Automatically maps Java objects to SQL statements, defining the type of input parameters through ParameterType in statement.

4, the result set parsing trouble, SQL changes lead to parsing code changes, and need to traverse before parsing, if the database records can be encapsulated into Pojo object parsing is more convenient.

Resolution: MyBatis automatically maps SQL execution results to Java objects, defining the type of output results through Resulttype in statement.

The disadvantage of traditional JDBC and the solution of MyBatis

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.