日期:2014-05-16  浏览次数:20580 次

(原创)Ibatis2调用数据库存储过程的相关示例

第一种方式,传入一个参数(非自定义java类型),返回单个值:

?

oracle代码:

???

CREATE OR REPLACE PROCEDURE testPro(bidObjectId? NUMBER,projectId OUT NUMBER)
AS
BEGIN
?????? SELECT bo.project_id INTO projectId FROM bm_t_bid_object bo WHERE bo.bid_object_id = bidObjectId;
? END;

??

Ibatis配置:

???? <parameterMap id="projectIdProc" class="java.util.Map">
??????? <parameter property="bidObjectId" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN" />
??????? <parameter property="result" jdbcType="VARCHAR" javaType="java.lang.String" mode="OUT"/>
??? </parameterMap>

?

<procedure id="getProjectIdProc" parameterMap="projectIdProc">
??????? { call testPro(?,?) }
</procedure>

?

Java调用代码:

???? public Long getProjectIdProc(Map map) throws Exception {
??????? this.getBaseDao().getSqlMapClientTemplate().queryForObject("getProjectIdProc",map);
??????? Long pId = Long.valueOf(map.get("result").toString());???????
??????? return pId;
??? }

?

Junit测试代码:基于org.springframework.test.AbstractDependencyInjectionSpringContextTests测试

????? public void testGetProjectIdProc() {
??????? HashMap parameters = new HashMap();
??????? parameters.put("bidObjectId","900000000000000003");
??????? Object o = null;
??????? try {
??????????? o = projectMgr.getProjectIdProc(parameters);
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? System.out.println(o);
??? }

?

第二种方式:?传入单个参数,返回游标结果集

?

oracle代码:

?

CREATE OR REPLACE PACKAGE myPage
AS
? TYPE myrctype IS REF CURSOR;
? PROCEDURE testListPro(projectId NUMBER,return_cursor OUT myrctype);
END;

?

CREATE OR REPLACE PACKAGE BODY myPage

AS
? PROCEDURE testListPro(projectId NUMBER,return_cursor OUT myrctype)
??? IS
??? sqlStr VARCHAR(200);
??? BEGIN
????? sqlStr := 'SELECT * FROM bm_t_bid_object bo WHERE bo.project_id =:p_id';
????? OPEN return_cursor FOR sqlStr USING projectId;
??? END testListPro;
END myPage;

?

Ibatis配置:

?

<resultMap id="bidObjectBasic" class="bidObject">
??????? <result property="id" column="BID_OBJECT_ID"/>
??????? <result property="objectNo" column="OBJECT_NO"/>
??????? <result property="objectName" column="OBJECT_NAME"/>
??? </resultMap>

?

??? <parameterMap id="bidObjectsProc" class="java.util.Map">
??????? <parameter property="projectId" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN" />
??????? <parameter property="result" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="bidObjectBasic"/>
??? </parameterMap>

?