gpt4 book ai didi

Spring boot调用Oracle存储过程的两种方式及完整代码

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Spring boot调用Oracle存储过程的两种方式及完整代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

前言 。

因工作需要将公司SSH项目改为Spingboot项目,将项目中部分需要调用存储过程的部分用entityManagerFactory.unwrap(SessionFactory.class).openSession()来获取Session实现后发现项目访问数据库超过十次就会挂掉,原因是Springboot连接池数量默认为10,猜测是每次访问数据库后连接未释放导致的,手动关闭session后问题解决.

解决问题的过程中又发现了另外两种调用方式:

  • 直接用EntityManager的createStoredProcedureQuery()方法调用 (推荐)
  • 通过如下方式获取Session来调用,这种方式不需要手动关闭Session来释放连接,具体原因我也没搞明白,有知道的朋友欢迎指点
    Session session = entityManager.unwrap(Session.class);

完整代码 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.hzjd.produre.repository;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.ParameterMode;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.procedure.ProcedureCall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
import com.hzjd.produre.bean.QueryResponse;
import com.hzjd.produre.utils.Assistant;
 
@Repository
public class ProdureDAO {
     public final static String PUBLIC_PAG_SYS_GETNEXTID = "PUBLIC_PAG.SYS_GETNEXTID" ;
     public final static String PSBC_QUERYBILL = "PSBCPAY.QUERYBILL" ;
     @PersistenceContext
     EntityManager entityManager;
     @Autowired
     EntityManagerFactory entityManagerFactory;
 
     public Session getSession() {
         return entityManagerFactory.unwrap(SessionFactory. class ).openSession();
     }
 
     /**
      * 使用entityManager调用存储过程
      *
      * @param pay_ID
      * @return
      */
     public QueryResponse queryBill1(String pay_ID) throws Exception {
         QueryResponse queryResponse = new QueryResponse();
         StoredProcedureQuery call = entityManager.createStoredProcedureQuery(PSBC_QUERYBILL);
         call.registerStoredProcedureParameter( 1 , String. class , ParameterMode.IN).setParameter( 1 , pay_ID);
         call.registerStoredProcedureParameter( 2 , String. class , ParameterMode.OUT);
         call.registerStoredProcedureParameter( 3 , String. class , ParameterMode.OUT);
         call.registerStoredProcedureParameter( 4 , String. class , ParameterMode.OUT);
         call.registerStoredProcedureParameter( 5 , String. class , ParameterMode.OUT);
         call.registerStoredProcedureParameter( 6 , String. class , ParameterMode.OUT);
         call.registerStoredProcedureParameter( 7 , String. class , ParameterMode.OUT);
         call.registerStoredProcedureParameter( 8 , String. class , ParameterMode.OUT);
         call.registerStoredProcedureParameter( 9 , String. class , ParameterMode.OUT);
         call.registerStoredProcedureParameter( 10 , String. class , ParameterMode.OUT);
         call.execute();
         queryResponse.getBody().setPAY_ID(pay_ID);
         queryResponse.getBody().setCUSTNAME(Assistant.nullToEmpty(call.getOutputParameterValue( 2 )));
         queryResponse.getBody().setHOME_ADDR(Assistant.nullToEmpty(call.getOutputParameterValue( 3 )));
         queryResponse.getBody().setTRAN_AMT(Assistant.nullToEmpty(call.getOutputParameterValue( 5 )));
         queryResponse.getBody().setTOTAL_AMT(Assistant.nullToEmpty(call.getOutputParameterValue( 6 )));
         queryResponse.getBody().setBALANCE(Assistant.nullToEmpty(call.getOutputParameterValue( 8 )));
         int errorcode = Assistant.nullToInt(call.getOutputParameterValue( 9 ));
         String errormsg = Assistant.nullToEmpty(call.getOutputParameterValue( 10 ));
         if (errorcode == 0 ) {
             return queryResponse;
         } else {
             throw new Exception(errormsg);
         }
     }
 
