IBatis的簡單一實例

來源:互聯網
上載者:User

最近在研究IBatis, 自己做了個簡單的例子,清晰簡潔,具體如下:

環境:vs2008+Sql Server2005


1.結構

 一個WebSite(IbatisSample);一個Model,用來放實體類;一個Service,用來放方法。


2.WebSite中內容介紹

Map檔案


sql語句寫在這裡

SqlMap.Config檔案


代碼

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

<!-- 外部設定檔
<properties resource="properties.config" />
-->

<settings>
<setting useStatementNamespaces="false" />
</settings>

<providers resource="providers.config"/>

<!-- 資料庫連接資訊 -->
<database>
<provider name="sqlServer2.0" />
<dataSource name="Person" connectionString="server=(local);database=Northwind;Integrated Security=SSPI;Persist Security Info=False;" />
</database>

<!-- SqlMap.xml 資訊 -->
<sqlMaps>
<sqlMap resource="Maps/Person.xml" />
</sqlMaps>

</sqlMapConfig>

 

 

這裡定義了資料庫連接,map檔案等。

資料庫是SqlServer,其中的connectionString使用的是Windows整合驗證。

SqlMaps裡面就是定義的map檔案


3.Model內容介紹

存放實體類。


4.Service內容介紹


要先引用兩個組件,PersonBiz中就是對應實體類Person的方法。

public static Person SelectPersonById(string id)
{
return Mapper.Instance().QueryForObject<Person>("SelectPersonById", id);
}

其中雙引號中的SelectPersonById就是在之前的Person.xml map檔案中所寫的sql語句名稱。


5.Person表建表語句

 用的是Northwind資料庫,O如果沒有這個資料庫,自己建也行,只是要在SqlMap.Config中將database後面換成自己的資料庫名稱。


代碼

USE NORTHWIND
CREATE TABLE PERSON(
ID VARCHAR (20) NOT NULL,
FIRST_NAME VARCHAR (40) ,
LAST_NAME VARCHAR (40) ,
BIRTH_DATE DATETIME ,
WEIGHT DECIMAL(4, 2) ,
HEIGHT DECIMAL(4, 2) ,
PRIMARY KEY (ID)
)

 

 

 

這樣就可以啦,很容易。

程式碼封裝暫存於此:http://www.brsbox.com/filebox/down/fc/1905a7f2284350713b079c8ac3c9d18d

另外哪位能告訴我怎麼上傳程式碼封裝,謝謝。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.