Spring 带有 ResultSet 和 OutPut 参数的 JDBCTemplate 存储过程

Spring JDBCTemplate Stored Procedure with ResultSet and OutPut Parameter

我创建了一个存储过程,其中 returns 结果行和两个输出参数。 我无法在 spring 中找到任何可以从中获取 ResultSetoutPutParameters 的东西。 我想实现类似的目标 this 使用 Spring 框架。

我们在代码中使用类似下面的内容

public Map<String, Object> findData() {
        List prmtrsList = new ArrayList();
        prmtrsList.add(new SqlParameter(Types.VARCHAR));
        prmtrsList.add(new SqlParameter(Types.VARCHAR));
        prmtrsList.add(new SqlOutParameter("result", Types.VARCHAR));

        Map<String, Object> resultData = jdbcTemplate.call(connection -> {
            CallableStatement callableStatement = connection.prepareCall("{call STORED_PROC(?, ?, ?)}");
            callableStatement.setString(1, "first");
            callableStatement.setString(2, "last");
            callableStatement.registerOutParameter(3, Types.VARCHAR);
            return callableStatement;
        }, prmtrsList);
        return resultData;
    }
@SuppressWarnings("rawtypes")
    @Override
    public List<?> applyLeave(int leave_id, int emp_id, boolean is_emer_lev, int is_emer_appr_by, String emp_role,
            int rep_id, String reason, String backup_person, int bak_per_count, String leave_status, String comments,
            int leave_det_id, int leave_type, Timestamp leave_apply_frm_date, int no_of_days, Timestamp worked_date,
            String reason_for_cancellation, boolean active, boolean is_submit, int user_id) {

        StoredProcedureQuery query = entitymanager.createStoredProcedureQuery("sp_iu_leave");

        query.registerStoredProcedureParameter("p_leave_id", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_emp_id", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_is_emgen_leave", Boolean.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_is_emgen_leave_apr_by", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_emp_role", String.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_rpt_person_id", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_reason", String.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_backup_person", String.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_backup_person_cnt", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_leave_status", String.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_comments", String.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_leave_det_id", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_leave_type", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_leave_apply_frm_dt", Timestamp.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_no_of_days", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_worked_date", Timestamp.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_reason_for_cancellation", String.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_active", Boolean.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_is_submit", Boolean.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_usr_id", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("error_msg", String.class, ParameterMode.OUT);

        query.setParameter("p_leave_id", leave_id);
        query.setParameter("p_emp_id", emp_id);
        query.setParameter("p_is_emgen_leave", is_emer_lev);
        query.setParameter("p_is_emgen_leave_apr_by", is_emer_appr_by);
        query.setParameter("p_emp_role", emp_role);
        query.setParameter("p_rpt_person_id", rep_id);
        query.setParameter("p_reason", reason);
        query.setParameter("p_backup_person", backup_person);
        query.setParameter("p_backup_person_cnt", bak_per_count);
        query.setParameter("p_leave_status", leave_status);
        query.setParameter("p_comments", comments);
        query.setParameter("p_leave_det_id", leave_det_id);
        query.setParameter("p_leave_type", leave_type);
        query.setParameter("p_leave_apply_frm_dt", leave_apply_frm_date);
        query.setParameter("p_no_of_days", no_of_days);
        query.setParameter("p_worked_date", worked_date);
        query.setParameter("p_reason_for_cancellation", reason_for_cancellation);
        query.setParameter("p_active", active);
        query.setParameter("p_is_submit", is_submit);
        query.setParameter("p_usr_id", user_id);
        query.execute();

        String errString = (String) query.getOutputParameterValue("error_msg");
        System.out.println(errString);
        List res = query.getResultList();
        if(errString.equals("NO_ERR"))
        {
            return res;
        }
        else
        {
            return res;
        }

    }

您也可以使用 StoredProcedurequery 来 return 结果集,如果错误请回复谢谢!!