ibatis與存儲過程(帶輸出參數的存儲過程)

 

全部是針對Microsoft SQL Server 2000的Stored Procedure的例子:
參照《iBATIS in Action》書寫:
1.   max_in_example
Stored Procedure:

CREATE PROCEDURE [dbo].[max_in_example]
@a INTEGER = 0 OUTPUT,
@b INTEGER = 0 OUTPUT,
@c INTEGER = 0 OUTPUT
AS
 
BEGIN
       IF (@a > @b)
              SET @c = @a
       ELSE
              SET @c = @b
       RETURN @c
END
GO
 
SqlMap:

<parameterMap id="pm_in_example" class="java.util.Map">
       <parameter property="c" javaType="int" jdbcType="INTEGER"
           mode="OUT" />
       <parameter property="a" javaType="int" jdbcType="INTEGER" />
       <parameter property="b" javaType="int" jdbcType="INTEGER" />
 
 
    </parameterMap>
    <procedure id="in_example" parameterMap="pm_in_example"
       resultClass="int">
       { ? = call max_in_example(?, ?) }
    </procedure>
 
Java Code:

publicstatic Integer getMax_in_example(int a, int b) throws SQLException {
    Map<String, Integer> m = new HashMap<String, Integer>(2);
    m.put("a", new Integer(a));
    m.put("b", new Integer(b));
    m.put("c", new Integer(0));
    //執行存儲過程in_example
    sqlMapper.queryForObject("in_example", m);
   
    return m.get("c");
}
 
2.   swap
Stored Procedure:

CREATE PROCEDURE [dbo].[swap]
@a INTEGER OUTPUT,
@b INTEGER OUTPUT
 
AS
 
BEGIN
       DECLARE @temp INTEGER
 
       SET @temp = @a
       SET @a = @b
       SET @b = @temp
END
GO
 
SqlMap:

    <parameterMap id="swapProcedureMap" class="java.util.Map">
       <parameter property="a" javaType="int" jdbcType="INTEGER"
           mode="INOUT" />
       <parameter property="b" javaType="int" jdbcType="INTEGER"
           mode="INOUT" />
    </parameterMap>
    <procedure id="swapProcedure" parameterMap="swapProcedureMap">
       { call swap(?, ?) }
    </procedure>
 
Java Code:

publicstatic Map swap(int a, int b) throws SQLException {
    Map<String, Integer> m = new HashMap<String, Integer>(2);
    m.put("a", new Integer(a));
    m.put("b", new Integer(b));
   
    //執行存儲過程swap
    sqlMapper.queryForObject("swapProcedure", m);
   
    return m;
}
 
3.   maximum
Stored Procedure:

CREATE PROCEDURE [dbo].[maximum]
@a INT OUTPUT,
@b INT OUTPUT,
@c INT OUTPUT
 
AS
 
BEGIN
    IF(@a > @b)
         SET @c = @a
 
    IF(@b >= @a)
         SET @c = @b
END
GO
 
SqlMap:

<parameterMap id="maxOutProcedureMap" class="java.util.Map">
    <parameter property="a" mode="IN" />
    <parameter property="b" mode="IN" />
    <parameter property="c" jdbcType="INTEGER" mode="OUT" />
</parameterMap>
<procedure id="maxOutProcedure"parameterMap="maxOutProcedureMap">
       { call maximum (?, ?, ?) }
</procedure>
 
Java Code:

publicstatic Integer maximum(int a, int b) throws SQLException {
    Map<String, Integer> m = new HashMap<String, Integer>(2);
    m.put("a", new Integer(a));
    m.put("b", new Integer(b));
    m.put("c", new Integer(0));
   
    //執行存儲過程maximum
    sqlMapper.queryForObject("maxOutProcedure", m);
           
    return m.get("c");
}
 
以上的Java Code類方法都是寫在相應的ProcedureDAOImpl類中,可以通過ProcedureDAOImpl類調用相應得方法和傳入對應參數來與數據庫存儲過程交互。
 
注意:在SqlMap.xml文件中<parameterMap>中參數的順序跟<Procedure>中”?”的順序一致。
如:a, b, c; ? = procedurename(?, ?) 則,第一個問號表示a,依次類推。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章