您好,登錄后才能下訂單哦!
這篇文章主要介紹Spring boot調用Oracle存儲過程的方法有哪些,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
前言
因工作需要將公司SSH項目改為Spingboot項目,將項目中部分需要調用存儲過程的部分用entityManagerFactory.unwrap(SessionFactory.class).openSession()來獲取Session實現后發現項目訪問數據庫超過十次就會掛掉,原因是Springboot連接池數量默認為10,猜測是每次訪問數據庫后連接未釋放導致的,手動關閉session后問題解決。
解決問題的過程中又發現了另外兩種調用方式:
Session session = entityManager.unwrap(Session.class);
完整代碼
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存儲過程的方法有哪些的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。