首先在数据库创建存储过程:

create proc [dbo].[usp_GetPersonById]
@Id int
as
begin
select Id,Name from Person where Id=@Id
end

XML映射文件中定义参数集合:

  <parameterMaps>
<!--注意:parameterMap中的参数个数和顺序要和存储过程中的一致-->
<parameterMap id="GetPersonByIdProc">
<parameter property="Id" column="Id"/>
</parameterMap>
</parameterMaps>

然后定义操作:

    <!--执行存储过程-->
<procedure id="usp_GetPersonById" parameterMap="GetPersonByIdProc" resultClass="PersonViewModel">
usp_GetPersonById
</procedure>

DAO层:

public PersonViewModel GetPersonByIdProc(Hashtable ht)
{
PersonViewModel p = mapper.QueryForObject<PersonViewModel>("usp_GetPersonById", ht);
return p;
}

Main调用:

 static void Main(string[] args)
{
PersonDAO dao = new PersonDAO();
Hashtable ht = new Hashtable();
ht.Add("Id", );
PersonViewModel p = dao.GetPersonByIdProc(ht);
if (p != null)
Console.WriteLine(p.Id + p.Name); Console.ReadKey();
}

注意:Hashtable中的键值名称和参数集合中的property相对应,并且区分大小写。

执行带output输出参数的存储过程

修改下存储过程:

ALTER proc [dbo].[usp_GetPersonById]
@Id int,
@totalCount int output
as
begin
select Id,Name from Person where Id=@Id
set @totalCount=(select count(*) from Person)
end

XML映射文件中定义参数集合:

 <parameterMaps>
<!--注意:parameterMap中的参数个数和顺序要和存储过程中的一致-->
<parameterMap id="GetPersonByIdProc">
<parameter property="Id" column="Id"/>
<parameter property="totalCount" column="totalCount" direction="Output"/>
</parameterMap>
</parameterMaps>

定义操作和DAO层不变:

 <!--执行存储过程-->
<procedure id="usp_GetPersonById" parameterMap="GetPersonByIdProc" resultClass="PersonViewModel">
usp_GetPersonById
</procedure>

Main调用:

 static void Main(string[] args)
{
int totalCount = ;
PersonDAO dao = new PersonDAO();
Hashtable ht = new Hashtable();
ht.Add("Id", );
ht.Add("totalCount", totalCount);
PersonViewModel p = dao.GetPersonByIdProc(ht);
if (p != null)
Console.WriteLine(p.Id + p.Name+" 共"+ ht["totalCount"] + "条数据"); Console.ReadKey();
}

参考:http://www.cnblogs.com/caoyc/category/873268.html

Ibatis.Net 执行存储过程学习(八)的更多相关文章

  1. MyCat 学习笔记 第十三篇.数据分片 之 通过HINT执行存储过程

    1 环境说明 VM 模拟3台MYSQL 5.6 服务器 VM1 192.168.31.187:3307 VM2 192.168.31.212:3307 VM3 192.168.31.150:  330 ...

  2. Java中执行存储过程和函数(web基础学习笔记十四)

    一.概述 如果想要执行存储过程,我们应该使用 CallableStatement 接口. CallableStatement 接口继承自PreparedStatement 接口.所以CallableS ...

  3. Mysql学习---使用Python执行存储过程

    使用Python执行存储过程 使用Python执行存储过程[2部分]: 1.执行存储过程,获取存储过程的结果集  2.将返回值设置给了  @_存储过程名_序号 = #!/usr/bin/env pyt ...

  4. sqlserver学习2---java执行存储过程

    一.存储过程 1.新增操作存储过程 --------------1.新建 增加学生的存储过程---------------------------- set IDENTITY_INSERT stude ...

  5. C#学习-执行存储过程

    使用存储的优点 1.执行更快.直接写sql脚本会有个解析编译的过程. 2.修改方便.当业务改变时,只需要改存储过程,不需要修改C#代码 3.传递Sql脚本数据相对更小 缺点: 1.使用存储过程,数据库 ...

  6. Python Tutorial 学习(八)--Errors and Exceptions

    Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...

  7. mySql-数据库之存储过程学习总结

    之前在工作中总是听别人提到存储过程,觉得是个很高深的东西,利用工作之余,看了下相关的知识,现将学习知识总结如下,希望可以为刚学习的人提供些许帮助. 开发环境:Navicat For Mysql. My ...

  8. SQL server 存储过程学习

    一.定义变量--简单赋值 declare @a intset @a=5 print @a --使用select语句赋值 declare @user1 nvarchar(50) select @user ...

  9. Ibatis.Net 各种配置说明学习(二)

    1.各个配置文件的配置说明 providers.config:指定数据库提供者,.Net版本等信息. xxxxx.xml:映射规则. SqlMap.config:大部分配置一般都在这里,如数据库连接等 ...

随机推荐

  1. node之文件的静态资源的托管

    /** * 文件的静态资源托管 */ let express = require('express'); let path =require('path'); let app = express(); ...

  2. [搜狐科技]由浅入深理解Raft协议

    由浅入深理解Raft协议 2017-10-16 12:12操作系统/设计 0 - Raft协议和Paxos的因缘 读过Raft论文<In Search of an Understandable ...

  3. 外媒速递:系统管理员必须掌握的20条Linux命令

    [51CTO.com原创稿件]外媒速递是核子可乐精选的近日国外媒体的精彩文章推荐,希望大家喜欢! 今天推荐的内容包括:系统管理员必须掌握的20条Linux命令.五款最佳Linux屏幕记录应用.MySQ ...

  4. FuelPHP 系列(四) ------ Validate 验证

    一.可用规则: 1.required 不能为 null, false or empty string.: 2.required_with 关联某个字段,关联字段有值则该字段必须有值: 3.match_ ...

  5. 用Setup Factory7.0怎样打包delphi的BDE?

    BDE打包发布实例操作步骤如下: 使用软件:Setup Factory 7.0打包 把C:\Program Files\Common Files\Borland Shared中的所有文件和你的开发的应 ...

  6. 3分钟带你搞懂ES6 import 和 export

    如下语句是 default import: // B.js import A from './A' 且只在A存在 default export 时生效: // A.js export default ...

  7. 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)

    [BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...

  8. c++中的 Stl 算法(很乱别看)

    #include <iostream> #include <vector> #include <functional> #include <algorithm ...

  9. OpenStack 存储服务 Cinder存储节点部署LVM(十六)

    Cinder存储节点部署 部署在192.168.137.12主机 1.安装lvm2软件包 yum install lvm2 -y 2.启动LVM的metadata服务并且设置该服务随系统启动 syst ...

  10. python---redis管道(事务)和发布订阅

    管道:将数据操作放在内存中,只有成功后,才会一次性全部放入redis #管道(事务),要是都成功则成功,失败一个全部失败 #原理:将数据操作放在内存中,只有成功后,才会一次性全部放入redis pip ...