fy_kenny的专栏

随风奔跑, 爱自由~

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,依次类推。
阅读更多
个人分类: ibatis
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