Ibatis.net Learning Note 13: Calling a stored procedure in ibatis.net

Source: Internet
Author: User

In fact, the method of invocation is relatively simple, mainly two types of stored procedures:
1. Update type of stored procedure
2. Query type of stored procedure
Here's a look at the specific invocation method:
1. Update type of stored procedure
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 (+) AS
BEGIN
Insert into accounts (account_id, Account_firstname, Account_lastname)
VALUES (@Account_ID, @Account_FirstName, @Account_LastName)
END

Map configuration file:

<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>

It is important to note that the number and order of parameters in the Parametermap are consistent with the sp_insertaccount stored procedure.

The calling code in ADO:

public void Insertaccountviastoreprocedure
{
Try
{
                 sqlmap.insert ("Insertaccountviastoreprocedure",  account);
            }
            catch  (DataAccessException  ex)
            {
                 throw new dataaccessexception ("Error executing insertaccountviastoreprocedure.  cause : " + ex. MESSAGE,&NBSP;EX);
            }
        }

Here is the method of Sqlmap.insert, in order to look intuitive, in fact, the use of the Sqlmap.queryforobject method is the same effect:)

2. Query type of stored procedure
getaccountbyname:

CREATE PROCEDURE [dbo]. [Getaccountbyname]
@name varchar (32)
As
BEGIN
SELECT * from accounts where account_firstname like '% ' + @name + '% '
END


Map configuration file:

<procedure id= "Getaccountbynameviastoreprocedure" resultmap= "Account-result" parametermap= "Selectpro-params" >
Getaccountbyname
</procedure>

<parametermap id= "Selectpro-params" class= "string" >
<parameter property= "Name"/>
</parameterMap>

Here Parametermap is also the same as the above requirements, as to the name of the property here does not have a practical role, can be arbitrarily named

The calling code in 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);
}
}http://www.cnblogs.com/firstyi/archive/2008/01/25/1053208.html

Ibatis.net Learning Note 13: Calling a stored procedure in ibatis.net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.