我的IBatisNet的Demo

这个Demo是按IBatisNet的Tutorial的代码修改,并可以完整的运行(Tutorial写了入门,但它提供的代码说明做的不好,还要看DevGuide)

程序包括:
数据库表 Person(和Tutorial一致,使用Access,用OleDb连接,后面的SqlMap定义连接串)
None.gif Name              Type            Size
None.gifPER_ID            
Long   Integer        4
None.gifPER_FIRST_NAME    
Text               40
None.gifPER_LAST_NAME     
Text               40
None.gifPER_BIRTH_DATE    Date
/ Time          8
None.gifPER_WEIGHT_KG     
Double              8
None.gifPER_HEIGHT_M      
Double              8
None.gif

NUnit测试客户端 TestPeron.cs(和Tutorial一致,使用NUnit2.0)
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  IBatisNet.DataMapper;
None.gif
using  NUnit.Framework;
None.gif
None.gif
namespace  TestPeople
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [TestFixture]
InBlock.gif    
public class PersonTest
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [Test]
InBlock.gif        
public void PersonList ()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// try it 
InBlock.gif
            IList people = Mapper.Instance().QueryForList("SelectAll",null);
InBlock.gif
InBlock.gif            
// test it 
InBlock.gif
            Assert.IsNotNull(people,"Person list not returned");
InBlock.gif            Assert.IsTrue(people.Count
>0,"Person list is empty"); 
InBlock.gif            Person person 
= (Person) people[0];
InBlock.gif            Assert.IsNotNull(person,
"Person not returned");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

Web页面测试WebForm1.aspx, WebForm1.aspx.cs
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestPeople.WebForm1"  %>
None.gif
< HTML >
None.gif  
< HEAD >
None.gif        
< title > WebForm1 </ title >
None.gif  
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Person"  method ="post"  runat ="server" >
None.gif            
< asp:Panel  ID ="pnlList"  Runat ="server" >
None.gif            
< H1 > Person List </ H1 >
None.gif            
< asp:DataGrid  id =dgList  Runat ="server" ></ asp:DataGrid >
None.gif            
</ asp:Panel ></ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
using  IBatisNet.DataMapper;
None.gif
using  IBatisNet.Common.Utilities;
None.gif
None.gif
None.gif
namespace  TestPeople
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid dgList;
InBlock.gif        
protected System.Web.UI.WebControls.Panel pnlList;
InBlock.gif
InBlock.gif        
private void List_Load ()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            dgList.DataSource 
= Mapper.Instance().QueryForList("SelectAll",null);
InBlock.gif            dgList.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if (!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                List_Load ();
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif



值对象Person.cs(这个类和数据库表是一致的,应该用代码生成器生成,网上有很多好的代码生成器,这里推荐一个CodeSmith)
None.gif using  System;
None.gif
None.gif
namespace  TestPeople
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Person 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Person
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int _Id;
InBlock.gif        
private string _FirstName;
InBlock.gif        
private string _LastName;
InBlock.gif        
private DateTime _BirthDate;
InBlock.gif        
private double _WeightInKilograms;
InBlock.gif        
private double _HeightInMeters;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Person()    dot.gif{}
InBlock.gif
InBlock.gif        
public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get    dot.gifreturn _Id;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set    dot.gif{_Id = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string FirstName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return _FirstName;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{_FirstName = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string LastName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return _LastName;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{_LastName = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public DateTime BirthDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return _BirthDate;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{_BirthDate = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public double WeightInKilograms
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return _WeightInKilograms;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{_WeightInKilograms = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public double HeightInMeters
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return _HeightInMeters;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{_HeightInMeters = value;}
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

数据库映射文件
Person.xml
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< sqlMap  namespace ="Person"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation ="SqlMap.xsd" >
None.gif    
< alias >
None.gif        
< typeAlias  alias ="Person"  assembly ="TestPeople.dll"  type ="TestPeople.Person"   />
None.gif    
</ alias >
None.gif    
< resultMaps >
None.gif        
< resultMap  id ="SelectAllResult"  class ="Person" >
None.gif            
< result  property ="Id"  column ="PER_ID"   />
None.gif            
< result  property ="FirstName"  column ="PER_FIRST_NAME"   />
None.gif            
< result  property ="LastName"  column ="PER_LAST_NAME"   />
None.gif            
< result  property ="BirthDate"  column ="PER_BIRTH_DATE"   />
None.gif            
< result  property ="WeightInKilograms"  column ="PER_WEIGHT_KG"   />
None.gif            
< result  property ="HeightInMeters"  column ="PER_HEIGHT_M"   />
None.gif        
</ resultMap >
None.gif    
</ resultMaps >
None.gif    
< statements >
None.gif        
< select  id ="SelectAll"  resultMap ="SelectAllResult" >
None.gif        select
None.gif        PER_ID,
None.gif        PER_FIRST_NAME,
None.gif        PER_LAST_NAME,
None.gif        PER_BIRTH_DATE,
None.gif        PER_WEIGHT_KG,
None.gif        PER_HEIGHT_M
None.gif        from PERSON
None.gif        
</ select >
None.gif    
</ statements >
None.gif
</ sqlMap >

SqlMap.xml
None.gif <? xml version="1.0" encoding="UTF-8"  ?>
None.gif
< sqlMapConfig  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation ="SqlMapConfig.xsd" >
None.gif    
< providers  file ="providers.config"   />
None.gif    
< settings >
None.gif        
< setting  useFullyQualifiedStatementNames ="false"   />
None.gif        
< setting  cacheModelsEnabled ="true"   />
None.gif    
</ settings >
None.gif    
< database >
None.gif        
< provider  name ="OleDb1.1"   />
None.gif        
< dataSource  name ="Access"  connectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Inetpub\\wwwroot\\TestPeople\\test.mdb"   />
None.gif    
</ database >
None.gif    
None.gif
None.gif    
< sqlMaps >
None.gif        
< sqlMap  resource ="Person.xml"   />
None.gif    
</ sqlMaps >
None.gif
</ sqlMapConfig >

数据库联接配置providers.config(都是官方标准的,这里就不贴了)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值