table of Contents
Two, call the stored procedure Demo
I. Introduction
We know that stored procedures can be executed in the following ways in plsql,
begin P_ACCOUNT(202004270000) ; end;
--或者
call P_ACCOUNT(202004270000)
But how to call a stored procedure in the code. I tried some methods provided by everyone on the Internet, but it was basically useless, including adding {call P_ACCOUNT(?,?)} after @Query what the name of the stored procedure, or various interfaces , The implementation classes call each other are the same. I don't know which copy is which has no effect.
Or these methods may be useful for their projects, and my posture may not be right, but in general, the following method is available in my personal test, and it is relatively simple to implement, and it can be implemented in less than ten lines of code.
Two, call the stored procedure Demo
//公众号灵儿的笔记:zygxsq
@Transactional
@Modifying
@Query
public void callProcedureSend(Long Id){
logger.info("调用存储过程P_ACCOUNT({})",new Object[]{Id});
StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("P_ACCOUNT");
storedProcedure.registerStoredProcedureParameter("A_ID", Long.class, ParameterMode.IN);
storedProcedure.registerStoredProcedureParameter("result", String.class, ParameterMode.OUT);
storedProcedure.setParameter("A_ID", Id);
boolean execute = storedProcedure.execute();
//博客原帖链接:https://blog.csdn.net/qq_27471405/article/details/105794591
//获取返回结果
String result = storedProcedure.getOutputParameterValue("result").toString();
}
Anti-hotlinking and anti-reptile instructions: search for Xiaoyuer Xiaolin's blog, and see more codes for free
Notes from the official account Linger: zygxsq
Link to the original post of this blog: https://blog.csdn.net/qq_27471405/article/details/105794591
All other places are stealing or crawling
Three, code description
P_ACCOUNT: is the name of the stored procedure in oracle
A_ID: Enter the parameter, if there are other parameters, write another line,
storedProcedure.registerStoredProcedureParameter("xxx parameter", parameter type, ParameterMode.IN);
The input parameter id here is a number type, so Long.class is used here, if it is a char type, String.class is used here
result: Here is the parameter name of a custom return value. If the stored procedure returns a return value, add this line of code. If not, you don’t need to add these two lines.
storedProcedure.registerStoredProcedureParameter("result", String.class , ParameterMode.OUT);
String result = storedProcedure.getOutputParameterValue("result").toString();
Reference article
https://www.it1352.com/956045.html
Thanks to the original author for sharing, so that technical people can solve the problem faster