Java tutorial
/** * Copyright 2006-2015 pyw.cn * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "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. */ package xyz.vopen.passport.commons.jdbc; import org.springframework.jdbc.CannotGetJdbcConnectionException; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.datasource.DataSourceUtils; import org.springframework.jdbc.support.SQLExceptionTranslator; import javax.annotation.Resource; import javax.sql.DataSource; import java.sql.Connection; import java.util.logging.Logger; /** * jdbc??? * * @author Hongvi Xu */ public class JdbcDaoSupport { protected Logger logger = Logger.getLogger(JdbcDaoSupport.class.getName()); /** * Get JdbcTemplate Instance */ private JdbcTemplate jdbcTemplate; /** * Create a JdbcTemplate for the given DataSource. Only invoked if * populating the DAO with a DataSource reference! * <p/> * Can be overridden in subclasses to provide a JdbcTemplate instance with * different configuration, or a custom JdbcTemplate subclass. * * @param dataSource the JDBC DataSource to create a JdbcTemplate for * @return the new JdbcTemplate instance * @see #setDataSource */ protected JdbcTemplate createJdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } /** * Create a NamedParameterJdbcTemplate for the given DataSource. Only * invoked if populating the DAO with a DataSource reference! * <p/> * Can be overridden in subclasses to provide a JdbcTemplate instance with * different configuration, or a custom NamedParameterJdbcTemplate subclass. * * @param dataSource the JDBC DataSource to create a NamedParameterJdbcTemplate for * @return the new NamedParameterJdbcTemplate instance * @see #setDataSource */ protected NamedParameterJdbcTemplate createNamedParameterJdbcTemplate(DataSource dataSource) { return new NamedParameterJdbcTemplate(dataSource); } /** * Return the JDBC DataSource used by this DAO. */ public final DataSource getDataSource() { return (this.jdbcTemplate != null ? this.jdbcTemplate.getDataSource() : null); } /** * Set the JDBC DataSource to be used by this DAO. */ @Resource public final void setDataSource(DataSource dataSource) { this.jdbcTemplate = createJdbcTemplate(dataSource); initTemplateConfig(); } /** * Return the JdbcTemplate for this DAO, pre-initialized with the DataSource * or set explicitly. */ public final JdbcTemplate getJdbcTemplate() { return this.jdbcTemplate; } /** * Set the JdbcTemplate for this DAO explicitly, as an alternative to * specifying a DataSource. */ public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; initTemplateConfig(); } /** * Initialize the template-based configuration of this DAO. Called after a * new JdbcTemplate has 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 void initTemplateConfig() { } protected void checkDaoConfig() { if (this.jdbcTemplate == null) { throw new IllegalArgumentException("'dataSource' or 'jdbcTemplate' is required"); } } /** * Return the SQLExceptionTranslator of this DAO's JdbcTemplate, for * translating SQLExceptions in custom JDBC access code. * * @see org.springframework.jdbc.core.JdbcTemplate#getExceptionTranslator() */ protected final SQLExceptionTranslator getExceptionTranslator() { return getJdbcTemplate().getExceptionTranslator(); } /** * Get a JDBC Connection, either from the current transaction or a new one. * * @return the JDBC Connection * @throws CannotGetJdbcConnectionException if the attempt to get a Connection failed * @see DataSourceUtils#getConnection(DataSource) */ protected final Connection getConnection() throws CannotGetJdbcConnectionException { return DataSourceUtils.getConnection(getDataSource()); } /** * Close the given JDBC Connection, created via this DAO's DataSource, if it * isn't bound to the thread. * * @param con Connection to close * @see DataSourceUtils#releaseConnection */ protected final void releaseConnection(Connection con) { DataSourceUtils.releaseConnection(con, getDataSource()); } }