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

Source: Internet
Author: User
With regard to the "thin" encapsulation of JDBC, Spring uses a spring JDBC template class to encapsulate cumbersome JDBC operations. The spring JDBC Framework is carefully explained below.

First, take a look at why you want to encapsulate JDBC.

The traditional JDBC API implementation is relatively low-level, regardless of whether the user performs a complex database operation or a simple database query operation, there is a template can be used: catch or throw an exception-get the connection-create statement or preparedstatement-perform database operations- Get Results (ResultSet)-Other operations-handle exceptions-close resultset-Close statement or preparedstatement-close the 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 has a "thin" encapsulation of the JDBC API, which allows programmers to break away from cumbersome structures and write only the core parts of each database operating program: CRUD. Gets the connection by configuring the data source in the configuration file. This reduces the duplication of programming in traditional JDBC programming, allowing this part to be repeatedly handled by the spring JDBC container.

Second, 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 specifically:

1, JdbcAccessor is an abstract class, implements the Initializingbean interface. The Initializingbean interface implements the response to all properties set in Beanfactory. 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 load).

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
Handle 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, main output exception information. Exception classes do not implement too many methods on their own, but generally invoke the parent class method for implementation, only to refine the output of the corresponding information and pass the corresponding parameter to the parent class method for execution.

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

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.