Spring 执行存储过程示例
在Spring中执行存储过程的方法:
- JDBC Template
- NamedParameterJdbcTemplate
- 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); 版权声明:本文为原创文章,版权归 戴老师的博客 所有,转载请联系博主获得授权。
本文地址:https://www.tushu.info/archives/archives-Spring-zhi-xing-cun-chu-guo-cheng-shi-li.html
如果对本文有什么问题或疑问都可以在评论区留言,我看到后会尽量解答。