     /**
      * 使用sessionFactory开启Session调用存储过程
      *
      * @param pay_ID
      * @return
      */
     public QueryResponse queryBill2(String pay_ID) throws Exception {
         QueryResponse queryResponse = new QueryResponse();
         // 调用完成后需关闭Session否则会出现连接失效
         try (Session session = getSession();) {
             ProcedureCall call = session.createStoredProcedureCall(PSBC_QUERYBILL);
             call.registerParameter( 1 , String. class , ParameterMode.IN).bindValue(pay_ID);
             call.registerParameter( 2 , String. class , ParameterMode.OUT);
             call.registerParameter( 3 , String. class , ParameterMode.OUT);
             call.registerParameter( 4 , String. class , ParameterMode.OUT);
             call.registerParameter( 5 , String. class , ParameterMode.OUT);
             call.registerParameter( 6 , String. class , ParameterMode.OUT);
             call.registerParameter( 7 , String. class , ParameterMode.OUT);
             call.registerParameter( 8 , String. class , ParameterMode.OUT);
             call.registerParameter( 9 , String. class , ParameterMode.OUT);
             call.registerParameter( 10 , String. class , ParameterMode.OUT);
             queryResponse.getBody().setPAY_ID(pay_ID);
             queryResponse.getBody().setCUSTNAME(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 2 )));
             queryResponse.getBody().setHOME_ADDR(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 3 )));
             queryResponse.getBody().setTRAN_AMT(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 5 )));
             queryResponse.getBody().setTOTAL_AMT(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 6 )));
             queryResponse.getBody().setBALANCE(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 8 )));
             int errorcode = Assistant.nullToInt(call.getOutputs().getOutputParameterValue( 9 ));
             String errormsg = Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 10 ));
             if (errorcode == 0 ) {
                 return queryResponse;
             } else {
                 throw new Exception(errormsg);
             }
         }
     }
 
     /**
      * 使用sessionFactory开启Session调用存储过程
      *
      * @param pay_ID
      * @return
      */
     public QueryResponse queryBill3(String pay_ID) throws Exception {
         QueryResponse queryResponse = new QueryResponse();
         Session session = entityManager.unwrap(Session. class );
         ProcedureCall call = session.createStoredProcedureCall(PSBC_QUERYBILL);
         call.registerParameter( 1 , String. class , ParameterMode.IN).bindValue(pay_ID);
         call.registerParameter( 2 , String. class , ParameterMode.OUT);
         call.registerParameter( 3 , String. class , ParameterMode.OUT);
         call.registerParameter( 4 , String. class , ParameterMode.OUT);
         call.registerParameter( 5 , String. class , ParameterMode.OUT);
         call.registerParameter( 6 , String. class , ParameterMode.OUT);
         call.registerParameter( 7 , String. class , ParameterMode.OUT);
         call.registerParameter( 8 , String. class , ParameterMode.OUT);
         call.registerParameter( 9 , String. class , ParameterMode.OUT);
         call.registerParameter( 10 , String. class , ParameterMode.OUT);
         queryResponse.getBody().setPAY_ID(pay_ID);
         queryResponse.getBody().setCUSTNAME(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 2 )));
         queryResponse.getBody().setHOME_ADDR(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 3 )));
         queryResponse.getBody().setTRAN_AMT(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 5 )));
         queryResponse.getBody().setTOTAL_AMT(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 6 )));
         queryResponse.getBody().setBALANCE(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 8 )));
         int errorcode = Assistant.nullToInt(call.getOutputs().getOutputParameterValue( 9 ));
         String errormsg = Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue( 10 ));
         if (errorcode == 0 ) {
             return queryResponse;
         } else {
             throw new Exception(errormsg);
         }
     }
}

总结 。

到此这篇关于Spring boot调用Oracle存储过程的两种方式及完整代码的文章就介绍到这了,更多相关Springboot调用Oracle存储过程内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://www.cnblogs.com/tiro996/p/13485980.html 。

最后此篇关于Spring boot调用Oracle存储过程的两种方式及完整代码的文章就讲到这里了,如果你想了解更多关于Spring boot调用Oracle存储过程的两种方式及完整代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com