zoukankan      html  css  js  c++  java
  • IBatis.Net学习笔记十三:在IBatis.Net中调用存储过程

    其实调用方式比较简单,主要也就是两种类型的存储过程:
    1、更新类型的存储过程
    2、查询类型的存储过程
    下面就来看看具体的调用方式:
    1、更新类型的存储过程
    sp_InsertAccount:

    CREATE PROCEDURE [dbo].[sp_InsertAccount]
        -- Add the parameters for the stored procedure here
       @Account_ID int,
       @Account_FirstName varchar(32),
       @Account_LastName varchar(32)AS
    BEGIN
    insert into accounts (account_id, account_firstname, account_lastname) 
        values (@Account_ID,@Account_FirstName,@Account_LastName )
    END

    Map配置文件:

            <procedure id="InsertAccountViaStoreProcedure" parameterMap="insert-params_new">
                sp_InsertAccount
            </procedure>

        <parameterMap id="insert-params_new" class="Account">
          <parameter property="Id" />
          <parameter property="FirstName" />
          <parameter property="LastName" />
        </parameterMap>

    这里要注意的就是ParameterMap中的参数个数和顺序要和sp_InsertAccount存储过程中的一致

    Ado中的调用代码:

            public void InsertAccountViaStoreProcedure(Account account)
            {
                try
                {
                    sqlMap.Insert("InsertAccountViaStoreProcedure", account);
                }
                catch (DataAccessException ex)
                {
                    throw new DataAccessException("Error executing InsertAccountViaStoreProcedure. Cause :" + ex.Message, ex);
                }
            }

    这里使用的是sqlMap.Insert的方法,为了看起来直观一点,其实使用sqlMap.QueryForObject方法的话效果也是一样的:)

    2、查询类型的存储过程
    GetAccountByName

    CREATE PROCEDURE [dbo].[GetAccountByName]
        @name varchar(32)
    AS
    BEGIN
    select * from accounts where Account_FirstName like '%' + @name + '%'
    END


    Map配置文件:

        <procedure id="GetAccountByNameViaStoreProcedure" resultMap="account-result" parameterMap="selectpro-params">
          GetAccountByName
        </procedure>

        <parameterMap id="selectpro-params" class="string">
          <parameter property="name"/>
        </parameterMap>

    这里parameterMap也是和上面的要求一样,至于property的名字在这里没有实际作用,可以任意取名的

    Ado中的调用代码:

            public ArrayList GetAccountByNameViaStoreProcedure(string strName)
            {
                try
                {
                    ArrayList list = (ArrayList)sqlMap.QueryForList("GetAccountByNameViaStoreProcedure", strName);
                    return list;
                }
                catch (DataAccessException ex)
                {
                    throw new DataAccessException("Error executing SqlAccountViaSqlMapDao.GetAccountById. Cause :" + ex.Message, ex);
                }
            }
     
    http://www.cnblogs.com/firstyi/archive/2008/01/25/1053208.html
  • 相关阅读:
    安防教育直播项目应用中RTSPSever组件libEasyRTSPServer编译arm版本报undefined reference to错误问题排查
    RTMP/RTSP/GB28181等协议TSINGSEE青犀能力开放平台汇总
    “Visual Studio.net已检测到指定的Web服务器运行的不是Asp.net1.1版。您将无法运行Asp.net Web应用程序或服务”问题的解决方案
    四大图像库的使用感受:OpenCV/FreeImage/CImg/CxImage
    我做的Java课件——http://sit.uibe.edu.cn/java/
    ARP协议工作原理
    ARP欺骗源码(基于WinPcap实现)
    多线程调用DBUS服务注意事项
    汉译英教程
    IIS5.1安装步骤及测试
  • 原文地址:https://www.cnblogs.com/soundcode/p/4981697.html
Copyright © 2011-2022 走看看