iBatisNet学习笔记三:QuickStart

参考:http://www.cnblogs.com/maplye/archive/2006/03/25/358586.html

http://opensource.atlassian.com/confluence/oss/display/IBATIS/Quick+Start+Guide

 

以一个小Demo为例,学习iBatisNet框架应用的大致步骤。

1. 建立一张数据表,如:Person.sql

2. 建立一个Web Application Project,如:iBatisTutorial 

3. 在项目目录下建立lib目录,复制IBatisNet必需的dll,如下:
IBatisNet.Common.dll
IBatisNet.DataMapper.dll
IBatisNet.DataAccess.dll
log4net.dll
Castle.DynamicProxy.dll
并在项目中添加这些dll的引用

4. 创建模型对象

在项目中创建目录Model,并在该目录下创建Person类文件

ExpandedBlockStart.gif Person.cs
using  System;
namespace  iBatisTutorial.Model
{
  
public   class  Person
  {
    
private   int  _id;
    
private   string  _firstName;
    
private   string  _lastName;
    
private  DateTime _birthDate;
    
private   double  _weightInKilograms;
    
private   double  _heightInMeters;

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

  
//  Other public properties for the private fields ...

  }
}

 这个类就是对Person的一个描述,只包含一些属性,这就是这个系统的数据的载体

5. 定义实体定义的XML

在项目目录下建Maps目录,在该目录下建立Person.xml,详见学习笔记二。

6. 定义数据连接

在项目根目录下定义sqlmap.config,copy providers.config文件到根目录,详见学习笔记一。

7. 定义Mapper(这个类是什么呢?为何在Demo中没有找到?)

在根目录下创建Mapper类,该类是得到单一的SqlMapper对象.

ExpandedBlockStart.gif Mapper.cs
using  IBatisNet.Common.Utilities;
using  IBatisNet.DataMapper;

namespace  IBatisNet.DataMapper
{
 
public   class  Mapper
 {  
  
private   static   volatile  SqlMapper _mapper  =   null ;

  
protected   static   void  Configure ( object  obj)
  {
   _mapper 
=  (SqlMapper) obj;
  }
  
protected   static   void  InitMapper()
  {   
   ConfigureHandler handler 
=   new  ConfigureHandler (Configure);
   _mapper 
=  SqlMapper.ConfigureAndWatch (handler);
  }

  
public   static  SqlMapper Instance()
  {
   
if  (_mapper  ==   null )
   {
    
lock  ( typeof  (SqlMapper))
    {
     
if  (_mapper  ==   null //  double-check
      InitMapper();
    }
   }
   
return  _mapper;
  }

  
public   static  SqlMapper Get()
  {
   
return  Instance();
  }
 }
}

可以根据需要使用很多不同的数据库,只需建立另外的Mapper类,每个Mapper配置是一个数据库表现。对于我们的应用程序来说,Mapper就是数据库。我们可以改变数据库以及在存储过程和SQL语句声明间切换,而不需要改变我们的应用程序代码。

 

 

 

转载于:https://www.cnblogs.com/niuniu1985/archive/2010/01/07/1641047.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值