Spring Framework ile Database Stored Procedure Çağrımı

Procedure/Fonksiyon çağrımlarını kolaylaştırabilmek için aşağıdaki gibi bir Util Class oluşturdum.

import java.sql.Types;

import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;

/**
 *
 * @param <T>
 * @author Dilaver Demirel
 */
public class SimpleDbProcedureCall<T> extends StoredProcedure {

    public SimpleDbProcedureCall(DataSource dataSource, String procedureName,boolean isFunction) {
        super(dataSource, procedureName);

        if(isFunction)
            declareParameter(new SqlOutParameter("return",Types.VARCHAR));

        setFunction(isFunction);        
    }

    public void addInParameter(String paramName,Integer type){
        declareParameter(new SqlParameter(paramName,type));
    }

    public void addOutParameter(String paramName,Integer type){
        declareParameter(new SqlOutParameter(paramName,type));
    }

    public T callFunction(Object... inParameters){
        compile();
        Map out = execute(inParameters);
        T returnVal = null;
        if(!out.isEmpty()){
            returnVal = (T)out.get("return");
            return returnVal;
        }else
            return null;
    }

    public Map callFunctionReturnMap(Object... inParameters){
        compile();
        return execute(inParameters);
    }
}

Örnek Kullanımı;

SimpleDbProcedureCall<String> fnc = new SimpleDbProcedureCall<String>(dataSource,"test_function",true);
fnc.addInParameter("id", Types.NUMERIC);

fnc.addOutParameter("o_val", Types.VARCHAR);

Map retVal = fnc.callFunctionReturnMap(id);

Tmp_Class tmpClass = new Tmp_Class();
tmpClass.setOut1((Integer)retVal.get("return"));
tmpClass.setOut2((String)retVal.get("o_val"));