/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

Spring如何使用JdbcTemplate调用存储过程的三种情况

注:原文 《Spring如何使用JdbcTemplate调用存储过程的三种情况

 

 

Spring的SimpleJdbcTemplate将存储过程的调用进行了良好的封装,下面列出使用JdbcTemplate调用Oracle存储过程的三种情况:

一、无返回值的存储过程调用

 

1、存储过程代码:

  1 create or replace procedure sp_insert_table(param1 in varchar2,param2 in varchar2) as
  2    begin
  3        insert into table MyTable (id,name) values ('param1 ','param2');
  4    end sp_insert_table;
  5 

 

2、JdbcTemplate调用该存储过程代码:

 

  1 package com.dragon.test;
  2 import org.springframework.jdbc.core.JdbcTemplate;
  3 public class JdbcTemplateTest {
  4   private JdbcTemplate jdbcTemplate;
  5   public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  6   this.jdbcTemplate = jdbcTemplate;
  7   }
  8   public void test(){
  9      this.jdbcTemplate.execute("call sp_insert_table('100001')");
 10   }
 11 }

 

 

二、有返回值的存储过程(非结果集)

 

1、存储过程代码:

  1 create or replace procedure sp_select_table (param1 in varchar2,param2 out varchar2) as
  2  begin select into param2 from MyTable where ID = param1 ;
  3 end sp_insert_table ;

 

 

2、JdbcTemplate调用该存储过程代码:

  1 public void test() {
  2   String param2Value = (String) jdbcTemplate.execute(
  3      new CallableStatementCreator() {
  4         public CallableStatement createCallableStatement(Connection con) throws SQLException {
  5            String storedProc = "{call sp_select_table (?,?)}";// 调用的sql   
  6            CallableStatement cs = con.prepareCall(storedProc);
  7            cs.setString(1, "p1");// 设置输入参数的值   
  8            cs.registerOutParameter(2,OracleTypes.Varchar);// 注册输出参数的类型   
  9            return cs;
 10         }
 11      }, new CallableStatementCallback() {
 12          public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
 13            cs.execute();
 14            return cs.getString(2);// 获取输出参数的值   
 15      }
 16   });
 17 }

 

三、有返回值的存储过程(结果集)

 

1、存储过程代码

   先创建程序包,因为Oracle存储过程所有返回值都是通过out参数返回的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用package:

  1 create or replace package mypackage as
  2     type my_cursor is ref cursor;
  3     end mypackage;

 

存储过程代码:可以看到,列表是通过把游标作为一个out参数来返回的。 

  1 create or replace procedure sp_list_table(param1 in varchar2,param2 out mypackage.my_cursor) is
  2     begin
  3     open my_cursor for select * from myTable;
  4     end sp_list_table;

 

2、JdbcTemplate调用该存储过程代码:

 

  1 public void test() {
  2   List resultList = (List) jdbcTemplate.execute(
  3      new CallableStatementCreator() {
  4         public CallableStatement createCallableStatement(Connection con) throws SQLException {
  5            String storedProc = "{call sp_list_table(?,?)}";// 调用的sql   
  6            CallableStatement cs = con.prepareCall(storedProc);
  7            cs.setString(1, "p1");// 设置输入参数的值   
  8            cs.registerOutParameter(2, OracleTypes.CURSOR);// 注册输出参数的类型   
  9            return cs;
 10         }
 11      }, new CallableStatementCallback() {
 12         public Object doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException {
 13            List resultsMap = new ArrayList();
 14            cs.execute();
 15            ResultSet rs = (ResultSet) cs.getObject(2);// 获取游标一行的值   
 16            while (rs.next()) {// 转换每行的返回值到Map中   
 17               Map rowMap = new HashMap();
 18               rowMap.put("id", rs.getString("id"));
 19               rowMap.put("name", rs.getString("name"));
 20               resultsMap.add(rowMap);
 21            }
 22            rs.close();
 23            return resultsMap;
 24         }
 25   });
 26   for (int i = 0; i < resultList.size(); i++) {
 27      Map rowMap = (Map) resultList.get(i);
 28      String id = rowMap.get("id").toString();
 29      String name = rowMap.get("name").toString();
 30      System.out.println("id=" + id + ";name=" + name);
 31   }
 32 }

 

 

 

 

——————————————————————————————————————————————————————————————————————————————————————

posted @ 2018-04-16 17:10  一品堂.技术学习笔记  阅读(3972)  评论(0编辑  收藏  举报