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

SPRING JDBC 的使用.

?

package com.just.zb.spring.jdbc;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

public abstract class AbstractExceute {
	 private  JdbcTemplate  template;
     private  DataSource  dataSource;
	public JdbcTemplate getTemplate() {
		return template;
	}
	public void setTemplate(JdbcTemplate template) {
		this.template = template;
	}
	public DataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public abstract void executeSql(String sql, List params) throws Exception;
	public abstract void executeSqls(String []sqls, List<List> params) throws Exception;
	public abstract void executeBatchSql(String sql, List<List> params) throws Exception;
	public abstract int getAllCount(String sql, List params) throws Exception;
	public abstract List getList(String sql,List params ,Class clazz) throws Exception;
	public abstract List processStoredProcedure(final String procedure ,final Map<Integer , Object> inParams , final  Map<Integer , Integer> outParams ) throws Exception ;
}

?

?

?

实现类:

?

package com.just.zb.spring.jdbc;

import java.lang.reflect.Field;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.CallableStatementCallback;

public class Execute  extends  AbstractExceute{
	protected static Logger log = Logger.getLogger(Execute.class);
	@Override
	public void executeBatchSql(final String sql, final List<List> params)
			throws Exception {
		
		BatchPreparedStatementSetter pss = null;
		pss = new BatchPreparedStatementSetter() {   
            public void setValues(PreparedStatement pstmt, int ord) throws SQLException { 
            	List param = params.get(ord);
                for (int i = 0; i < (null != param ? param.size() : 0); i++) {
    				pstmt.setObject(i + 1, param.get(i));
    			}
            }   
            public int getBatchSize() {   
                return 3;   
            }   
        };  
        getTemplate().batchUpdate(sql, pss);
	}

	@Override
	public void executeSql(String sql, List params) throws Exception {
		   getTemplate().update(sql,  params.toArray());
	}

	@Override
	public void executeSqls(String []sqls, List<List> params) throws Exception {
		for(int i=0;i<sqls.length;i++){
			getTemplate().update(sqls[i],  ((List)params.get(i)).toArray());
		}
	}

	@Override
	public int getAllCount(String sql, List params) throws Exception {
		  return getTemplate().queryForInt(sql,params.toArray());
		
	}

	@Override
	public List getList(String sql, List params, Class clazz) throws Exception {
		List list = getTemplate().queryForList(sql, params.toArray());
		return list;
	}
	
	
	public List processStoredProcedure(final String procedure ,final Map<Integer , Object> inParams ,  final  Map<Integer , Integer> outParams ) throws Exception {
        CallableStatementCallback cb = new CallableStatementCallback() {
            public Object doInCallableStatement(CallableStatement cs)
                    throws SQLException {
            	Set  outSet = null;
            	if(outParams != null && outParams.size()!= 0){
            		   outSet = outParams.entrySet();
            		for (Iterator iter = outSet.iterator(); iter.hasNext();) {
                		Entry<Integer , Integer> entry = (Entry <Integer , Integer>)iter.next();
                		 cs.registerOutParameter(entry.getKey(),entry.getValue()