2017-10-17 163 views
0

我有以下存儲過程,它接受三個參數並返回三個參考遊標。在java spring中獲取oracle存儲過程結果集mvc

variable id refcursor 
variable item refcursor 
variable amount refcursor 
exec getdata(123,date1,date2, :id, :item, :amount) ; 

print id; 
print item; 
print amount; 

我有這個存儲過程輸出的三個結果集。我怎麼能在春天mvc調用這個,並顯示這三個結果集。我正在使用下面的代碼通過sql查詢獲取數據。但是現在我開發了一個存儲過程。所以我怎麼能稱這個SP輸出insted我的查詢輸出。

public Optional<List<student>> getStudentDetails(String id) { 

NamedParameterJdbcTemplate parameterJdbcTemplate = new 
NamedParameterJdbcTemplate(dataSource); 

MapSqlParameterSource namedParameters = new MapSqlParameterSource(); 
namedParameters.addValue("Id", id); 

List<student> studentList = 
parameterJdbcTemplate.query(StudentQueryRepository.STUDENT_DETAIL_QUERY, 
namedParameters, new studentDecodeRowMapper()); 

if (studentList.isEmpty()) { 
return Optional.empty(); 
} else { 
return Optional.of(studentList); 
} 

} 
+0

如果不需要,請刪除sql-server標記 – Tanner

回答

0

試試這個:

List<CommunicationContact> campaigns = jdbcTemplate.execute(
        new CallableStatementCreator() { 
         public CallableStatement createCallableStatement(Connection con) throws SQLException { 
          CallableStatement cs = con.prepareCall("{? = call MY_SERVICES.GET_CAMPAIGNS(?,?)}"); 
          cs.registerOutParameter(1, OracleTypes.CURSOR); 
          cs.setString(2, customer); 
          cs.setString(3, channel); 

          return cs; 
         } 
        }, 
        new CallableStatementCallback<List<CommunicationContact>>() { 

         @Override 
         public List<CommunicationContact> doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { 
          cs.execute(); 
          ResultSet rs = (ResultSet) cs.getObject(1); 
          List<CommunicationContact> communications = commContactRsExtractor.extractData(rs); 

          return communications; 
         } 
        } 
      ); 

而且我對數據庫的函數:

FUNCTION GET_CAMPAIGNS(p_cust IN VARCHAR2, 
            p_channel IN VARCHAR2) 
    RETURN SYS_REFCURSOR 
    IS 
    l_campaigns_cursor SYS_REFCURSOR; 
    BEGIN 
     BEGIN 
     OPEN l_campaigns_cursor FOR 
     SELECT... 

     EXCEPTION 
     WHEN OTHERS THEN 
     ... 
     END; 

     RETURN l_campaigns_cursor; 
    END GET_CAMPAIGNS; 

相信隨着OUT參數的存儲過程也會起作用。