JdbcTemplate root dbutils very similar, if you have a lot of DAO, every DAO you need to write it
/* inject a JDBC template on the DAO layer */ Private jdbctemplate jdbctemplate; Public void setjdbctemplate (JdbcTemplate jdbctemplate) { this. JdbcTemplate= jdbctemplate; }
And you have to inject it in every configuration file.
<id= "Userdao" class= "Cn.itcast.spring3.demo2.UserDao " > <name= "JdbcTemplate" ref= " JdbcTemplate "></Property> </Bean >
Source Code of Jdbcdaosupport
/** Copyright 2002-2008 the original author or authors. * Licensed under the Apache License, Version 2.0 (the "Licens E "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' As is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageOrg.springframework.jdbc.core.support;Importjava.sql.Connection;ImportJavax.sql.DataSource;ImportOrg.springframework.dao.support.DaoSupport;Importorg.springframework.jdbc.CannotGetJdbcConnectionException;Importorg.springframework.jdbc.core.JdbcTemplate;Importorg.springframework.jdbc.datasource.DataSourceUtils;ImportOrg.springframework.jdbc.support.SQLExceptionTranslator;/*** Convenient Super class for jdbc-based data Access objects. * * <p>requires a {@linkJavax.sql.DataSource} to BES set, providing a * {@linkOrg.springframework.jdbc.core.JdbcTemplate} based on it to * subclasses through the {@link#getJdbcTemplate ()} method. * * <p>this base class is mainly intended for jdbctemplate usage but can * also was used when working with a connecti On directly or when using * <code>org.springframework.jdbc.object</code> operation objects. * * @authorJuergen Hoeller *@since28.07.2003 *@see#setDataSource *@see#getJdbcTemplate *@seeorg.springframework.jdbc.core.JdbcTemplate*/ Public Abstract classJdbcdaosupportextendsDaosupport {PrivateJdbcTemplate JdbcTemplate; /*** Set The JDBC DataSource to is used by this DAO. */ Public Final voidSetdatasource (DataSource DataSource) {if( This. JdbcTemplate = =NULL|| DataSource! = This. Jdbctemplate.getdatasource ()) { This. JdbcTemplate =createjdbctemplate (DataSource); Inittemplateconfig (); } } /*** Create A jdbctemplate for the given DataSource. * Only invoked if populating the DAO with a DataSource reference! * <p>can is overridden in subclasses to provide a JdbcTemplate instance * with different configuration, or a Cus Tom JdbcTemplate subclass. * @paramDataSource the JDBC dataSource to create a jdbctemplate for *@returnThe new JdbcTemplate instance *@see#setDataSource*/ protectedjdbctemplate createjdbctemplate (DataSource DataSource) {return NewJdbcTemplate (DataSource); } /*** Return The JDBC DataSource used by this DAO. */ Public FinalDataSource Getdatasource () {return( This. jdbctemplate! =NULL? This. Jdbctemplate.getdatasource ():NULL); } /*** Set The jdbctemplate for this DAO explicitly, * as an alternative to specifying a DataSource. */ Public Final voidsetjdbctemplate (JdbcTemplate jdbctemplate) { This. JdbcTemplate =JdbcTemplate; Inittemplateconfig (); } /*** Return The JdbcTemplate for this DAO, * pre-initialized with the DataSource or set explicitly. */ Public FinalJdbcTemplate getjdbctemplate () {return This. JdbcTemplate; } /*** Initialize The template-based configuration of this DAO. * Called after a new jdbctemplate have been set, either directly * or through a DataSource. * <p>this implementation is empty. Subclasses may override this * to configure further objects based on the JdbcTemplate. * @see#getJdbcTemplate ()*/ protected voidInittemplateconfig () {} @Overrideprotected voidCheckdaoconfig () {if( This. JdbcTemplate = =NULL) { Throw NewIllegalArgumentException ("' DataSource ' or ' jdbctemplate ' is required"); } } /*** Return The sqlexceptiontranslator of this DAO's jdbctemplate, * for translating sqlexceptions in custom JDB C access code. * @seeOrg.springframework.jdbc.core.jdbctemplate#getexceptiontranslator ()*/ protected Finalsqlexceptiontranslator Getexceptiontranslator () {returngetjdbctemplate (). Getexceptiontranslator (); } /*** Get a JDBC Connection, either from the current transaction or a new one. * @returnThe JDBC Connection *@throwsCannotgetjdbcconnectionexception If the attempt to get a Connection failed *@seeorg.springframework.jdbc.datasource.datasourceutils#getconnection (Javax.sql.DataSource)*/ protected FinalConnection getconnection ()throwscannotgetjdbcconnectionexception {returndatasourceutils.getconnection (Getdatasource ()); } /*** Close The given JDBC Connection, created via this DAO's DataSource, * if it isn ' t bound to the thread. * @paramcon Connection to close *@seeorg.springframework.jdbc.datasource.datasourceutils#releaseconnection*/ protected Final voidreleaseconnection (Connection con) {datasourceutils.releaseconnection (Con, Getdatasource ()); }}
day39-spring 17-spring JDBC Template: Completing the Add-and-snip operation