为了方便我们经常需要函数/过程返回结果集时采用返回cursor的方式得到结果集,在ibatis以前版本中支持的不是很好,
现在最新版本已经可以支持的很好了.
一.下载最新ibatis包.
二.最新的连接oracle 10g的驱动ojdbc(我这里用的是oracle 10g)
三.下载最新的common包.
示例如下:
函数定义:
- CREATE OR REPLACE FUNCTION GetCurTest(
- param1 in varchar2;
- param2 in varchar2;
- )return SYS_REFCURSOR
- IS
- outCursor SYS_REFCURSOR; --输出的游标
- BEGIN
- open outCursor for
- select ORGANCODE from testTable where col1 = GetCurTest.param1 and col2 = GetCurTest.param2;
- return outCursor;
- END;-- 注意这里用的是SYS_REFCURSOR,在oracle10g里面提供的.
CREATE OR REPLACE FUNCTION GetCurTest(
param1 in varchar2;
param2 in varchar2;
)return SYS_REFCURSOR
IS
outCursor SYS_REFCURSOR; --输出的游标
BEGIN
open outCursor for
select ORGANCODE from testTable where col1 = GetCurTest.param1 and col2 = GetCurTest.param2;
return outCursor;
END;-- 注意这里用的是SYS_REFCURSOR,在oracle10g里面提供的.
ibatis中调用如下:
- <typeAlias alias="corp" type="com.wes.model.Corp" />
- <resultMap id="corp-map" class="corp">
- <result property="corpNo" column="ORGANCODE" />
- </resultMap>
- <parameterMap id="test" class="java.util.HashMap" >
- <parameter property="result" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="corp-map" />
- <parameter property="param1" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>
- <parameter property="param2" jdbcType="Date" javaType="java.util.Date" mode="IN"/>
- </parameterMap>
- <procedure id="testCur" parameterMap="test">
- {? = call GetCurTest(?, ?)}
- </procedure>
<typeAlias alias="corp" type="com.wes.model.Corp" /> <resultMap id="corp-map" class="corp"> <result property="corpNo" column="ORGANCODE" /> </resultMap> <parameterMap id="test" class="java.util.HashMap" > <parameter property="result" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="corp-map" /> <parameter property="param1" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/> <parameter property="param2" jdbcType="Date" javaType="java.util.Date" mode="IN"/> </parameterMap> <procedure id="testCur" parameterMap="test"> {? = call GetCurTest(?, ?)} </procedure>
java中调用如下:
- String resource = "SqlMapConfig.xml";
- SqlMapClient client = null;
- try...{
- Reader reader = Resources.getResourceAsReader(resource);
- client = SqlMapClientBuilder.buildSqlMapClient(reader);
- }catch (Throwable e)...{}
- Map p = new HashMap();
- p.put("param1","a");
- p.put("param2","b");
- client.queryForObject(mapid, p);
- List list = (List) p.get("result");
- if (list == null || list.size() == 0)
- list = new ArrayList();
- return list;
String resource = "SqlMapConfig.xml";
SqlMapClient client = null;
try...{
Reader reader = Resources.getResourceAsReader(resource);
client = SqlMapClientBuilder.buildSqlMapClient(reader);
}catch (Throwable e)...{}
Map p = new HashMap();
p.put("param1","a");
p.put("param2","b");
client.queryForObject(mapid, p);
List list = (List) p.get("result");
if (list == null || list.size() == 0)
list = new ArrayList();
return list;
list存放的就是我们要的结果集.
4.Procedure
procedure 元素支持存储过程。大部分存储过程要一些类似IN, INOUT 或者 OUT 的参数。所以,创建一个 <parameterMap> 元素并列出你要向存储过程传递的参数。只有当参数类型为OUT或者INOUT时parameterMap 对象才会被改变。
- <parameterMap id="swapParameters" class="map" >
- <parameter property="contactId" jdbcType="INTEGER" javaType="java.lang.Integer" mode="IN"/>
- <parameter property="firstName" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>
- <parameter property="lastName" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>
- </parameterMap>
- <procedure id="swapContactName" parameterMap="swapParameters" >
- {call swap_contact_name (?, ?,?)}
- </procedure>
<parameterMap id="swapParameters" class="map" > <parameter property="contactId" jdbcType="INTEGER" javaType="java.lang.Integer" mode="IN"/> <parameter property="firstName" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/> <parameter property="lastName" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/> </parameterMap> <procedure id="swapContactName" parameterMap="swapParameters" > {call swap_contact_name (?, ?,?)} </procedure>
你的代码一开始就要创建一个你要传递给存储过程的参数的 HashMap ,然后把它和你要执行的查询的名称一同传递给 sqlMap 。
- HashMap paramMap = new HashMap();
- paramMap.put("contactId", new Integer(1));
- paramMap.put("firstName", "Sunil");
- paramMap.put("lastName", "Patil");
- sqlMap.queryForObject("swapCustomerName", paramMap);
HashMap paramMap = new HashMap();
paramMap.put("contactId", new Integer(1));
paramMap.put("firstName", "Sunil");
paramMap.put("lastName", "Patil");
sqlMap.queryForObject("swapCustomerName", paramMap);
5.Calling an Oracle procedure function using Ibatis
The ibatis documentation provides a sample on how to call a procedure, but how do you call a function in Oracle? The code sample shows it.
- <parameterMap id="getSomeFunction" class="java.util.Map" >
- <parameter property="question1" jdbcType="INTEGER" javaType="java.lang.String" mode="OUT"/>
- <parameter property="question2" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>
- <parameter property="question3" jdbcType="INTEGER" javaType="java.lang.String" mode="IN"/>
- <parameter property="question54" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>
- <parameter property="question5" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>
- </parameterMap>
- <procedure id="getSomeFunction" parameterMap="getSomeFunction">
- {? = call FNC_EXECUTE_SOME_FUNCTION(?, ?, ?, ?)}