Ibatis.Net 学习手记一 简单的Demo

Ibatis.Net 学习手记  

    Wednesday, December 07, 2011

最近在做游戏推广的需求,趁公司给了不少充足的时间...再一次看了下自己以前学过的IbatisDemo,同时拿出来分享一下

Ibatis.Net的官方文档地址为 http://www.mybatis.org/dotnet.html  

下面晒一下我简单的Demo代码 


 Demo 中引用Castle.DynamicProxy.dll

                   IbatisNet.Common.dll

                   IbatisNet.Common.Logging.Log4Net.dll

                   IbatisNet.DataMapper.dll

                   log4net. dll

providers.config 是各类数据库mysql、sql server、Orcale的驱动配置文件,官方里面已经给出

SqlMap.config 需要配置一下:

<providers resource="providers.config" />  

一看就知道是引用providers.config配置文件

 

 <database>
    <provider name= " sqlServer2005 "></provider>
    <dataSource name= " IBatisNet "  connectionString= " server=localhost;database=IBatisDB;user id=sa;password=123;connection reset=false; "/>
  </database>

 

数据库方面的配置,也很容易知道不需要细讲

 

  <sqlMaps>
    <sqlMap resource= " Person.xml " />
  </sqlMaps>

 

将sql映射写到外部文件,其实可以直接在SqlMap.config里面写的,不过项目大的话不建议这么做。

 接下来是person.xml里面的配置了...其实就是映射一下属性跟数据库表, 里面有

<?xml version= " 1.0 " encoding= " utf-8 " ?>

<sqlMap  namespace= " IBatisNetDemo " xmlns= " http://ibatis.apache.org/mapping " xmlns:xsi= " http://www.w3.org/2001/XMLSchema-instance " >



  <alias>
    <typeAlias alias= " Person " type= " IBatisNetDemo.Person,IBatisNetDemo " />
  </alias>

  <statements>
    < select id= " SelectAll " resultMap= " SelectAllResult " >
       select
      Id,Name,Password
       from Person
    </ select>
    <update id = " UpdatePerson " parameterClass= " Person ">
      Update Person
       set Name=#Name#
       where Password=#Password#
    </update>
    < select id= " SelectByPassword " resultClass= " Person " parameterClass= " string ">
       select Name,Password  from Person  where Password=#value#
    </ select>
    <insert id= " InsertPerson " parameterClass= " Person ">
      INSERT INTO Person
      (
      [Name],
      [Password]
      )
      VALUES
      (
      #Name#,
      #Password#
      )
    </insert>
    <delete id= " DeletePersonByPassword " parameterClass= " string ">
      delete  from Person  where Password=#value#
    </delete>
    <delete id= " DeletePerson " parameterClass= " Person ">
      delete  from Person  where Name=#Name# and Password=#Password#
    </delete>
    <procedure id= " PrcInsertPerson " parameterMap= " personSwap ">
      InsertPerson
    </procedure>
    <procedure id= " PrcInsertPersonBackID " parameterMap= " personSwapTwo ">
      InsertPersonBackID
    </procedure>
    <procedure id= " PrcSelectPersonByName " parameterMap= " personSwapThree " resultMap= " personSwapThree ">
      SelectPersonByName
    </procedure>
  </statements>
  

  <parameterMaps>
    <parameterMap id= " personSwap "  class= " Person ">
      <parameter property= " Name " column= " Name " />
      <parameter property= " Password " column= " Password " />
    </parameterMap>

    <parameterMap id= " personSwapThree "  class= " Person ">
      <parameter property= " Name " column= " Name " />
    </parameterMap>

    <parameterMap id= " personSwapTwo "  class= " Person ">
      <parameter property= " Name " column= " Name " />
      <parameter property= " Password " column= " Password " />
      <parameter property= " ID " column= " ID "  direction= " Output " /> 
    </parameterMap>
  </parameterMaps>

  <resultMaps >

    <resultMap id= " SelectAllResult "  class= " Person ">
      <result property= " ID " column= " Id " />
      <result property= " Name " column= " Name " />
      <result property= " Password " column= " Password " />
    </resultMap>

    <resultMap id= " personSwapThree "  class= " Person ">
      <result property= " Name " column= " Name " />
      <result property= " Password " column= " Password " />
      <result property= " ID " column= " ID " />
    </resultMap>

  </resultMaps>


学习用的,所以只测试了下增删改查、还有调用存储过程等的不同方式实现方法。

实现的形式也只有一种,个人觉得会一种就够了,学样东西没问题一看所有方式都要会。想要的话自己看官方文档就好了。

 person.cs类

