ibatis 调用存储过程(2)

http://blog.csdn.net/fy_kenny/archive/2008/04/30/2350586.aspx

全部是针对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,依次类推。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fy_kenny/archive/2008/04/30/2350586.aspx

猜你喜欢

转载自fhqllt.iteye.com/blog/1059947