spring jdbc 调用存储过程封装




</pre><h1>1、调用存储过程--返回list以及无结果返回以及只返回一个参数</h1><pre code_snippet_id="671267" snippet_file_name="blog_20150520_2_9698049" name="code" class="java">
 
 

 
 
  1. /
  2. * 获取数据库内的存储过程--返回一个List
  3. * @param procedureName 存储过程名
  4. * @param inParameter 输入的参数
  5. * @param outParamter 输出的参数
  6. * @return
  7. /
  8. public List<Map> callProcedure(final String procedureName,final List inParameter,final List outParamter){
  9. if(procedureName== null || procedureName.length() == 0 ){
  10. return null;
  11. }
  12. //没有返回参数
  13. if(outParamter == null ){
  14. if(callProcedureWithoutOut(procedureName,inParameter)) return null;
  15. }
  16. List resultList = (List) DBUtility.getJdbcTemplate().execute(
  17. new CallableStatementCreator() {
  18. public CallableStatement createCallableStatement(Connection con) throws SQLException {
  19. int inSize = inParameter== null? 0:inParameter.size();
  20. int outSize = outParamter== null? 0:outParamter.size();
  21. StringBuffer sbsql = new StringBuffer();
  22. sbsql.append( "{call "+procedureName).append( "(");
  23. for( int i= 0;i<(inSize+outSize);i++){
  24. if(i == 0){
  25. sbsql.append( "?");
  26. } else{
  27. sbsql.append( ",?");
  28. }
  29. }
  30. sbsql.append( ")}");
  31. CallableStatement cs = con.prepareCall(sbsql.toString());
  32. // 设置输入参数的值
  33. if(inSize > 0 ){
  34. String typeName = null;
  35. for( int i= 0;i<inSize;i++){
  36. typeName = inParameter.get(i).getClass().getName().toString();
  37. cs.setObject(i+ 1, inParameter.get(i));
  38. }
  39. }
  40. // 注册输出参数的类型
  41. cs.registerOutParameter(inSize+ 1, OracleTypes.CURSOR);
  42. return cs;
  43. }
  44. }, new CallableStatementCallback<List>() {
  45. public List<Map> doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException {
  46. int inSize = inParameter== null? 0:inParameter.size();
  47. List<Map> resultsMap = new ArrayList();
  48. cs.execute();
  49. ResultSet rs = (ResultSet) cs.getObject(inSize+ 1); // 获取游标一行的值
  50. while (rs.next()) { // 转换每行的返回值到Map中
  51. Map rowMap = new HashMap();
  52. for( int i= 0;i<outParamter.size();i++){
  53. String outP = outParamter.get(i).toString();
  54. rowMap.put(outP.toLowerCase(),rs.getObject(outP.toUpperCase()) );
  55. }
  56. resultsMap.add(rowMap);
  57. }
  58. rs.close();
  59. return resultsMap;
  60. }
  61. });
  62. return resultList;
  63. }
  64. /
  65. 获取数据库内的存储过程--没有输出
  66. * @param procedureName 存储过程名
  67. * @param inParameter 输出的参数
  68. /
  69. public Boolean callProcedureWithoutOut(final String procedureName,final List inParameter){
  70. if(procedureName== null || procedureName.length() == 0 ){
  71. return false;
  72. }
  73. StringBuffer sbsql = new StringBuffer();
  74. sbsql.append( "{call "+procedureName).append( "(");
  75. for( int i= 0;i<inParameter.size();i++){
  76. if(i == 0){
  77. sbsql.append(inParameter.get(i));
  78. } else{
  79. sbsql.append( ","+inParameter.get(i));
  80. }
  81. }
  82. sbsql.append( ")}");
  83. DBUtility.getJdbcTemplate().execute(sbsql.toString());
  84. return true;
  85. }
  86. /*
  87. * 获取数据库内的存储过程--输出只有一个参数
  88. * @param procedureName
  89. * @param inParameter
  90. * @param outParamer
  91. * @return
  92. */
  93. public String callProcedureOnlyoneOut(final String procedureName,final List inParameter,final String outParamer){
  94. if(procedureName== null || procedureName.length() == 0 ){
  95. return null;
  96. }
  97. String result = (String) DBUtility.getJdbcTemplate().execute(
  98. new CallableStatementCreator() {
  99. public CallableStatement createCallableStatement(Connection con) throws SQLException {
  100. int inSize = inParameter== null? 0:inParameter.size();
  101. StringBuffer sbsql = new StringBuffer();
  102. sbsql.append( "{call "+procedureName).append( "(");
  103. for( int i= 0;i<(inSize+ 1);i++){
  104. if(i == 0){
  105. sbsql.append( "?");
  106. } else{
  107. sbsql.append( ",?");
  108. }
  109. }
  110. sbsql.append( ")}");
  111. CallableStatement cs = con.prepareCall(sbsql.toString());
  112. // 设置输入参数的值
  113. if(inSize > 0 ){
  114. String typeName = null;
  115. for( int i= 0;i<inSize;i++){
  116. typeName = inParameter.get(i).getClass().getName().toString();
  117. cs.setObject(i+ 1, inParameter.get(i));
  118. }
  119. }
  120. // 注册输出参数的类型
  121. cs.registerOutParameter(inSize+ 1, OracleTypes.CURSOR);
  122. return cs;
  123. }
  124. }, new CallableStatementCallback<Object>() {
  125. public Object doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException {
  126. int inSize = inParameter== null? 0:inParameter.size();
  127. List<Map> resultsMap = new ArrayList();
  128. cs.execute();
  129. return cs.getObject(inSize+ 1); // 获取游标一行的值
  130. }
  131. });
  132. return result;
  133. }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值