學習MyBatis.Net 之路(一)

MyBatis.NET的前身爲IBatis,是JAVA版MyBatis在.NET平臺上的翻版,相對NHibernate、EntityFramework等重量級ORM框架而言,MyBatis.NET必須由開發人員手動寫SQL,相對靈活性更大,更容易保證DB訪問的性能,適用開發團隊裏有SQL熟手的場景。

下面是使用步驟:
1.到官網http://code.google.com/p/mybatisnet/ 下載相關dll和文檔
這裏寫圖片描述

2.創建一個Web程序
創建一個MVC4的Web應用程序,並引用我們下載的IBatisNet程序包中的IBatisNet.DataMapper.dllIBatisNet.Common.dll,這裏我只引用了這兩個,log4net我沒有引用也沒有配置使用,全部程序結構如下
這裏寫圖片描述

3.添加Providers.config,並複製到項目根目錄下
把下載的IBatisNet程序包打開,就能找到providers.config文件,裏面定義了MyBatis.Net支持的各種數據庫驅動,這裏我以sqlServer爲例,把其他不用的全部刪除,只留下sqlServer的並且把版本號全部改爲4.0,同時把enabled屬性設置成true,如下:

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

  <clear/>
  <!--設置enabled=true-->
  <provider
     name="sqlServer4.0"
     enabled="true"
     description="Microsoft SQL Server, provider V4.0.0.0 in framework .NET V4.0"
     assemblyName="System.Data, Version=4.0.0.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="@"
     allowMARS="false"
    />
</providers>

把這個文件複製到Web項目根目錄下

4.在項目目錄下添加SqlMap.config,它的作用主要是指定db連接串,告訴系統providers.config在哪? 以及db與entity的映射文件在哪?如下:

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

  <settings>
    <setting useStatementNamespaces="false"/>
    <!--true表示statementName要使用Namespace-->
    <setting cacheModelsEnabled="true"/>
  </settings>

  <providers resource="providers.config"/>
  <!--指定providers.config文件的路徑-->

  <!-- Database connection information -->
  <database>
    <provider name="sqlServer4.0"/>
    <!--設置數據庫連接字符串-->
    <dataSource name="DB" connectionString="Data Source=.;Initial Catalog=School;uid=sa;pwd=123456"/>
  </database>

  <!--db與Entity的映射文件-->
  <sqlMaps>
    <sqlMap resource="Maps/PersonAndCourse.xml"/>
    <sqlMap resource="Maps/PersonMap.xml"/>
  </sqlMaps>
</sqlMapConfig>

5.這裏在數據庫建兩張表一個是個人信息還有一個是課程表,如下:
這裏寫圖片描述
第一張表是Person表,第二張表爲Cour課程表,課程表名字是錯的只敲一半,大家略過,第一張表的ID和第二張表的PerID關聯,建兩張表是爲了後面的聯合查詢,這裏先不多說

下面爲創建表的SQL語句


create database School
go 
use School
go 
create table Person
(
ID int not null primary key Identity(1,1),
Name nvarchar(50),
Age int ,
Sex nvarchar(50)
)
go
create table Course
(
ID int not null primary key Identity(1,1),
PerID int not null ,
CourseName nvarchar(50)
)

go 
insert into Person values
('sasa',15,'女'),
('ff',20,'男')
go
insert into Course values
(1,'計算機'),
(1,'標準'),
(1,'物理')
go
--給兩個表添加主外鍵關係
alter table Course add
Constraint FK_Curse_Per foreign key (PerId) references Person(ID)
go

select * from Person
select * from Course

6.接下來在Models文件夾裏建兩個實體類。一個爲Person類,另一個爲PersonAndCourse類. 後者只比前者多了一個課程名稱的字段.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PratiseMyBatis.Models
{
    public class Person
    {

        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Sex { get; set; }
    }

  public class PersonAndCourse
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Sex { get; set; }
        public string CourseName { get; set; }
    }
}

博主也是纔開始學這個東東的,如果大家有疑問的地方歡迎來交流。這個Demo裏沒有用到log4net日誌。 所以我也沒配置。主要講的是MyBatis的功能。 如果大家希望學log4net配置這方面,我後面會講到的.
好了,今天先到此爲止了。 接下來看第二章

發佈了44 篇原創文章 · 獲贊 21 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章