iBATIS.NET 学习笔记(四)

今天开始尝试着用iBATIS.NET写程序,创建WEB项目IbatisNET.Example,添加引用IBatisNet.DataMapper.dll,IBatisNet.DataAccess.dll,IBatisNet.Common.dll。
实体类Customers.cs
// ***********************************************************
// *公司:
// *作者:YK
// *模块:IbatisNet.Example.Model
// *功能:
// *创建日期:
// *修改日期:
// ***********************************************************
using  System;

namespace  IbatisNet.Example.Model
{
    
/// <summary>
    
/// Customers 的摘要说明。
    
/// </summary>

    [Serializable]
    
public class Customers
    
{
        
public Customers()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
private string _CustomerID = string.Empty;
        
private string _CompanyName = string.Empty;
        
private string _ContactName = string.Empty;
        
private string _ContactTitle = string.Empty;
        
private string _Address = string.Empty;
        
private string _City = string.Empty;
        
private string _Region  = string.Empty;
        
private string _PostalCode = string.Empty;
        
private string _Country = string.Empty;
        
private string _Phone = string.Empty;
        
private string _Fax = string.Empty;

        
public string CustomerID
        
{
            
get{return _CustomerID;}
            
set{_CustomerID = value;}
        }


        
public string CompanyName
        
{
            
get{return _CompanyName;}
            
set{_CompanyName= value;}
        }


        
public string ContactName
        
{
            
get{return _ContactName;}
            
set{_ContactName = value;}
        }


        
public string ContactTitle
        
{
            
get{return _ContactTitle;}
            
set{_ContactTitle = value;}
        }


        
public string Address
        
{
            
get{return _Address;}
            
set{_Address = value;}
        }

        
public string City
        
{
            
get{return _City;}
            
set{_City = value;}
        }


        
public string Region
        
{
            
get{return _Region;}
            
set{_Region = value;}
        }


        
public string PostalCode
        
{
            
get{return _PostalCode;}
            
set{_PostalCode = value;}
        }


        
public string Country
        
{
            
get{return _Country;}
            
set{_Country = value;}
        }

        
public string Phone
        
{
            
get{return _Phone;}
            
set{_Phone = value;}
        }

        
public string Fax
        
{
            
get{return _Fax;}
            
set{_Fax = value;}
        }


    }

}


对应实体类影射的Customers.XML文件
<? xml version="1.0" encoding="utf-8"  ?>  
< sqlMap  namespace ="Customers"  xmlns ="http://ibatis.apache.org/mapping"  
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" >
    
< alias >
        
< typeAlias  alias ="Customers"  type ="IbatisNet.Example.Model.Customers, IbatisNet.Example"   />
    
</ alias >
    
< resultMaps >                                     
        
< resultMap  id ="CustomersResult"  class ="Customers" >
            
< result  property ="CustomerID"  column ="CustomerID" />
            
< result  property ="CompanyName"  column ="CompanyName" />
            
< result  property ="ContactName"  column ="ContactName" />
            
< result  property ="ContactTitle"  column ="ContactTitle"   />
            
< result  property ="Address"  column ="Address"   />
            
< result  property ="City"  column ="City"   />
            
< result  property ="Region"  column ="Region"   />
            
< result  property ="PostalCode"  column ="PostalCode"   />
            
< result  property ="Country"  column ="Country"   />
            
< result  property ="Phone"  column ="Phone"   />
            
< result  property ="Fax"  column ="Fax"   />
        
</ resultMap >
    
</ resultMaps >
    
<!--  =============================================
        MAPPED STATEMENTS 
    ============================================= 
    
-->
    
< statements >
  
        
< select  id ="GetAllCustomers"  resultMap ="CustomersResult"  parameterClass ="Customers" >
            select
                CustomerID,
                CompanyName,
                ContactName,
                ContactTitle,
                Address,
                City,
                Region,
                PostalCode,
                Country,
                Phone,
                Fax 
            from Customers
        
</ select >
        
        
< insert  id ="InsertCustomer"  parameterClass ="Customers" >
          insert into Customers
             (CustomerID,
              CompanyName,
              ContactName,
              ContactTitle,
              Address,
              City,
              Region,
              PostalCode,
              Country,
              Phone,
              Fax)
          values
             (#CustomerID#,
              #CompanyName#,
              #ContactName#,
              #ContactTitle#,
              #Address#,
              #City#,
              #Region#,
              #PostalCode#,
              #Country#,
              #Phone#,
              #Fax#)
        
</ insert >
    
</ statements >
</ sqlMap >
配置SqlMap.config文件
<? xml version="1.0" encoding="utf-8" ?>
< sqlMapConfig  xmlns ="http://ibatis.apache.org/dataMapper"  
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" >
    
    
< settings >
        
< setting  useStatementNamespaces ="false" />
        
< setting  cacheModelsEnabled ="true" />
        
< setting  validateSqlMap ="false" ></ setting >

    
</ settings >

    
<!--  ==== SqlClient configuration =========     -->
    
<!--  Optional ( default )  -->
    
<!--  Rem : If used with a Dao it will be ignored  -->
    
< database >
        
< provider  name ="sqlServer1.1" />
        
< dataSource  name ="IbatisNet.Example"  connectionString ="data source=(local);database=Northwind;user id=sa;password=sa;" />
    
</ database >
    
    
< sqlMaps >
        
< sqlMap  resource ="Maps/Customers.xml" />
    
</ sqlMaps >
</ sqlMapConfig >

你可能感兴趣的:(ibatis)