日期:2014-05-16  浏览次数:20506 次

Spring之JDBC模板(手写)
一 模板父类: SpringBaseDAO.java
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.lob.OracleLobHandler;
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor;
import org.springframework.jdbc.support.nativejdbc.WebLogicNativeJdbcExtractor;


public class SpringBaseDAO extends JdbcDaoSupport{		
	
	private NativeJdbcExtractor nativeJdbcExtractor;	
	protected OracleLobHandler lobHandler;
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	private SimpleJdbcTemplate simpleJdbcTemplate;
	

	public SpringBaseDAO() {
		setDataSource(ConnectionUtils.getDataSource());
		namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(ConnectionUtils.getDataSource());
		simpleJdbcTemplate = new SimpleJdbcTemplate(ConnectionUtils.getDataSource());
		nativeJdbcExtractor = new WebLogicNativeJdbcExtractor();
		lobHandler = new OracleLobHandler();
		lobHandler.setNativeJdbcExtractor(nativeJdbcExtractor);
		getJdbcTemplate().setNativeJdbcExtractor(nativeJdbcExtractor);		
	}	

	public void close(Connection childConnection) throws SQLException {
		releaseConnection(childConnection);
	}

	public void close(Statement childStatement) throws SQLException {
		if (null != childStatement) {
			childStatement.close();
			childStatement = null;
		}
	}

	public void close(CallableStatement childCallStatement) throws SQLException {
		if (null != childCallStatement) {
			childCallStatement.close();
			childCallStatement = null;
		}
	}

	public void close(PreparedStatement childPsmt) throws SQLException {
		if (null != childPsmt) {
			childPsmt.close();
			childPsmt = null;
		}
	}

	public void close(ResultSet childRs) throws SQLException {
		if (null != childRs) {
			childRs.close();
			childRs = null;
		}
	}

	public void close(Connection childConnection, Statement childStatement)
			throws SQLException {
		close(childConnection);
		close(childStatement);
	}

	public void close(Connection childConnection, PreparedStatement childPsmt)
			throws SQLException {
		close(childConnection);
		close(childPsmt);
	}

	public void close(Connection childConnection, ResultSet childRs)
			throws SQLException {
		close(childConnection);
		close(childRs);
	}

	public void closeChild(Connection childConnection,
			Statement childStatement, ResultSet childRs) throws SQLException {
		close(childConnection, childStatement);
		close(childRs);
	}

	public void close(Connection childConnection, PreparedStatement childPsmt,
			ResultSet childRs) throws SQLException {
		close(childConnection, childPsmt);
		close(childRs);
	}

	public void close(PreparedStatement childPsmt, ResultSet childRs)
			throws SQLException {
		close(childPsmt);
		close(childRs);
	}

	public NativeJdbcExtractor getNativeJdbcExtractor() {
		return nativeJdbcExtractor;
	}

	public void setNativeJdbcExtractor(NativeJdbcExtractor nativeJdbcExtractor) {
		this.nativeJdbcExtractor = nativeJdbcExtractor;
	}

	public OracleLobHandler getLobHandler() {
		return lobHandler;
	}

	public void setLobHandler(OracleLobHandler lobHandler) {
		this.lobHandler = lobHandler;
	}

	public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
		return namedParameterJdbcTemplate;
	}

	public void setNamedParameterJdbcTemplate(
			NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
		this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
	}

	public SimpleJdbcTemplate getSimpleJdbcTemplate() {
		return simpleJdbcTemplate;
	}

	public void setSimpleJdbcTemplate(SimpleJdbcTemplate simpleJdbcTemplate) {
		this.simpleJdbcTemplate = simpleJdbcTemplate;
	}
}