Spring 执行存储过程示例

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

  1. JDBC Template
  2. NamedParameterJdbcTemplate
  3. SimpleJdbcCall

JDBC Template示例:

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

NamedParameterJdbcTemplate示例:

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

SimpleJdbcCall示例:

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

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×