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

IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程CREATE PROCEDURE [dbo].[sp_InsertAccount]
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程    
-- Add the parameters for the stored procedure here
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程
   @Account_ID int,
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程   
@Account_FirstName varchar(32),
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程   
@Account_LastName varchar(32)AS
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程
BEGIN
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程
insert into accounts (account_id, account_firstname, account_lastname) 
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程    
values (@Account_ID,@Account_FirstName,@Account_LastName )
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程
END
Map配置文件:
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程        <procedure id="InsertAccountViaStoreProcedure" parameterMap="insert-params_new">
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程            sp_InsertAccount
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程        
</procedure>
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程    
<parameterMap id="insert-params_new" class="Account">
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程      
<parameter property="Id" />
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程      
<parameter property="FirstName" />
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程      
<parameter property="LastName" />
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程    
</parameterMap>
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程

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

Ado中的调用代码:

IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程        public void InsertAccountViaStoreProcedure(Account account)
        }

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

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

IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程CREATE PROCEDURE [dbo].[GetAccountByName]
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程    
@name varchar(32)
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程
AS
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程
BEGIN
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程
select * from accounts where Account_FirstName like '%' + @name + '%'
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程
END

Map配置文件:
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程    <procedure id="GetAccountByNameViaStoreProcedure" resultMap="account-result" parameterMap="selectpro-params">
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程      GetAccountByName
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程    
</procedure>
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程    
<parameterMap id="selectpro-params" class="string">
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程      
<parameter property="name"/>
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程    
</parameterMap>
这里parameterMap也是和上面的要求一样,至于property的名字在这里没有实际作用,可以任意取名的

Ado中的调用代码:
IBatis.Net学习笔记(七):在IBatis.Net中调用存储过程        public ArrayList GetAccountByNameViaStoreProcedure(string strName)
        }

相关文章: