• IBatis.Net中调用存储过程


    其实调用方式比较简单,主要也就是两种类型的存储过程:

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

    1 CREATE PROCEDURE [dbo].[sp_InsertAccount]
    2     -- Add the parameters for the stored procedure here
    3    @Account_ID int,
    4    @Account_FirstName varchar(32),
    5    @Account_LastName varchar(32)AS
    6 BEGIN
    7 insert into accounts (account_id, account_firstname, account_lastname) 
    8     values (@Account_ID,@Account_FirstName,@Account_LastName )
    9 END

    Map配置文件:

    1         <procedure id="InsertAccountViaStoreProcedure" parameterMap="insert-params_new">
    2             sp_InsertAccount
    3         </procedure>
    4 
    5     <parameterMap id="insert-params_new" class="Account">
    6       <parameter property="Id" />
    7       <parameter property="FirstName" />
    8       <parameter property="LastName" />
    9     </parameterMap>

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

    Ado中的调用代码:

     1         public void InsertAccountViaStoreProcedure(Account account)
     2         {
     3             try
     4             {
     5                 sqlMap.Insert("InsertAccountViaStoreProcedure", account);
     6             }
     7             catch (DataAccessException ex)
     8             {
     9                 throw new DataAccessException("Error executing InsertAccountViaStoreProcedure. Cause :" + ex.Message, ex);
    10             }
    11         }
    View Code

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

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

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

    Map配置文件:

    1     <procedure id="GetAccountByNameViaStoreProcedure" resultMap="account-result" parameterMap="selectpro-params">
    2       GetAccountByName
    3     </procedure>
    4 
    5     <parameterMap id="selectpro-params" class="string">
    6       <parameter property="name"/>
    7     </parameterMap>

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

    Ado中的调用代码:

     1        public ArrayList GetAccountByNameViaStoreProcedure(string strName)
     2         {
     3             try
     4             {
     5                 ArrayList list = (ArrayList)sqlMap.QueryForList("GetAccountByNameViaStoreProcedure", strName);
     6                 return list;
     7             }
     8             catch (DataAccessException ex)
     9             {
    10                 throw new DataAccessException("Error executing SqlAccountViaSqlMapDao.GetAccountById. Cause :" + ex.Message, ex);
    11             }
    12         }
  • 相关阅读:
    sql子查询
    java中entity和object的区别
    eclipse F3可以查询某个方法的具体定义
    SQL语句的MINUS,INTERSECT和UNION ALL
    jquery 循环获取checkBox的值,以及对复选框选中,取消,操作按钮
    jQuery 函数位于一个 document ready 函数中
    <script>的用法
    jquery ui-----弹出窗口 dialog
    util包就是用来放一些公用方法和数据结构的
    BigDecimal
  • 原文地址:https://www.cnblogs.com/Artemisblog/p/3707392.html
Copyright © 2020-2023  润新知