zoukankan      html  css  js  c++  java
  • 最简单的IBatis for .net实现

    看过IBatis在Java下简单的实现所以写个.net下IBatis最简单的实现,下面我来介绍下我的做法。
           需要引入Castle.DynamicProxy.dll、IBatisNet.Common.dll、IBatisNet.DataAccess.dll和IBatisNet.DataMapper.dll四个DLL。
           首先是需要一个测试的用的数据库(Test)下的一张表(Table1)有两个字段:col1,col2,还有一个和数据库对应的类Table1,代码如下:
         public class Table1
        {
            private string _col1;
            private string _col2;
            public string col2
            {
               get { return _col2; }
                set { _col2 = value; }
            }
            public string col1
            {
                get { return _col1; }
                set { _col1 = value; }
            }  
    }
    然后,需要添加三个配置文件:provider.config,dao.config,SqlMap.config,其中provider.config可以直接添加IBatis提供的文件,dao.config主要配置数据库连接和接口类,SqlMap存储具体的XML路径。将IBatis提供的三个XML模板复制到Microsoft Visual Studio 8\Xml\Schemas下面。
    dao.config:
    <?xmlversion="1.0"encoding="utf-8"?>
    <daoConfig     xmlns="http://ibatis.apache.org/dataAccess"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <providers resource="providers.config"/>
       <contextid="SqlMapDao"default="true">
           <!-- Database connection information -->
           <database>
               <providername="sqlServer2.0"/>
               <dataSource name="Test"connectionString="server='localhost';uid=**;password=****;database='Test';Min Pool Size=7;Max Pool Size=50"/>
           </database>
           <daoSessionHandlerid="SqlMap">
               <propertyname="sqlMapConfigFile"value="SqlMap.config"/>
           </daoSessionHandler>       
           <daoFactory>
               <!—因为是简单实现所以不演示接口实现------>
           </daoFactory>
     
       </context>
    </daoConfig>
     
    SqlMap.config:
    <?xmlversion="1.0"encoding="utf-8"?>
    <sqlMapConfig   xmlns="http://ibatis.apache.org/dataMapper"   http://www.w3.org/2001/XMLSchema-instance">
     <settings>
        <settinguseStatementNamespaces="false"/>
     </settings>
       <sqlMaps>
        <sqlMapresource ="Maps/Table1.xml"/>
       </sqlMaps>
    </sqlMapConfig>
    sqlMap.config中有一个Table1.xml,我就是通过它来存储具体的SQL语句的.
    Table1.XML:
    <?xmlversion="1.0"encoding="utf-8" ?>
    <sqlMapxmlns="http://ibatis.apache.org/mapping"namespace ="Table1">
     <alias>
        <typeAliasalias ="Table1"type ="MyIBatis.Table1"/>
     </alias>
     
     <statements>
        <insertid ="InsertTable1"parameterClass ="Table1">
          insert into Table1
          (col1,col2)
          values(#col1#,#col2#)
        </insert>
     </statements>
    </sqlMap>
     
    最后通过我们来实现在一个类里面实现在Table1.XML里的InsertTable1的方法,具体如下:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using IBatisNet.DataAccess.Configuration;
    using IBatisNet.DataMapper;
    using IBatisNet.DataAccess;
    using IBatisNet.DataAccess.DaoSessionHandlers;
     
    namespace MyIBatis
    {
        public class Test
        {
            public void TestInert()
            {
                DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
                builder.Configure();//用来注册dao.config
                Table1 t1= new Table1();
                t1.col1="col";
                t1.col2="col2";
     
                IDaoManager daoManager = DaoManager.GetInstance();
                daoManager.OpenConnection();
                SqlMapDaoSession sqlMapDaoSession = (SqlMapDaoSession)daoManager.LocalDaoSession;
                ISqlMapper sqlMapper = sqlMapDaoSession.SqlMap;
                sqlMapper.Insert("InsertTable1",t1);
            }
        }
    }
     
    这样就实现了一个简单的IBatis应用,怎么样是不是很简单?这个东西其实一点都不简单,很适用哦。

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kolacowboy/archive/2007/09/01/1768593.aspx

  • 相关阅读:
    Dapr微服务应用开发系列1:环境配置
    酷炫的终端模拟器eDEX-UI
    man 命令显示简体中文帮助
    通过二维码传输文件到linux
    awk命令使用实例
    Centos安装vncserver虚拟网络控制台
    使用SSHFS文件系统远程挂载目录
    搜狗服务器引擎Sogou C++ Workflow开源啦!
    网站服务器系统,选windows还是linux?
    TikTok总部将留在美国 字节跳动保有控制权和核心算法
  • 原文地址:https://www.cnblogs.com/guyuehuanhuan/p/1901737.html
Copyright © 2011-2022 走看看