spring jdbc 调用存储过程封装

1、调用存储过程--返回list以及无结果返回以及只返回一个参数

 
  
/**
		 *  获取数据库内的存储过程--返回一个List
		 * @param procedureName 存储过程名
		 * @param inParameter   输入的参数 
		 * @param outParamter   输出的参数
		 * @return
		 */
		public List callProcedure(final String procedureName,final List inParameter,final List outParamter){
			if(procedureName==null || procedureName.length() == 0 ){
				return null;
			}
			//没有返回参数
			if(outParamter == null ){
				if(callProcedureWithoutOut(procedureName,inParameter))return null;
			}
			 List resultList = (List) DBUtility.getJdbcTemplate().execute(  
				     new CallableStatementCreator() {
						public CallableStatement createCallableStatement(Connection con) throws SQLException {
							int inSize = inParameter==null?0:inParameter.size();
							int outSize = outParamter==null?0:outParamter.size();
							StringBuffer sbsql = new StringBuffer();
							sbsql.append("{call "+procedureName).append("(");
							for(int i=0;i<(inSize+outSize);i++){
								if(i == 0){
									sbsql.append("?");
								}else{
									sbsql.append(",?");
								}
							}
							sbsql.append(")}");
					         CallableStatement cs = con.prepareCall(sbsql.toString());
					         // 设置输入参数的值
					         if(inSize > 0 ){
					        	String typeName =  null;
					        	for(int i=0;i() {
						public List doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException {   
							   int inSize = inParameter==null?0:inParameter.size();
					           List resultsMap = new ArrayList();   
					           cs.execute();   
					           ResultSet rs = (ResultSet) cs.getObject(inSize+1);// 获取游标一行的值   
					           while (rs.next()) {// 转换每行的返回值到Map中   
					              Map rowMap = new HashMap();  
					              for(int i=0;i 0 ){
					        	String typeName =  null;
					        	for(int i=0;i() {
						public Object doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException {   
							   int inSize = inParameter==null?0:inParameter.size();
					           List resultsMap = new ArrayList();   
					           cs.execute();   
					           return  cs.getObject(inSize+1);// 获取游标一行的值   
					        }   
						
					}); 
			 return result;
		}	
	

你可能感兴趣的:(spring,java)