zoukankan      html  css  js  c++  java
  • Ibatis.Net 执行存储过程学习(八)

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

    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", 2);
                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 = 0;
                PersonDAO dao = new PersonDAO();
                Hashtable ht = new Hashtable();
                ht.Add("Id", 2);
                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

  • 相关阅读:
    starUML学习笔记一
    android 反编译教程
    android asyncTask 笔记
    android v13 的新特性
    ViewPage+Fragment+indicator+Tabhost效果
    android dp sp pt mm in px
    Tabhost+framgent+ViewPager滑动效果
    android studio github 项目导入问题
    Fragment 中 onCreate和onCreateView的区别
    android 抽屉式滑动demo
  • 原文地址:https://www.cnblogs.com/vanblog/p/8708030.html
Copyright © 2011-2022 走看看