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

  • 相关阅读:
    PPT2010中插入页码并且改变页码的位置
    Axis2发布webservice(2)--利用eclipse的axis2插件打包为arr发布
    Axis2发布webservice(1)--0配置发布
    权限设计
    网易2016实习生前端笔试题部分总结
    浅谈HTML文档模式
    从原型链看DOM--Node类型
    JavaScript之Function函数深入总结
    javascript之面向对象程序设计(对象和继承)
    javascript之基本包装类型(Boolean,Number,String)基础篇
  • 原文地址:https://www.cnblogs.com/niuniu1985/p/1641047.html
Copyright © 2011-2022 走看看