Spring JDBC template class-org.springframework.jdbc.core.jdbctemplate.

Source: Internet
Author: User
With regard to the "thin" encapsulation of JDBC, spring encapsulates the tedious JDBC operation with a spring JDBC template class. The spring JDBC Framework is carefully explained below.

First, let's take a look at why JDBC is encapsulated.

Traditional JDBC API implementations are relatively low-level, regardless of whether a user performs a complex database operation or a simple database query operation, there is a template that can be used: catch or throw exceptions-get connections-Create statement or preparedstatement-perform database operations- Get Results (ResultSet)-Other actions-handle exceptions-close resultset-Close statement or preparedstatement-close connection. This repetitive operation can make people feel more structured and consume a lot of time. Since all database operations can reference this template. Spring then encapsulates the JDBC API in a "thin" package, leaving the programmer out of a cumbersome structure and writing only the core parts of each database operating procedure: CRUD. Get the connection by configuring the data source in the configuration file. This reduces repetitive programming in traditional JDBC programming, so that this part is repeated to the spring JDBC container for processing.

What is the Spring JDBC package?

Spring JDBC is a template class JdbcTemplate that provides a database operation for encapsulation. Org.springframework.jdbc.core.JdbcTemplate extends JdbcAccessor implements JdbcOperations. Let's take a look at this encapsulation class in detail:

1, JdbcAccessor is an abstract class, implements the Initializingbean interface. The Initializingbean interface implements the response to all properties in the beanfactory after it has been set. JdbcAccessor the only way to implement Initializingbean Afterpropertiesset is to set the data source and other log information and exception information and whether it is lazyinit (lazy loading).

2, JdbcOperations is an interface. This interface defines a set of JDBC operations, most of which use callbacks to define operations. Here is the definition of the method, which can basically be read as a SPRINGJDBC document.

Operation JdbcOperations method definition
Handling Simple Connection Connection Object Execute (connectioncallback action)
Handling static Queries statement

Object Execute (statementcallback action)

void execute (String sql)

Object query (String sql, ResultSetExtractor rse)

void query (String sql, RowCallbackHandler rch)

List query (String sql, RowMapper rowmapper)

Object queryForObject (String sql, RowMapper rowmapper)

Object queryForObject (String sql, Class requiredtype)

Map queryForMap (String sql)

Long queryForLong (String sql)

int queryForInt (String sql)

List queryforlist (String sql, Class ElementType)

List queryforlist (String sql)

SqlRowSet queryForRowSet (String sql)

int update (String sql)

Int[] batchUpdate (string[] sql)

Handling PreparedStatement

Object Execute (PreparedStatementCreator psc, PreparedStatementCallback Action)

Object Execute (String sql, PreparedStatementCallback action)

Object query (PreparedStatementCreator psc, ResultSetExtractor rse)

Object query (String sql, PreparedStatementSetter pss, ResultSetExtractor rse)

Object query (String sql, object[] args, int[] argtypes, ResultSetExtractor rse)

Object query (String sql, object[] args, ResultSetExtractor rse)

void query (PreparedStatementCreator psc, RowCallbackHandler rch)

void query (String sql, PreparedStatementSetter pss, RowCallbackHandler rch)

void query (String sql, object[] args, int[] argtypes, RowCallbackHandler rch)

void query (String sql, object[] args, RowCallbackHandler rch)

List query (PreparedStatementCreator psc, RowMapper rowmapper)

List query (String sql, PreparedStatementSetter pss, RowMapper rowmapper)

List query (String sql, object[] args, int[] argtypes, RowMapper rowmapper)

List query (String sql, object[] args, RowMapper rowmapper)

Object queryForObject (String sql, object[] args, int[] argtypes, RowMapper rowmapper)

Object queryForObject (String sql, object[] args, RowMapper rowmapper)

Object queryForObject (String sql, object[] args, int[] argtypes, Class requiredtype)

Object queryForObject (String sql, object[] args, Class requiredtype)

Map queryForMap (String sql, object[] args, int[] argtypes)

Map queryForMap (String sql, object[] args)

Long queryForLong (String sql, object[] args, int[] argtypes)

Long queryForLong (String sql, object[] args)

int queryForInt (String sql, object[] args, int[] argtypes)

int queryForInt (String sql, object[] args)

List queryforlist (String sql, object[] args, int[] argtypes, Class ElementType)

List queryforlist (String sql, object[] args, Class elementtype)

List queryforlist (String sql, object[] args, int[] argtypes)

List queryforlist (String sql, object[] args)

SqlRowSet queryForRowSet (String sql, object[] args, int[] argtypes)

SqlRowSet queryForRowSet (String sql, object[] args)

int update (PreparedStatementCreator psc, Keyholder generatedKeyHolder)

int update (String sql, PreparedStatementSetter pss)

int update (String sql, object[] args, int[] argtypes)

int update (String sql, object[] args)

Int[] BatchUpdate (String sql, BatchPreparedStatementSetter pss)

Handling callable Statement

Object Execute (CallableStatementCreator csc, CallableStatementCallback action)

Object Execute (String callstring, CallableStatementCallback action)

Map Call (CallableStatementCreator csc, List declaredparameters)

All the above methods are throws DataAccessException;

3, DataAccessException inherit nestedruntimeexception,nestedruntimeexception inherit RuntimeException, the main output exception information. The exception class does not implement too many methods by itself, but rather calls the parent class method to implement it, only to refine the output of the corresponding information and pass in the corresponding parameter to the parent class method for execution.

4, we can see that many of the above method is used as a callback parameter, and the implementation of the method in JdbcTemplate or using the traditional JDBC API connection, Statement, PreparedStatement, ResultSet classes and corresponding methods to implement callbacks.

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.