Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call oracle stored procedure in mybatis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to call oracle storage procedures in mybatis. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope everyone can gain something according to this article.

1. Stored procedures without input and output parameters

I wrote a relatively simple, it should be noted that Oracle parameter-free stored procedures can not write parentheses

CREATE OR REPLACE Procedure cascadeoperationAsBegin Delete From teacher Where id=1; Update studentdetail Set address='Ningbo City Haishu District' Where studentid=10;End;

There are two operations performed here. People who may have used mybatis will be confused whether to use the update tag or the delete tag when executing it. In fact, both are OK. I have also tried the select tag and it is OK. Here are some configuration files.

{call cascadeoperation} 2. Stored procedures with input and output parameters

ah, i've added a few if else judgments here.

CREATE OR REPLACE Procedure queryTeacher(fid In Integer,Type In Varchar,Name Out Varchar)AsBeginIf Type='1' thenSelect Name Into Name From student Where id=fid;Else if Type='2' thenSelect Name Into Name From teacher Where id=fid;Else Name: ='ERROR '; End If;End If;End;

Here's how to paste the stored procedure statements I execute in the command line window

DeclareName Varchar2(50);Beginqueryteacher(3,'2',Name);DBMS_OUTPUT.put_line(Name);End;/

You may not see any output when executing similar statements, so don't worry. Just use set serveroutput on the command line.

See the result, below use mybatis to execute this stored procedure, below is the mapping file writing

{call queryTeacher(#{fid,mode=IN,jdbcType=INTEGER},#{type,mode=IN,jdbcType=VARCHAR},#{name,mode=OUT,jdbcType=VARCHAR})}

Then how to get the returned content? In fact, as long as the stored procedure is executed, there will be a value in the map. Java code is roughly as follows.

Map mm=new HashMap(); mm.put("fid", 3); mm.put("type", 2); m.queryTeacher(mm); System.out.println(mm.get("name")); Save 3. Return stored procedure of cursor

There's also a stored procedure that returns a cursor like a collection.

CREATE OR REPLACE Procedure getTeacher(cur_arg out Sys_Refcursor)Asbegin open cur_arg for Select * From teacher;End;

This situation is slightly different in mybatis, where jdbcType is CURSOR, javaType is ResultSet, and the result can also be converted to resultMap, as shown below.

{call GETTEACHER(#{result,jdbcType=CURSOR,mode=OUT,javaType=ResultSet, resultMap=resultMap3})}

Java code is a little bit more complicated here.

Map map = new HashMap(); m.getAllTeacher(map); Set set = map.entrySet(); for (Iterator it = set.iterator(); it .hasNext();) { Map.Entry entry = (Map.Entry) it .next(); // System.out.println(entry.getKey() + "--->" + // (Teacher)entry.getValue()); List t = (List) entry.getValue(); Iterator itera = t.iterator(); while (itera.hasNext()) { Teacher tt = itera.next(); System.out.println(tt.getName() + "," + tt.getAddress()); } }

Return cursor can be directly used in the following way: The original author's writing is too troublesome

Map map = new HashMap(); map.put("jid", jid); userInfoMapper.getFriendList(map); //result is the return result name written in the mybatis xml file List list = (List)map.get("result"); return list; After reading the above, do you have any further understanding of how to invoke oracle stored procedures in mybatis? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report