ibatisnet框架使用说明

ibatis配置文件主要包括三个 sqlmap.config,providers.config,database.config,注意所有文件生成操作都为嵌入的资源。其中database.config主要是配置数据库参数的一个config文件

"1.0" encoding="utf-8" ?>

    
    
    "userid" value="sa" /> 
    "password" value="sa" />
    "database" value="person" />
    "datasource" value="." />
    "selectKey" value="select @@IDENTITY as value" />
    "directory" value="Maps" />
    "useStatementNamespaces" value="false" />

其次 providers.config 这个主要存放连接数据库的驱动程序 主要有 oracle sqlserver等等。
最后讲解 sqlmap.config这个配置比较重要。

"1.0" encoding="utf-8"?>
"http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

  
  
  "database.config, IBatisNetDemo"/>
  
    "${useStatementNamespaces}"/>
    "true"/>
    "false"/>
  
 
  
  "providers.config,IBatisNetDemo"/>
    
  
  
  
    
    
    "sqlServer1.1"/>
    "test" connectionString="data source=${datasource};database=${database};user id=${userid};password=${password};connection reset=false;connection lifetime=5; min pool size=1; max pool size=50"/>
  

  
    
    
    "Map.SqlClient.Dep.xml,IBatisNetDemo"/>
    "Map.SqlClient.Person.xml,IBatisNetDemo"/>
  

其次讲解xml文件 这里主要讲解两个xml文件,第一个Person.xml

"1.0" encoding="utf-8" ?>

namespace="Person" xmlns="http://ibatis.apache.org/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

  
    "Person" type="IBatisNetDemo.Domain.Person,IBatisNetDemo" /> 
  

    
    "SelectAllResult" class="Person">
      "Id" column="PER_ID" />
      "FirstName" column="PER_FIRST_NAME" />
      "LastName" column="PER_LAST_NAME" />
      "BirthDate" column="PER_BIRTH_DATE" />
      "WeightInKilograms" column="PER_WEIGHT_KG" />
      "HeightInMeters" column="PER_HEIGHT_M" />
      "depid" column="DepID" />
    

  

  

    <select id="SelectPersonByDepId" resultMap="SelectAllResult">
      select
      PER_ID,
      PER_FIRST_NAME,
      PER_LAST_NAME,
      PER_BIRTH_DATE,
      PER_WEIGHT_KG,
      PER_HEIGHT_M,
      DepID
      from PERSON where DepID=#DepID#
    select>
    <select id="SelectAllPerson" resultMap="SelectAllResult">
      select
      PER_ID,
      PER_FIRST_NAME,
      PER_LAST_NAME,
      PER_BIRTH_DATE,
      PER_WEIGHT_KG,
      PER_HEIGHT_M,
      DepID
      from PERSON
    select>

    <select id="SelectByPersonId" resultMap="SelectAllResult" parameterClass="Hashtable">
      select
      PER_ID,
      PER_FIRST_NAME,
      PER_LAST_NAME,
      PER_BIRTH_DATE,
      PER_WEIGHT_KG,
      PER_HEIGHT_M,
      DepID
      from PERSON
      "where">
        
          "and" property="id" >
            PER_ID = '$id$'
          
          "and" property="name" >
            PER_FIRST_NAME LIKE '%$name$%'
          
        
      
    select>

    "InsertPerson"  parameterclass="Person" >
      "Id" type="post" resultClass="int">
        ${selectKey}
      
      insert into Person
      ( PER_FIRST_NAME,
      PER_LAST_NAME,
      PER_BIRTH_DATE,
      PER_WEIGHT_KG,
      PER_HEIGHT_M)
      values
      (#FirstName#,#LastName#,#BirthDate#, #WeightInKilograms#, #HeightInMeters#)
    

    "UpdatePerson"
                   parameterclass="Person">
      set
      PER_FIRST_NAME =#FirstName#,
      PER_LAST_NAME =#LastName#,
      PER_BIRTH_DATE =#BirthDate#,
      PER_WEIGHT_KG=#WeightInKilograms#,
      PER_HEIGHT_M=#HeightInMeters#
      where
      PER_ID = #Id# ]]>
    

    "DeletePerson" parameterclass="Person">
      delete from Person
      where
      PER_ID = #Id#
    

  

第二个xml文件 Dep.xml

"1.0" encoding="utf-8" ?>

namespace="Dep" xmlns="http://ibatis.apache.org/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

  
    "Dep" type="IBatisNetDemo.Domain.Dep,IBatisNetDemo" />  


  

  
    "SelectAllResultDep" class="Dep">
      "DepId" column="DepID" />
      "DepName" column="DepName" />
      "list" column="DepID" select="SelectPersonByDepId" />
    
  

  
    <select id="SelectDepById" resultMap="SelectAllResultDep">
      select DepID,DepName from Dep where DepId=#DepID#
    select>
  

其中一个为部门,一个为人员,两个之间关系为一对多的关系

配置文件讲解完过后我们来讲解怎么调用这个框架,首先要引用IBatisNet.Common.dll,IBatisNet.DataMapper.dll 两个文件

其实加载sqlmap对象

代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using IBatisNetDemo.Domain;

namespace IBatisNetDemo
{
    public class BaseDao where T : class
    {
        private ISqlMapper sqlMap;

        //private string fileName = "sqlMap.Config";

        public BaseDao()
        {
            Assembly assembly = Assembly.Load("IBatisNetDemo");
            Stream stream = assembly.GetManifestResourceStream("IBatisNetDemo.sqlmap.config");

            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            sqlMap = builder.Configure(stream);

        }


        public ISqlMapper SqlMap
        {
            get
            {
                return sqlMap;
            }
        }

        public IList GetAllList(string key)
        {
            
            return SqlMap.QueryForList(key, null);
            
        }
        public T GetModel(string key, object id)
        {

            return SqlMap.QueryForObject(key, id);

        }

        public object Insert(string key, T model)
        {
            object o = null;
            o = sqlMap.Insert(key, model);
            return o;

        }
    }
}
源码数据库下载地址:下载

你可能感兴趣的:(ibatisnet框架使用说明)