众所周知,Ibatis的核心就在于映射文件(Data Map XML File),在调用存储过程返回取得返回参数的过程中,最有可能出错的地方,就是映身文件的配置
我的开发环境地是(VS2008(.Net2.0)+Win2003Server+Oracle 10g)
直接进入主题,先来看一个资料
调用存储过程只能用到<procedure>(当然,我在博客园也见到一些朋友介绍说可以使用<statement>标 签,但经过小弟在测试调用Oracle数据库一直报错String类型没有长度限制,后来改成<procedure>就正常了)
<statements>
<procedure id="testProcedure" parametersMap="paramMap">
Test.TestProcedure
</procedure>
</statements>
<parameterMaps>
<parameterMap id="paramMap" class="Hashtable">
<parameter property="ID" column="PS_ID" />
<parameter property="UserName" column="PS_USERNAME" />
<parameter property="PassWord" column="PS_PASSWORD" />
<parameter property="returnValue" column="PS_RETURNVALUE" direction="Output" />
</parameterMap>
</parameterMaps>
<procedure id="testProcedure" parametersMap="paramMap">
Test.TestProcedure
</procedure>
</statements>
<parameterMaps>
<parameterMap id="paramMap" class="Hashtable">
<parameter property="ID" column="PS_ID" />
<parameter property="UserName" column="PS_USERNAME" />
<parameter property="PassWord" column="PS_PASSWORD" />
<parameter property="returnValue" column="PS_RETURNVALUE" direction="Output" />
</parameterMap>
</parameterMaps>
class一定设置class="Hashtable",当然也可以根据你的实际情况填写;在本文中是以Hashtable为例
Column属性一定要全部大写,否则会报错,找不到该参数(Oracle)
Hashtable map = new Hashtable();
map.Add("ID", 1);
map.Add("UserName", "user");
map.Add("PassWord", "pwd");
map.Add("returnValue", -1);
mapper.Insert("testProcedure", map);
Console.WriteLine(map["returnValue"].ToString());
map.Add("ID", 1);
map.Add("UserName", "user");
map.Add("PassWord", "pwd");
map.Add("returnValue", -1);
mapper.Insert("testProcedure", map);
Console.WriteLine(map["returnValue"].ToString());
Ibatis使用简单,就是配置要注意,注意大小写敏感,一般是不会出错的~
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
-- 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>
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);
}
}
{
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
@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>
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);
}
}
{
try
{
ArrayList list = (ArrayList)sqlMap.QueryForList("GetAccountByNameViaStoreProcedure", strName);
return list;
}
catch (DataAccessException ex)
{
throw new DataAccessException("Error executing SqlAccountViaSqlMapDao.GetAccountById. Cause :" + ex.Message, ex);
}
}