其实调用方式比较简单,主要也就是两种类型的存储过程:
1、更新类型的存储过程
2、查询类型的存储过程
下面就来看看具体的调用方式:1、更新类型的存储过程sp_InsertAccount:
CREATEPROCEDURE[dbo].[sp_InsertAccount]--Addtheparametersforthestoredprocedurehere@Account_IDint,@Account_FirstNamevarchar(32),@Account_LastNamevarchar(32)ASBEGINinsertintoaccounts(account_id,account_firstname,account_lastname)values(@Account_ID,@Account_FirstName,@Account_LastName)END
Map配置文件:
<procedureid="InsertAccountViaStoreProcedure"parameterMap="insert-params_new">sp_InsertAccount</procedure><parameterMapid="insert-params_new"class="Account"><parameterproperty="Id"/><parameterproperty="FirstName"/><parameterproperty="LastName"/></parameterMap>
这里要注意的就是ParameterMap中的参数个数和顺序要和sp_InsertAccount存储过程中的一致
Ado中的调用代码:
publicvoidInsertAccountViaStoreProcedure(Accountaccount){try{sqlMap.Insert("InsertAccountViaStoreProcedure",account);}catch(DataAccessExceptionex){thrownewDataAccessException("ErrorexecutingInsertAccountViaStoreProcedure.Cause:"+ex.Message,ex);}}
这里使用的是sqlMap.Insert的方法,为了看起来直观一点,其实使用sqlMap.QueryForObject方法的话效果也是一样的:)2、查询类型的存储过程GetAccountByName:
CREATEPROCEDURE[dbo].[GetAccountByName]@namevarchar(32)ASBEGINselect*fromaccountswhereAccount_FirstNamelike'%'+@name+'%'END
Map配置文件:
<procedureid="GetAccountByNameViaStoreProcedure"resultMap="account-result"parameterMap="selectpro-params">GetAccountByName</procedure><parameterMapid="selectpro-params"class="string"><parameterproperty="name"/></parameterMap>
这里parameterMap也是和上面的要求一样,至于property的名字在这里没有实际作用,可以任意取名的
Ado中的调用代码:
publicArrayListGetAccountByNameViaStoreProcedure(stringstrName){try{ArrayListlist=(ArrayList)sqlMap.QueryForList("GetAccountByNameViaStoreProcedure",strName);returnlist;}catch(DataAccessExceptionex){thrownewDataAccessException("ErrorexecutingSqlAccountViaSqlMapDao.GetAccountById.Cause:"+ex.Message,ex);}} /firstyi/archive//01/25/1053208.html