Home > Java > javaTutorial > Briefly describe the meaning of the JdbcTemolate class

Briefly describe the meaning of the JdbcTemolate class

巴扎黑
Release: 2017-07-22 14:25:29
Original
2189 people have browsed it

Introduction to the JdbcTemolate class<1>

JdbcTemplate is the core class of Spring JDBC, which encapsulates common JDBC usage while trying to avoid common errors. This class simplifies the operation of JDBC. We only need to write the code that provides SQL and how to return the results. JdbcTemplate can perform operations such as querying and updating, initialize traversal operations on ResultSets, and capture JDBC exceptions and convert them into more conventional and useful exception classes defined under the org.springframework.dao package.

By implementing the callback interface, you can customize the specific operations of these callback functions. Among them, PreparedStatementSetter and RowMapper are the two most commonly used callback interfaces.

All SQL operations are recorded with debug level logs under org.springframework.jdbc.core.JdbcTemplate.

Description: Instances of this class are thread-safe after configuration

JdbcAccessor class introduction

The JdbcAccessor class is the JdbcTemplate class The base class is used to handle JDBC connection operations, and also defines common attributes such as data sources and exception translators.

Introduction to the JdbcOperations interface

The JdbcOperations interface defines some basic operations of JDBC. The specific implementation is placed in the JdbcTemplate class. It is not recommended to use it directly, but because it is more suitable for mocks and stubs, it is used in A very good choice when testing.

Variables of JdbcTemplate

ignoreWarnings

If this variable is false, JDBC warnings (SQL warnings) will be thrown. Default is true.

Description: SQL Warnings handle less severe exceptions, non-fatal errors, or unexpected conditions so they can be ignored.

fetchSize

If the variable is a non-negative value, it will be assigned to the fetchSize variable of the statements used to execute the query. Default is -1.

maxRows

If this variable is a non-negative value, it will be assigned to the maxRows variable of the statements used to execute the query. Default is -1.

queryTimeout

If the variable is non-negative, it will be assigned to the queryTimeout variable of the statements used to execute the query. Default is -1.

skipResultsProcessing

If this variable is true, then all callable statement processing will bypass all result checks, which can be used to avoid some earlier versions of the oracle jdbc driver (such as 10.1.0.2 ). The default is false.

skipUndeclaredResults

If this variable is true, then the call result check of the stored procedure with output parameters will be omitted, unless skipResultsProcessing is true, otherwise all other returned results will be processed. Default is false.

dataSource

This variable is of javax.sql.DataSource type, inherited from the JdbcAccessor class, and can be null, but this variable will be checked when Spring initializes the bean. If it is null, An IllegalArgumentException will be thrown, prompting "Property 'dataSource' is required".

exceptionTranslator

This variable belongs to a functional interface, used to convert SQLException and Spring's customized DataAccessException. It is inherited from the JdbcAccessor class and can be null.

lazyInit

If this variable is true, then the first time a SQLException is encountered, otherwise the exceptionTranslator will not be initialized. Default is true.

Because the JdbcAccessor class inherits the InitializingBean interface, and the JdbcTemplate class inherits the JdbcAccessor class, Spring will call afterPropertiesSet when initializing the JdbcTemplate bean. At this time, if lazyInit is false and exceptionTranslator is used, then exceptionTranslator will be attempted to be initialized. If dataSource is null, SQLStateSQLExceptionTranslator will be used for initialization, otherwise SQLErrorCodeSQLExceptionTranslator will be used.

nativeJdbcExtractor

Customized local JDBC operation object, used to operate non-standard JDBC API.

In order to better support JDBC4, the Spring Framework working group deleted the nativeJdbcExtractor on the master branch on Github on June 7, 2017, but the variable still exists in other branches, which is not clear yet In order to restore the variable

The above is the detailed content of Briefly describe the meaning of the JdbcTemolate class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template