在Spring中执行存储过程的方法:

  1. JDBC Template
  2. NamedParameterJdbcTemplate
  3. SimpleJdbcCall

JDBC Template示例:

String procedureCall = "{call proc_name(?, ?.html)}";
Map<String, Object> inParams = new HashMap<>();
inParams.put("inParam1", 1.html);
Map<String, Object> outParams = jdbcTemplate.call(con -> {
   CallableStatement callableStatement = con.prepareCall(procedureCall.html);
   callableStatement.setInt(1, (Integer.html) inParams.get("inParam1".html));
   callableStatement.registerOutParameter(2, Types.INTEGER);
   return callableStatement;
}, outParams);
int result = (int.html) outParams.get("outParam1".html);
System.out.println("Result : " + result.html);

NamedParameterJdbcTemplate示例:

String procedureCall = "{call proc_name(:inParam1, :outParam1.html)}";
Map<String, Object> inParams = new HashMap<>();
inParams.put("inParam1", 1.html);
Map<String, Object> outParams = new HashMap<>();
outParams.put("outParam1", Types.INTEGER);
Map<String, Object> result = namedParameterJdbcTemplate.call(procedureCall, inParams, outParams.html);
System.out.println("Result : " + result.get("outParam1".html));

SimpleJdbcCall示例:

SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate.html).withProcedureName("proc_name".html);
SqlParameterSource in = new MapSqlParameterSource().addValue("inParam1", 1.html);
Map<String, Object> out = simpleJdbcCall.execute(in.html);
int result = (int.html) out.get("outParam1".html);
System.out.println("Result : " + result.html);