The stored procedure is defined as follows:
Set ansi_nulls on
Go
Set quoted_identifier on
Go
Alter procedure sp_insertrequisition
@ ID bigint output,
@ Proposerid int,
@ Available bit,
@ Priority varchar (10 ),
@ Stage varchar (50)
As
Begin
Set nocount on;
Insert into [procurment]. [DBO]. [requisition]
([Proposerid]
, [Available]
, [Stage]
, [Priority])
Values
(@ Proposerid, @ available, @ stage, @ priority)
Select @ ID = ident_current ('requestition ')
End
Go
The configuration file is defined as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Sqlmap namespace = "requisition"
Xmlns = "http://ibatis.apache.org/mapping"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
<Statements>
<Insert id = "insertrequisitionparametermap" parameterclass = "requisition">
Insert into [procurment]. [DBO]. [requisition]
([Proposerid]
, [Available]
, [Stage]
, [Priority])
Values
(# Proposerid #, # available #, # stage #, # priority #)
<Selectkey resultclass = "int64" type = "Post" property = "ID">
Select @ identity as Value
</Selectkey>
</Insert>
<Procedure id = "sp_insertrequisition" parametermap = "insert-parameter">
Sp_insertrequisition
</Procedure>
</Statements>
<Parametermaps>
<Parametermap id = "insert-parameter" class = "requisition">
<Parameter property = "ID" direction = "output" dbtype = "bigint" column = "ID"/>
<Parameter property = "proposerid"/>
<Parameter property = "available" dbtype = "bit"/>
<Parameter property = "Priority"/>
<Parameter property = "stage"/>
</Parametermap>
</Parametermaps>
</Sqlmap>
You can use an SQL statement and specify <selectkey resultclass = "int64" type = "Post" property = "ID">
Select @ identity as Value
</Selectkey> to obtain the value of the current primary key. You can also run SP to obtain the output parameter.
When specifying a stored procedure, the important step is to map the output parameter name to the corresponding attribute of the object. Ibatis modifies the object property value as the ref type (the retrieveoutputparameters method is available in the mappedstatement class)
Program Code Call the following code:
Public void insert (requisition Requisition)
{
This. executeinsert ("sp_insertrequisition", requisition );
}
Defined in the parent class:
Protected object executeinsert (string statementname, object parameterobject)
{
Try
{
Isqlmapper sqlmap = This. getmapper ();
Return sqlmap. insert (statementname, parameterobject );
}
Catch (exception ex)
{
Throw new dataaccessexception ("error executing executeinsert. Cause:" + ex. Message, ex );
}
}