Stored Procedure
The following example shows getting character delimited list of
distinct hobbies from a oracle stored procedure.
As procedures doesn't return a value, we need to map an OUT or INOUT parameter
to get result back from the stored procedure.
We can specify the same using mode attribute
of parameter tag. It takes IN, OUT, INOUT as values.
01
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
02
<!DOCTYPE sqlMap
PUBLIC
"-//ibatis.apache.org//DTD SQL Map 2.0//EN"
05
<sqlMap namespace=
"Student"
>
07
<parameterMap id=
"hobbyParams"
class=
"map"
>
08
<parameter property=
"sep"
jdbcType=
"VARCHAR"
javaType=
"java.lang.String"
mode=
"IN"
/>
09
<parameter property=
"hobbyStr"
jdbcType=
"VARCHAR"
javaType=
"java.lang.String"
mode=
"OUT"
/>
12
<
procedure
id=
"hobbyList"
parameterMap=
"hobbyParams"
>
13
{ call distinct_hobbies( ? , ? ) }
02
create
or
replace
procedure
distinct_hobbies (sep
IN
VARCHAR2, hobbyStr
OUT
VARCHAR2)
is
04
CURSOR
hobby_cur
IS
SELECT
distinct
(hobby)
as
hobby
FROM
STUDENT;
05
hobby_rec hobby_cur%rowtype;
09
FOR
hobby_rec
in
hobby_cur
11
IF hobby_cur%ROWCOUNT = 1
THEN
12
hobbyStr := hobby_rec.hobby;
14
hobbyStr := hobbyStr || sep || hobby_rec.hobby;
package com.bethecoder.tutorials.ibatis.tests.basic;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class StoredProcedure {
/**
* @param args
* @throws IOException
* @throws SQLException
*/
public static void main ( String [] args ) throws IOException, SQLException {
Reader reader = Resources.getResourceAsReader ( "SqlMapConfig.xml" ) ;
SqlMapClient sqlMapClent = SqlMapClientBuilder.buildSqlMapClient ( reader ) ;
Map<String, String> dataMap = new HashMap<String, String> () ;
dataMap.put ( "sep" , "," ) ;
System.out.println ( "Before Proc Call : " + dataMap ) ;
sqlMapClent.queryForObject ( "Student.hobbyList" , dataMap ) ;
System.out.println ( "After Proc Call : " + dataMap ) ;
dataMap = new HashMap<String, String> () ;
dataMap.put ( "sep" , "#" ) ;
System.out.println ( "Before Proc Call : " + dataMap ) ;
sqlMapClent.queryForObject ( "Student.hobbyList" , dataMap ) ;
System.out.println ( "After Proc Call : " + dataMap ) ;
}
}
It gives the following output,
Before Proc Call : {sep=,}
After Proc Call : {hobbyStr=Reading books,Cricket,Painting,Football , sep=,}
Before Proc Call : {sep=#}
After Proc Call : {hobbyStr=Reading books#Cricket#Painting#Football , sep=#}