Stored procedures are not used in small companies, but if the business is complex or the performance requirements are more stringent when the stored procedures come in handy, some of the earlier version of Ibatis does not seem to support stored procedures so I chose MyBatis to do the experiment.
1. No input and output parameters stored procedures, I wrote a relatively simple, it should be noted that the Oracle non-parametric stored procedures can not write parentheses
[SQL]
- CREATE OR REPLACE Procedure cascadeoperation
- As
- Begin
- Delete from teacher Where id=1;
- Update studentdetail Set address=' Ningbo Haishu District ' Where studentid=10;
- End;
Here do 2 operations, may have used mybatis people will be confused when the implementation of the use of the update tag or delete tag, in fact, I have tried the select tag is OK, the following is part of the configuration file
[HTML]
- <delete id= "cascadeoperation" statementtype="callable" >
- {Call Cascadeoperation}
- </Delete>
2. Stored procedures with input and output parameters, I've added a few judgments of if else here
[SQL]
- CREATE OR REPLACE Procedure queryteacher (FID in Integer,type with Varchar,Name out C12>varchar)
- As
- Begin
- If type=' 1 ' Then
- Select name into name from student Where Id=fid;
- Else if type=' 2 ' Then
- Select name into name from teacher Where Id=fid;
- Else
- name:=' error ';
- End If;
- End If;
- End;
I'll post the stored procedure statements that I executed in the command-line window by the way.
[SQL]
- Declare
- Name Varchar2 (50);
- Begin
- Queryteacher (3,' 2 ',Name);
- Dbms_output.put_line (Name);
- End;
- /
You may not see any output when you execute a similar statement, so don't worry, just use set serveroutput on the command line, and you can see my execution results.
See the results, the following use MyBatis to execute this stored procedure, the following is the mapping file
[HTML]
-
- <select id="Queryteacher" statementtype="callable" parametertype= "java.util.Map" >
-
- {Call Queryteacher (#{fid,mode= in,jdbctype=integer},#{type,mode= in,jdbctype= Varchar},#{name,mode= out,jdbctype=VARCHAR})}
-
- </Select>
How to get the contents of the return, in fact, as long as the stored procedure is executed after the map has value, Java code roughly as follows
[Java]
- map<string,object> mm=New hashmap<string,object> ();
- Mm.put ("FID", 3);
- Mm.put ("type", 2);
- M.queryteacher (mm);
- System.out.println (Mm.get ("name"));
The following is the result of the console output
3. There is also a stored procedure that returns a cursor that is similar to a collection of such
[SQL]
- CREATE OR REPLACE Procedure getteacher (cur_arg out sys_refcursor)
- As
- Begin
- Open Cur_arg for Select * from teacher;
- End;
This situation, in the mybatis is slightly different, at this time Jdbctype is Cursor,javatype is resultset, here can also turn the results into Resultmap, as shown below
[HTML]
- <resultmap id="resultMap3" type="Org.lxh.module.usefunction.info.Teacher">
- <result property="Address" column="Address"/>
- <result property="name" column="name"/>
- <result property="id" column="id"/>
- </resultmap>
[HTML]
- <select id="Getallteacherinfo" statementtype="callable" parametertype=" Java.util.Map " >
- {Call Getteacher (#{result,jdbctype=CURSOR,mode= out,javatype=ResultSet, resultmap=resultMap3})}
- </Select>
The Java code is a little bit more complicated here.
[Java]
- map<string, object> map = new hashmap<string, object> ();
- M.getallteacher (map);
- Set<map.entry<string, object>> set = Map.entryset ();
- for (iterator<map.entry<string, object>> it = Set.iterator (); it
- . Hasnext ();) {
- map.entry<string, object> Entry = (map.entry<string, object>) it
- Next ();
- //System.out.println (Entry.getkey () + "--->" +
- //(Teacher) Entry.getvalue ());
- list<teacher> t = (list<teacher>) entry.getvalue ();
- iterator<teacher> Itera = T.iterator ();
- While (Itera.hasnext ()) {
- Teacher TT = Itera.next ();
- System.out.println (Tt.getname () + "," + tt.getaddress ());
- }
- }
Here is the result of the execution
A cursor can be returned directly using the following method
Map map = new HashMap();
Map.put("jid", jid);
userInfoMapper.getFriendList(map);
//result is the return result name written in the mybatis xml file
List<UserInfo> list = (List<UserInfo>)map.get("result");
Return list;
MyBatis call stored procedure without parameters, with input and output parameter, output cursor type of storage