Spring JdbcTemplate Source Analysis

Source: Internet
Author: User
Tags assert stmt

This article analyzes spring JdbcTemplate source code, mainly is to study its design essence. Template mode, smart callbacks

First, JdbcTemplate class structure

①, JdbcOperations: Interfaces define methods, such as

<T> T Execute (statementcallback<t> action) throws DataAccessException;

void execute (String sql) throws DataAccessException;

<T> T query (String sql, resultsetextractor<t> rse) throws DataAccessException;

。。。

②, JdbcAccessor: Defines the data source.

③, implementing the JdbcOperations interface definition method

Second, jdbctemplate template mode

1. Let's look at the execute (statementcallback<t> action) implementation ( core )

 Public<T> T Execute (statementcallback<t> action)throwsDataAccessException {assert.notnull (action,"Callback object must not being null"); Connection Con=datasourceutils.getconnection (Getdatasource ()); Statement stmt=NULL; Try{Connection Contouse=con; if( This. nativejdbcextractor! =NULL&& This. Nativejdbcextractor.isnativeconnectionnecessaryfornativestatements ()) {Contouse= This. Nativejdbcextractor.getnativeconnection (Con); } stmt=contouse.createstatement ();            Applystatementsettings (stmt); Statement Stmttouse=stmt; if( This. nativejdbcextractor! =NULL) {Stmttouse= This. Nativejdbcextractor.getnativestatement (stmt); } T result= action.doinstatement (stmttouse);            //Callback handlewarnings (stmt); returnresult; }        Catch(SQLException ex) {jdbcutils.closestatement (stmt); stmt=NULL;            Datasourceutils.releaseconnection (Con, Getdatasource ()); Con=NULL; ThrowGetexceptiontranslator (). Translate ("Statementcallback", GetSQL (action), ex); }        finally{jdbcutils.closestatement (stmt);        Datasourceutils.releaseconnection (Con, Getdatasource ()); }    }

2. Then see execute (String sql) source code

 Public voidExecuteFinalString SQL)throwsDataAccessException {if(logger.isdebugenabled ()) {Logger.debug ("Executing SQL statement [" + SQL + "]"); }        classExecutestatementcallbackImplementsStatementcallback<object>, SQLProvider {@Override PublicObject doinstatement (Statement stmt)throwsSQLException { stmt.execute (SQL);//JAVA jdbc return NULL; } @Override PublicString GetSQL () {returnSQL; }} execute ( New Executestatementcallback ());//Call the above T execute (statementcallback<t> action) }

Thus, we can know that we usually use the Execute (final String sql) method, the underlying help us do a lot of things, such as the creation of the default Executestatementcallback, in the form of callbacks, the template to execute SQL, close the link

3, T query (final String SQL, final resultsetextractor<t> rse) source code implementation

@Override Public<T> T Query (FinalString SQL,FinalResultsetextractor<t> rse)throwsdataaccessexception {assert.notnull (SQL,"SQL must not being null"); Assert.notnull (RSE,"ResultSetExtractor must not being null"); if(logger.isdebugenabled ()) {Logger.debug ("Executing SQL query [" + SQL + "]"); }        classQuerystatementcallbackImplementsStatementcallback<t>, SQLProvider {@Override PublicT doinstatement (Statement stmt)throwsSQLException {ResultSet rs=NULL; Try{RS=stmt.executequery (SQL); ResultSet Rstouse=rs; if(NativeJdbcExtractor! =NULL) {Rstouse=Nativejdbcextractor.getnativeresultset (RS); }                    returnRse.extractdata (Rstouse); }                finally{Jdbcutils.closeresultset (RS); }} @Override PublicString GetSQL () {returnSQL; }        }        returnExecuteNewquerystatementcallback ()); }
View Code

4. Update (final String SQL) source code

@Override Public intUpdateFinalString SQL)throwsdataaccessexception {assert.notnull (SQL,"SQL must not being null"); if(logger.isdebugenabled ()) {Logger.debug ("Executing SQL UPDATE [" + SQL + "]"); }        classUpdatestatementcallbackImplementsStatementcallback<integer>, SQLProvider {@Override PublicInteger doinstatement (Statement stmt)throwsSQLException {introws =stmt.executeupdate (SQL); if(logger.isdebugenabled ()) {Logger.debug ("SQL Update Affected" + rows + "Rows"); }                returnrows; } @Override PublicString GetSQL () {returnSQL; }        }        returnExecuteNewupdatestatementcallback ()); }
View Code

Spring JdbcTemplate Source Analysis

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.