2009-05-14 11 views
7

Używam ramy uporczywości wiosennej dla mojego projektu. Chcę wywołać funkcję Oracle lub procedura przechowywana z tego ramy.Jak wywołać funkcję Oracle lub procedurę przechowywaną za pomocą struktury sprężystości?

Czy ktoś może zasugerować, w jaki sposób mogę to osiągnąć.

Podaj rozwiązanie dla funkcji * oracle i * procedury składowanej.

Dzięki.

+0

wiosna ramy trwałości? Czy odnosisz się do Spring JdbcTemplate? Lub hibernacji? –

+0

Użyłem DPTK do stworzenia szkieletu trwałości i fabryki zapytań Spring. Teraz chcesz zadzwonić do funkcji Oracle lub procedura przechowywana przy użyciu istniejącej funkcjonalności, możesz pomóc mi to posortować .. –

+0

DPTK wydaje się być produktem IBM: http://www.alphaworks.ibm.com/tech/dptk –

Odpowiedz

32

Zakładając, że odnosząc się do 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 
     } 
    } 
); 

Wywołanie funkcji jest niemal identyczna:

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

tutaj nazywamy SP, jak możemy zadzwonić do funkcji Oracle ... Nie mam na myśli JdbcTemplate, ja odtwarzam strukturę utrwalania za pomocą DPTK .. –

+0

Drugi przykład to wywołanie funkcji. Niestety nie znam DPTK. Czy ma stronę internetową? –

+0

ok, spróbuję to zaimplementować z moim kodem ... mam nadzieję, że zadziała. Dzięki Adamowi da znać wynik. –

17

prostszy sposób wywoływania funkcji Oracle na wiosnę jest instacji StoredProcedure jak poniżej

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; 
    } 
} 

I nazwij to tak

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

Zastosowana tutaj funkcja Oracle przyjmuje tylko parametr numeryczny i zwraca parametr numeryczny.

+0

jak rozumiem, kod SQL powinien wyglądać tak: '{: param_out = call schema.package.MY_FUNCTION (: param_in)}' – mmoossen

+0

mój ostatni komentarz jest nieprawidłowy. stała SQL musi zawierać tylko nazwę procedury/funkcji, np. 'schema.package.MY_FUNCTION', oraz nazwy parametrów w Javie ** HAVE **, aby dopasować nazwy parametrów zgodnie z definicją w procedurze/funkcji. – mmoossen

0

Moim zdaniem jest to jeden z najprostszych sposobów:

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); 
     } 
} 
Powiązane problemy