using System;
using System.Data;
using System.Text;
namespace IBatisNetDemo
{
     ///   <summary>
    
///  类Person。
    
///   </summary>
    [Serializable]
     public  class Person
    {
         public Person()
        { }
         #region Model
         private  int _id;
         private  string _name;
         private  string _password;

         public  int ID
        {
             set { _id = value; }
             get {  return _id; }
        }
         ///   <summary>
        
///  
        
///   </summary>
         public  string Name
        {
             set { _name = value; }
             get {  return _name; }
        }
         ///   <summary>
        
///  
        
///   </summary>
         public  string Password
        {
             set { _password = value; }
             get {  return _password; }
        }
         #endregion Model
    }

Default.aspx.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IBatisNet.Common;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;

namespace IBatisNetDemo
{
     public  partial  class _Default : System.Web.UI.Page
    {
         protected  void Page_Load( object sender, EventArgs e)
        {


        }
         ///   <summary>
        
///  配置SQL映射
        
///   </summary>
        
///   <returns> SQL映射 </returns>
         public ISqlMapper getMapper()
        {
            DomSqlMapBuilder builder =  new DomSqlMapBuilder();
            ISqlMapper Map = builder.Configure( " SqlMap.config ");
             return Map;
        }
         ///   <summary>
        
///  实例化Person
        
///   </summary>
         public Person getPerson
        {
             get
            {
                Person p =  new Person
                {
                    Name = txtname.Text.ToString(),
                    Password = txtpassword.Text.ToString()
                };
                 return p;
            }
        }
         ///   <summary>
        
///  插入Person
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         protected  void btnInsert_Click( object sender, EventArgs e)
        {
            getMapper().Insert( " InsertPerson ", getPerson);
        }
         ///   <summary>
        
///  修改Person
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         protected  void btnUpdate_Click( object sender, EventArgs e)
        {
            getMapper().Update( " UpdatePerson ", getPerson);
        }
         ///   <summary>
        
///  查找Person
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         protected  void btnSelect_Click( object sender, EventArgs e)
        {
             // IList<Person> plist = getMapper().QueryForList<Person>("SelectAll", null);
            IList<Person> plist = Mapper.Instance().QueryForList<Person>( " SelectAll "null);
            gvPersonData.DataSource = plist;
            gvPersonData.DataBind();
        }
         ///   <summary>
        
///  按密码查找
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         protected  void btnSelectByPassword_Click( object sender, EventArgs e)
        {
            IList<Person> plist = Mapper.Instance().QueryForList<Person>( " SelectByPassword ", txtpassword.Text.ToString());
             if (plist !=  null && plist.Count >  0)
            {
                gvPersonData.DataSource = plist;
                gvPersonData.DataBind();
            }
        }
         ///   <summary>
        
///  删除Person
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         protected  void btnDelete_Click( object sender, EventArgs e)
        {
             int i = Mapper.Instance().Delete( " DeletePerson ", getPerson);
        }
         ///   <summary>
        
///  按密码删除
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         protected  void btnDeleteByPassword_Click( object sender, EventArgs e)
        {
             int i = Mapper.Instance().Delete( " DeletePersonByPassword ", txtpassword.Text.ToString());
        }
         ///   <summary>
        
///  存储过程操作插入Person
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         protected  void btnPrcInsert_Click( object sender, EventArgs e)
        {
            Mapper.Instance().Insert( " PrcInsertPerson ", getPerson);
        }
         ///   <summary>
        
///  存储过程插入Person
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         protected  void btnPrcInsertBack_Click( object sender, EventArgs e)
        {
            Person p =  new Person
            {
                Name =  " xiede ",
                Password =  " 123456 "
            };
            Mapper.Instance().Insert( " PrcInsertPersonBackID ", p);

            Response.Write(p.ID.ToString());
        }
         ///   <summary>
        
///  存储过程按姓名查找Person
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         protected  void btnPrcSelect_Click( object sender, EventArgs e)
        {
            IList<Person> plist = Mapper.Instance().QueryForList<Person>( " PrcSelectPersonByName ", getPerson);

          gvPersonData.DataSource = plist;
          gvPersonData.DataBind();
        }

    }


 看过的人都会觉得这样的实现方式相当简单,确实在我之前跟进一个项目时也用到了Ibatis,记得人家还特意封装了下Ibatis。

而Ibatis给我的感觉还是比较多的配置。配置这些都是简单的事情。我也刚学不久,还是先把理论下下来,呵呵!

 

附上一下源代码

 

 

 

 

 

转载于:https://www.cnblogs.com/xoray007/archive/2011/12/07/2278958.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值