我们在代码中使用类似以下内容
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;
}
我制作了一个简单的 library 来隐藏样板部分以获取
OUT
参数的结果,它是类型安全的并且支持引用游标(Maven Central 上提供的库)。它的工作原理超出了 spring-jdbc。对于具有两个 OUT 参数的给定示例过程
CREATE PROCEDURE proc_extras_tab(extra_string varchar2, out_extra_string OUT varchar2, v_cur OUT sys_refcursor)
is
begin
out_extra_string := extra_string;
open v_cur for
select
simple_decode(regexp_replace(pair, '([^=]+)(=)(.+)', '\1')) id,
simple_decode(regexp_replace(pair, '([^=]+)(=)(.+)', '\3')) value
from (
select regexp_substr(extra_string, '[^;]+', 1, level)
as pair
from dual
connect by instr(extra_string, ';', 1, level) > 0
);
end;
通话看起来像这样
import static org.morejdbc.SqlTypes.VARCHAR;
import static org.morejdbc.OracleSqlTypes.cursor;
import static org.morejdbc.NamedJdbcCall.call;
import org.morejdbc.Out;
...
Out<String> outExtraString = Out.of(VARCHAR);
Out<List<Entity>> outExtras = Out.of(cursor((rs, rowNum) -> {
// spring RowMapper reading cursor as ResultSet to entity
return new Entity(rs.getString("id"), rs.getString("value"));
}));
jdbcTemplate.execute(call("proc_extras_tab")
.in("extra_string", "1=value1;2=value2;6=value6;")
.out("out_extra_string", outExtraString)
.out("v_cur", outExtras));
// outExtras.get() has a List<Entity> from cursor ResultSet
@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返回结果集,如果错误请回复谢谢!!