IBatis for .Net 测试

一.路径安排

 

文件安排在同一路径下,这里是personTest,也就是项目名

 

二.活动图

三.程序详解

导入包的时候需要注意版本要一致(DataMapperDataAccess

 

Form1 作为程序的入口点

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

 

namespace personTest

{

     /// <summary>

     /// Form1 的摘要说明。

     /// </summary>

     public class Form1 : System.Windows.Forms.Form

     {

         private System.Windows.Forms.Button button1;

         /// <summary>

         /// 必需的设计器变量。

         /// </summary>

         private System.ComponentModel.Container components = null;

 

         public Form1()

         {

              //

              // Windows 窗体设计器支持所必需的

              //

              InitializeComponent();

 

              //

              // TODO: InitializeComponent 调用后添加任何构造函数代码

              //

         }

 

         /// <summary>

         /// 清理所有正在使用的资源。

         /// </summary>

         protected override void Dispose( bool disposing )

         {

              if( disposing )

              {

                   if (components != null)

                   {

                       components.Dispose();

                   }

              }

              base.Dispose( disposing );

         }

 

         #region Windows 窗体设计器生成的代码

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器修改

         /// 此方法的内容。

         /// </summary>

         private void InitializeComponent()

         {

              this.button1 = new System.Windows.Forms.Button();

              this.SuspendLayout();

              //

              // button1

              //

              this.button1.Location = new System.Drawing.Point(104, 80);

              this.button1.Name = "button1";

              this.button1.Size = new System.Drawing.Size(72, 32);

              this.button1.TabIndex = 0;

              this.button1.Text = "button1";

              this.button1.Click += new System.EventHandler(this.button1_Click);

              //

              // Form1

              //

              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

              this.ClientSize = new System.Drawing.Size(292, 273);

              this.Controls.Add(this.button1);

              this.Name = "Form1";

              this.Text = "Form1";

              this.ResumeLayout(false);

 

         }

         #endregion

 

         /// <summary>

         /// 应用程序的主入口点。

         /// </summary>

         [STAThread]

         static void Main ()

         {

              Application.Run(new Form1());

         }

 

         private void button1_Click(object sender, System.EventArgs e)

         {

//Man 类似Formbean的程序用以保存信息

              Man kk = new Man();

              kk.id = 1;

//DateTime 构造函数的格式为(int ,int ,int ,int 小时, int ,int 秒,int 毫秒)

 

              kk.birthday = new DateTime(1983,1,1,1,1,1,1);

              kk.name = "lanhai";

              kk.fathername = "Yan";

              kk.weight = 2.00f ;

              kk.height = 2.00f ;

              try

              {

// 这里使用了默认的位置,并没有改写Mapper。使用Mapperstatic instance方法生成SqlMap对象,并调用SqlMapUpdate方法将Man的对象通过man.xml映射文件存储到数据库

// 可以写成如下格式

//SqlMap map = IBatisNet.DataMapper.Mapper.Instance();

//map.Update( Update ,kk);

                   IBatisNet.DataMapper.Mapper.Instance().Update("Update",kk);

              }

              catch(Exception exp)

              {thrownew Exception(exp.Message);}

         }

 

     }

}

 

 

SqlMap.config 配置文件

 

由于使用了默认的 Mapper ,它会在调用它的程序目录下查找这个文件,所以它与程序必须在同一目录下

 

<? xml version ="1.0" encoding ="utf-8" ?>

< sqlMapConfig xmlns : xsi ="http://www.w3.org/2001/XMLSchema-instance"

xsi : noNamespaceSchemaLocation ="SqlMapConfig.xsd">

< properties resource ="properties.config"/>  // 设置属性文件的位置可以在后面使用EL表达式应用

< settings >

     < setting useStatementNamespaces ="false"/>

     < setting cacheModelsEnabled ="true"/>  // 设置缓存机制

</ settings >

< database >

  < provider name ="sqlServer1.1"/>

  < dataSource name ="BOYYY" connectionString ="server=${datasource};database=${database};user id=${userid};password=${password};connection reset=false;connection lifetime=5; min pool size=1; max pool size=50"/>

// 此节点的说明见属性文件properties.configproviders.config

// 此节点的作用是配置数据库访问,SqlMap对象会根据它来连接数据库

</ database >

 

< sqlMaps >

 

// 指定数据映射文件

     < sqlMap resource ="man.xml"/>

</ sqlMaps >

 

</ sqlMapConfig >

 

 

Properties.config 属性文件

<? xml version ="1.0" encoding ="utf-8" ?>

< settings >

     < add key ="userid" value ="yyy" />    // 数据库访问用户名

     < add key ="password" value ="yyy" />  // 数据库访问用户密码

     < add key ="database" value ="Mans" /> // 数据库名称

     < add key ="datasource" value ="BOY" /> // 服务器名称

</ settings >

 

 

Providers.config 属性文件

<? xml version ="1.0" encoding ="utf-8" ?>

 

< providers >

     < clear />

     < provider

         name ="sqlServer1.0"  // 服务器类型名称

         description ="Microsoft SQL Server 7.0/2000, provider V1.0. 3300.0 in framework .NET V1.1"

         // 连接数据库出错时弹出提示框的描述

         enabled ="false"  // 默认为不允许访问这时如果使用SqlMap.config文件的Property节点下的name属性设置为这个sqlServer1.0既这个节点的name属性会导致读取Provider.config属性文件失败

         assemblyName ="System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b 77a 5c 561934e089"

 

 

// 以下是连接数据库用到的类库文件设置,SqlMap对象会调用他们

         connectionClass ="System.Data.SqlClient.SqlConnection"

         commandClass ="System.Data.SqlClient.SqlCommand"

         parameterClass ="System.Data.SqlClient.SqlParameter"

         parameterDbTypeClass ="System.Data.SqlDbType"

         parameterDbTypeProperty ="SqlDbType"

         dataAdapterClass ="System.Data.SqlClient.SqlDataAdapter"

         commandBuilderClass ="System.Data.SqlClient.SqlCommandBuilder"

         usePositionalParameters = "false"

         useParameterPrefixInSql = "true"

         useParameterPrefixInParameter = "true"    

         parameterPrefix ="@"

         />   

 

 

     < provider

         name ="sqlServer1.1"

         description ="Microsoft SQL Server 7.0/2000, provider V1.0. 5000.0 in framework .NET V1.1"

         default ="true"   // 这个属性设置此连接为默认值

         enabled ="true"

         assemblyName ="System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b 77a 5c 561934e089"

         connectionClass ="System.Data.SqlClient.SqlConnection"

         commandClass ="System.Data.SqlClient.SqlCommand"

          parameterClass ="System.Data.SqlClient.SqlParameter"

         parameterDbTypeClass ="System.Data.SqlDbType"

         parameterDbTypeProperty ="SqlDbType"

         dataAdapterClass ="System.Data.SqlClient.SqlDataAdapter"

         commandBuilderClass ="System.Data.SqlClient.SqlCommandBuilder"    

         usePositionalParameters = "false"    

         useParameterPrefixInSql = "true"

         useParameterPrefixInParameter = "true"                  

         parameterPrefix ="@"

     />       

</providers>

 

man.xml 映射文件

 

<? xml version ="1.0" encoding ="UTF-8" ?>

< sqlMap namespace ="personTest" xmlns : xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi : noNamespaceSchemaLocation ="SqlMap.xsd">

 

< statements >

 

 

// 会在SqlMap的数据库查询方法中调用这个节点对应的查询ID,此为Update

// parameterClass是指的储存信息的那个FormBean,注意一定要写全名,包括类库的命名空间

     < update id ="Update" parameterClass ="personTest.Man">

         update MAN set

                   name=#name#,

                   fathername=#fathername#,

                   weight=#weight#,

                   height=#height#,

                   birthday=#birthday#

       where id=#id#

     </ update >

    

</ statements >

 

</ sqlMap >

 

 

 

Man.cs 此为FormBean,用来存储数据。注意和映射文件中数据库查询语句的配置一定要一致,还有和数据库中的列名也要一致

 

using System;

 

namespace personTest

{

     public class Man

     {

         private int _ID;

         private string _Name;

         private string _FatherName;

         private DateTime _BirthDay;

         private float _Weight;

         private float _Height;

 

         public int id

         {

                   get{returnthis._ID;}

              set{this._ID = value;}

         }

         public string name

         {

              get{returnthis._Name;}

              set{this._Name = value;}

 

         }

         public string fathername

         {

              get{returnthis._FatherName;}

              set{this._FatherName = value;}

         }

         public DateTime birthday

         {

              get{returnthis._BirthDay;}

              set{this._BirthDay = value;}

         }

         public float weight

         {

              get{returnthis._Weight;}

              set{this._Weight = value;}

         }

         public float height

         {

              get{returnthis._Height;}

              set{this._Height = value;}

         }

 

    

     }

 

}

 

 

MAN(table)

Int  id | nvchar name | nvchar fathername |dateTime birthday |float height |float weight

 

你可能感兴趣的:(IBatis for .Net 测试)