2009-05-14 14 views
7

Utilizzo il framework di persistenza Spring per il mio progetto. Voglio chiamare la funzione oracle o stored procedure da questo framework.Come chiamare la funzione Oracle o la stored procedure utilizzando il framework di persistenza primaverile?

Qualcuno può suggerire come posso ottenere questo.

Si prega di fornire una soluzione per entrambe le funzioni * oracle e * stored.

Grazie.

+0

Primavera ha un framework di persistenza? Ti riferisci a Spring's JdbcTemplate? O in ibernazione? –

+0

Ho usato DPTK per creare il framework di persistenza e il factory di query di Spring. Ora voglio chiamare la funzione oracle o stored procedure usando le funzionalità esistenti, puoi aiutarmi a ordinare questo .. –

+0

DPTK sembra essere un prodotto IBM: http://www.alphaworks.ibm.com/tech/dptk –

risposta

32

si Supponendo che si riferiscono a JdbcTemplate:

jdbcTemplate.execute(
    new CallableStatementCreator() { 
     public CallableStatement createCallableStatement(Connection con) throws SQLException{ 
      CallableStatement cs = con.prepareCall("{call MY_STORED_PROCEDURE(?, ?, ?)}"); 
      cs.setInt(1, ...); // first argument 
      cs.setInt(2, ...); // second argument 
      cs.setInt(3, ...); // third argument 
      return cs; 
     } 
    }, 
    new CallableStatementCallback() { 
     public Object doInCallableStatement(CallableStatement cs) throws SQLException{ 
      cs.execute(); 
      return null; // Whatever is returned here is returned from the jdbcTemplate.execute method 
     } 
    } 
); 

Chiamata di una funzione è quasi identico:

jdbcTemplate.execute(
    new CallableStatementCreator() { 
     public CallableStatement createCallableStatement(Connection con) { 
      CallableStatement cs = con.prepareCall("{? = call MY_FUNCTION(?, ?, ?)}"); 
      cs.registerOutParameter(1, Types.INTEGER); // or whatever type your function returns. 
      // Set your arguments 
      cs.setInt(2, ...); // first argument 
      cs.setInt(3, ...); // second argument 
      cs.setInt(4, ...); // third argument 
      return cs; 
     } 
    }, 
    new CallableStatementCallback { 
     public Object doInCallableStatement(CallableStatement cs) { 
      cs.execute(); 
      int result = cs.getInt(1); 
      return result; // Whatever is returned here is returned from the jdbcTemplate.execute method 
     } 
    } 
); 
+0

qui stiamo chiamando SP, come possiamo chiamare la funzione Oracle ... Non mi riferisco a JdbcTemplate, sto riferendo il framework di persistenza usando DPTK .. –

+0

Il secondo esempio chiama una funzione. Sfortunatamente, non conosco DPTK. Ha un sito web? –

+0

okay, cercherò di implementarlo con il mio codice ... spero che funzioni ... Grazie Adam, ti farò sapere il risultato .. –

17

modo più semplice di chiamare una funzione Oracle in primavera è sottoclassi StoredProcedure come qui di seguito

public class MyStoredProcedure extends StoredProcedure{ 
    private static final String SQL = "package.function"; 

    public MyStoredProcedure(DataSource ds){ 
     super(ds,SQL); 
     declareParameter(new SqlOutParameter("param_out",Types.NUMERIC)); 
     declareParameter(new SqlParameter("param_in",Types.NUMERIC)); 
     setFunction(true);//you must set this as it distinguishes it from a sproc 
     compile(); 
    } 

    public String execute(Long rdsId){ 
     Map in = new HashMap(); 
     in.put("param_in",rdsId); 
     Map out = execute(in); 
     if(!out.isEmpty()) 
      return out.get("param_out").toString(); 
     else 
      return null; 
    } 
} 

E chiamalo così

@Autowired DataSource ds; 
MyStoredProcedure sp = new MyStoredProcedure(ds); 
String i = sp.execute(1l); 

La funzione Oracle utilizzata qui contiene solo un parametro numerico e restituisce un parametro numerico.

+0

come ho capito il codice SQL dovrebbe essere simile: '{: param_out = chiama schema.package.MY_FUNCTION (: param_in)}' – mmoossen

+0

il mio ultimo commento è sbagliato. la costante SQL deve contenere solo il nome procedura/funzione, come 'schema.package.MY_FUNCTION', ei nomi dei parametri in Java ** HAVE ** per abbinare i nomi dei parametri come definiti nella procedura/funzione. – mmoossen

0

A mio parere questo è uno degli approcci più semplici:

public class ServRepository { 

    private JdbcTemplate jdbcTemplate; 
    private SimpleJdbcCall functionGetServerErrors; 

    @Autowired 
    public void setDataSource(DataSource dataSource) { 
     this.jdbcTemplate = new JdbcTemplate(dataSource); 
     JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 
     jdbcTemplate.setResultsMapCaseInsensitive(true); 
     this.functionGetServerErrors = new SimpleJdbcCall(jdbcTemplate).withFunctionName("THIS_IS_YOUR_DB_FUNCTION_NAME").withSchemaName("OPTIONAL_SCHEMA_NAME"); 
    } 

     public String callYourFunction(int parameterOne, int parameterTwo) { 
      SqlParameterSource in = new MapSqlParameterSource().addValue("DB_FUNCTION_INCOMING_PARAMETER_ONE", parameterOne).addValue("DB_FUNCTION_INCOMING_PARAMETER_TWO", parameterTwo); 
      return functionGetServerErrors.executeFunction(String.class, in); 
     } 
}