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类文件

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 
    {
      
getreturn _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对象.

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语句声明间切换,而不需要改变我们的应用程序代码。

 

 

 

posted @ 2010-01-07 11:12  牛小花  阅读(311)  评论(0编辑  收藏  举报