700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > IBatis.Net学习笔记十三:在IBatis.Net中调用存储过程

IBatis.Net学习笔记十三:在IBatis.Net中调用存储过程

时间:2019-11-27 02:28:22

相关推荐

IBatis.Net学习笔记十三:在IBatis.Net中调用存储过程

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。