IBatis for .Net 測試

          Posted on 2006-04-04 10:56 java++ 閱讀(1511) 評論(1)  編輯  收藏

          一.路徑安排

          ?

          文件安排在同一路徑下,這里是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=b77a5c561934e089"

          ?

          ?

          // 以下是連接數據庫用到的類庫文件設置,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=b77a5c561934e089"

          ???????? 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

          ?

          Feedback

          # re: IBatis for .Net 測試[未登錄]  回復  更多評論   

          2007-06-20 19:47 by king
          太簡單了

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 祁阳县| 工布江达县| 东辽县| 西乌珠穆沁旗| 会东县| 安泽县| 金湖县| 紫金县| 金川县| 松桃| 宿松县| 岑巩县| 澄迈县| 十堰市| 汝城县| 上杭县| 胶州市| 宁武县| 兰坪| 广东省| 谷城县| 抚松县| 大关县| 永泰县| 偏关县| 乡城县| 饶河县| 甘谷县| 安远县| 山丹县| 淳安县| 龙里县| 澎湖县| 潜江市| 会理县| 隆德县| 米泉市| 吉木乃县| 阿巴嘎旗| 南昌市| 乌拉特后旗|