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