我的IBatisNet的Demo

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

程序包括:
数据库表 Person(和Tutorial一致,使用Access,用OleDb连接,后面的SqlMap定义连接串)
Name              Type            Size
PER_ID            
Long Integer      4
PER_FIRST_NAME    
Text             40
PER_LAST_NAME     
Text             40
PER_BIRTH_DATE    Date
/Time         8
PER_WEIGHT_KG     
Double            8
PER_HEIGHT_M      
Double            8

NUnit测试客户端 TestPeron.cs(和Tutorial一致,使用NUnit2.0)
using System;
using System.Collections;
using IBatisNet.DataMapper;
using NUnit.Framework;

namespace TestPeople
{
    [TestFixture]
    
public class PersonTest
    
{
        [Test]
        
public void PersonList ()
        
{
            
// try it 
            IList people = Mapper.Instance().QueryForList("SelectAll",null);

            
// test it 
            Assert.IsNotNull(people,"Person list not returned");
            Assert.IsTrue(people.Count
>0,"Person list is empty"); 
            Person person 
= (Person) people[0];
            Assert.IsNotNull(person,
"Person not returned");
        }

    }

}


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

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using IBatisNet.DataMapper;
using IBatisNet.Common.Utilities;


namespace TestPeople
{
    
/// <summary>
    
/// WebForm1 的摘要说明。
    
/// </summary>

    public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DataGrid dgList;
        
protected System.Web.UI.WebControls.Panel pnlList;

        
private void List_Load ()
        
{
            dgList.DataSource 
= Mapper.Instance().QueryForList("SelectAll",null);
            dgList.DataBind();
        }

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


        }


        
Web 窗体设计器生成的代码

    
    }

}




值对象Person.cs(这个类和数据库表是一致的,应该用代码生成器生成,网上有很多好的代码生成器,这里推荐一个CodeSmith)
using System;

namespace TestPeople
{
    
/// <summary>
    
/// Person 的摘要说明。
    
/// </summary>

    public class Person
    
{
        
private int _Id;
        
private string _FirstName;
        
private string _LastName;
        
private DateTime _BirthDate;
        
private double _WeightInKilograms;
        
private double _HeightInMeters;

        
public Person()    {}

        
public int Id
        
{
            
get    return _Id;}
            
set    {_Id = value;}
        }


        
public string FirstName
        
{
            
get{return _FirstName;}
            
set{_FirstName = value;}
        }


        
public string LastName
        
{
            
get{return _LastName;}
            
set{_LastName = value;}
        }


        
public DateTime BirthDate
        
{
            
get{return _BirthDate;}
            
set{_BirthDate = value;}
        }


        
public double WeightInKilograms
        
{
            
get{return _WeightInKilograms;}
            
set{_WeightInKilograms = value;}
        }


        
public double HeightInMeters
        
{
            
get{return _HeightInMeters;}
            
set{_HeightInMeters = value;}
        }

    }

}


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

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

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

数据库联接配置providers.config(都是官方标准的,这里就不贴了)
posted @ 2004-11-04 09:13  dannyr|一个都不能少!  阅读(4688)  评论(9编辑  收藏  举报