java 호출 저장 프로세스 - 커서 해석 반환 집합

12800 단어

// String procedureName = PNAME_SINGLE_INDEX_DETAIL_LIST; // List params = Lists.newArrayList(singleIndexDetailRequestVo.getCurOrgId(), singleIndexDetailRequestVo.getCurOrgType(), singleIndexDetailRequestVo.getTimeDim(), singleIndexDetailRequestVo.getIndexId(),singleIndexDetailRequestVo.getPageNo(),singleIndexDetailRequestVo .getPageSize()); // BI List> resultSet = getResultSet(procedureName, params);

2. 매개변수 변환 자리 표시자 처리
 public List> getResultSet(String procedureName, List params) {
    List> resultSet = null;
    try {
      String placeHolder = Joiner.on(",").join(params).replaceAll("\\w+", "?");
      resultSet = getCallProcedureResult(String.format("{call %s(%s,?)}", procedureName, placeHolder), params);
    } catch (Exception e) {
      resultSet = Lists.newArrayList();
      logger.error("call procedure:" + procedureName + " happened an exception:" + e.getMessage());
    }
    return resultSet;
  }

3. 저장 프로세스의 핵심 방법을 호출한다
/**
     *
     * @param procedureName               {call testpro(?,?)}                 
     * @param params                ,                    
     * @return
     */
    @Override
    @SuppressWarnings("unchecked")
    public List> getCallProcedureResult(final String procedureName, final List params) {
        final long startTime = System.currentTimeMillis();
        logger.info("        【"+procedureName+"】,  【" + JSON.toJSONString(params) + "】");
        List> resultList = null;
        try {
            resultList = (List>) biJdbcTemplate.execute(
                new CallableStatementCreator() {
                    public CallableStatement createCallableStatement(Connection con) throws SQLException {
                        String storedProc = procedureName;//    sql
                        CallableStatement cs = con.prepareCall(storedProc);
                        for (int i=0; i< params.size();i++) {
                            cs.setString((i+1), params.get(i));//         
                        }
                        cs.registerOutParameter((params.size()+1),OracleTypes.CURSOR);//          
                        return cs;
                    }
                }, new CallableStatementCallback() {
                    public Object doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException {
                        List> resultsMap = new ArrayList>();
                        cs.execute();
                        ResultSet rs = (ResultSet) cs.getObject((params.size()+1));;//                     
                        if (rs!=null) {
                            ResultSetMetaData rsmd = rs.getMetaData();
                            List columNames = new ArrayList();
                            for(int i=1; i<= rsmd.getColumnCount(); i++){
                                columNames.add(rsmd.getColumnName(i)); //      List 
                            }
                            if (!CollectionUtils.isEmpty(columNames)) {
                                while (rs.next()) {//          Map 
                                    Map rowMap = new HashMap();
                                    for (String columName : columNames) {
                                        rowMap.put(columName, rs.getObject(columName));
                                    }
                                    resultsMap.add(rowMap);
                                }
                            }
                            rs.close();
                        }
                        return resultsMap;
                    }
                });
            final long endTime = System.currentTimeMillis();
            logger.info("        【"+procedureName+"】,  【"+ JSON.toJSONString(params) + "】,            【"+resultList.size()+"】   :" + (endTime-startTime) + "  ");
            logger.info("            :"+JSON.toJSONString(resultList));
            final List> finalResultList = resultList;
            jdbcTemplate.update("INSERT INTO PROCEDURE_LOGS VALUES(?,?,?,?,?,?,?)",
                new PreparedStatementSetter() {
                    public void setValues(PreparedStatement ps) throws SQLException {
                        ps.setString(1, JugHelper.generalUUID());
                        ps.setString(2, procedureName);
                        ps.setString(3, JSON.toJSONString(params));
                        ps.setString(4, JSON.toJSONString(finalResultList));
                        ps.setTimestamp(5, new Timestamp(new Date().getTime()));
                        ps.setLong(6, (endTime-startTime));
                        ps.setString(7, "1");//   
                    }
                }
            );
        } catch (Exception e) {
            final long endTime = System.currentTimeMillis();
            final String errorMsg = getStackTrace(e);
            jdbcTemplate.update("INSERT INTO PROCEDURE_LOGS VALUES(?,?,?,?,?,?,?)",
                    new PreparedStatementSetter() {
                        public void setValues(PreparedStatement ps) throws SQLException {
                            ps.setString(1, JugHelper.generalUUID());
                            ps.setString(2, procedureName);
                            ps.setString(3, JSON.toJSONString(params));
                            ps.setString(4, errorMsg);
                            ps.setTimestamp(5, new Timestamp(new Date().getTime()));
                            ps.setLong(6, (endTime-startTime));
                            ps.setString(7, "0");//   
                        }
                    }
            );
        }
        return resultList;
    }

테스트 커서
CREATE OR REPLACE PROCEDURE TEST_SAULE_DETAIL_BING(
CUR_ORG_ID       VARCHAR2, --    Id
CUR_ORG_TYPE     VARCHAR2, --     1:  6:  4:  2:  99:   9:  
INDEX_DIM         varchar2,
INDEX_ID         varchar2,
test_cursor out sys_refcursor
)
AS
BEGIN
OPEN test_cursor for SELECT * FROM SAULSE_BING_TEST;
END TEST_SAULE_DETAIL_BING ;

 
전재 대상:https://www.cnblogs.com/pypua/articles/9622576.html

좋은 웹페이지 즐겨찾기