MyBatis.NET的前身为IBatis,是JAVA版MyBatis在.NET平台上的翻版,相对NHibernate、EntityFramework等重量级ORM框架而言,MyBatis.NET必须由开发人员手动写SQL,相对灵活性更大,更容易保证DB访问的性能,适用开发团队里有SQL熟手的场景。
下面是使用步骤:
1、到官网http://code.google.com/p/mybatisnet/ 下载相关dll和文档
Doc-DataAccess-1.9.2.zip
Doc-DataMapper-1.6.2.zip
IBatis.DataAccess.1.9.2.bin.zip
IBatis.DataMapper.1.6.2.bin.zip
一共有4个zip包
2、创建一个Web应用,参考下图添加程序集引用
3、修改web.config,主要是配置log4net,参考下面的内容:
1 <?xml version="1.0"?>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
View Code
4、添加Providers.config
把从官方下载的压缩包解开,就能找到providers.config文件,里面定义了MyBatis.Net支持的各种数据库驱动,本例以oracle为例,把其它不用的db provider全删掉,只保留下oracleClient1.0,同时把enabled属性设置成true,参考下面这样:
1 <?xml version="1.0"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4
5
6
7
8
9 name="oracleClient1.0"
10 description="Oracle, Microsoft provider V1.0.5000.0"
11 enabled="true"
12 assemblyName="System.Data.OracleClient, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" connectionClass="System.Data.OracleClient.OracleConnection"
13 commandClass="System.Data.OracleClient.OracleCommand"
14 parameterClass="System.Data.OracleClient.OracleParameter"
15 parameterDbTypeClass="System.Data.OracleClient.OracleType"
16 parameterDbTypeProperty="OracleType"
17 dataAdapterClass="System.Data.OracleClient.OracleDataAdapter"
18 commandBuilderClass="System.Data.OracleClient.OracleCommandBuilder"
19 usePositionalParameters="false"
20 useParameterPrefixInSql="true"
21 useParameterPrefixInParameter="false"
22 parameterPrefix=":"
23 allowMARS="false"
24 />
25
26
View Code
把这个文件复制到Web项目根目录下
5、添加SqlMap.config,内容如下:
1 <?xml version="1.0" encoding="utf-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
View Code
这个文件也复制到Web项目根目录下,它的作用主要是指定db连接串,告诉系统providers.config在哪? 以及db与entity的映射文件在哪?(映射文件后面会讲到,这里先不管)
6、在Oraccle中先建表Product以及Sequence,方便接下来测试
1 -- CREATE TABLE
2 CREATE TABLE PRODUCT
3 (
4 PRODUCTID NUMBER NOT NULL,
5 PRODUCTNAME VARCHAR2(100),
6 PRODUCTCOMPANY VARCHAR2(100),
7 SIGNDATE DATE,
8 UPDATEDATE DATE
9 );
10 -- CREATE/RECREATE PRIMARY, UNIQUE AND FOREIGN KEY CONSTRAINTS
11 ALTER TABLE PRODUCT
12 ADD CONSTRAINT PK_PRODUCT_ID PRIMARY KEY (PRODUCTID);
13
14 -- CREATE SEQUENCE
15 CREATE SEQUENCE SQ_PRODUCT
16 MINVALUE 1
17 MAXVALUE 9999999999999999999999999
18 START WITH 1
19 INCREMENT BY 1
20 CACHE 20;
View Code
7、创建Maps目录,并在该目录下,添加映射文件ProductMap.xml,内容如下:
1 <?xml version="1.0" encoding="utf-8" ?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 select SQ_Product.nextval as ProductId from dual
38
39
40 VALUES(#ProductId#,#ProductCompany#, #ProductName# , #SignDate# , #UpdateDate#)]]>
41
42
43
44
45
46 ProductCompany=#ProductCompany#,
47 SignDate=#SignDate#,
48 UpdateDate=#UpdateDate#
49 Where ProductId=#ProductId#]]>
50
51
52
53
54
55
56
57
58
59
60
View Code
它的作用就是指定各种sql,以及db表与entity的映射规则,注意下insert中Sequence的用法!
8、创建实体类Product
1 using System;
2
3 namespace Web
4 {
5 public class Product
6 {
7 public int ProductId { get; set; }
8 public string ProductName { get; set; }
9 public string ProductCompany { get; set; }
10 public DateTime SignDate { get; set; }
11 public DateTime UpdateDate { get; set; }
12
13 public Product() { }
14 }
15 }
View Code
9、写一个通用的BaseDA类,对MyBatis.Net做些基本的封装
1 using IBatisNet.DataMapper;
2 using System.Collections.Generic;
3
4 namespace Web
5 {
6 public static class BaseDA
7 {
8 public static int Insert(string statementName, T t)
9 {
10 ISqlMapper iSqlMapper = Mapper.Instance();
11 if (iSqlMapper != null)
12 {
13 return (int)iSqlMapper.Insert(statementName, t);
14 }
15 return 0;
16 }
17
18 public static int Update(string statementName, T t)
19 {
20 ISqlMapper iSqlMapper = Mapper.Instance();
21 if (iSqlMapper != null)
22 {
23 return iSqlMapper.Update(statementName, t);
24 }
25 return 0;
26 }
27
28 public static int Delete(string statementName, int primaryKeyId)
29 {
30 ISqlMapper iSqlMapper = Mapper.Instance();
31 if (iSqlMapper != null)
32 {
33 return iSqlMapper.Delete(statementName, primaryKeyId);
34 }
35 return 0;
36 }
37
38 public static T Get(string statementName, int primaryKeyId) where T : class
39 {
40 ISqlMapper iSqlMapper = Mapper.Instance();
41 if (iSqlMapper != null)
42 {
43 return iSqlMapper.QueryForObject(statementName, primaryKeyId);
44 }
45 return null;
46 }
47
48 public static IList QueryForList(string statementName, object parameterObject = null)
49 {
50 ISqlMapper iSqlMapper = Mapper.Instance();
51 if (iSqlMapper != null)
52 {
53 return iSqlMapper.QueryForList(statementName, parameterObject);
54 }
55 return null;
56 }
57 }
58 }
View Code
10、然后就可以在Default.aspx.cs上测试了,参考下面的代码:
using System;
using System.Web.UI;
namespace Web
{
public partial class Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
//插入
var insertProductId = BaseDA.Insert("InsertProduct", new Product()
{
ProductCompany = "INFOSKY",
ProductName = "iGSA2",
SignDate = DateTime.Now,
UpdateDate = DateTime.Now
});
//查单条记录
var model = BaseDA.Get("SelectByProductId", insertProductId);
ShowProduct(model);
Response.Write("
");
//修改记录
if (model != null)
{
model.ProductName = (new Random().Next(0, 99999999)).ToString().PadLeft(10, '0');
int updateResult = BaseDA.Update("UpdateProduct", model);
Response.Write("update影响行数:" + updateResult + "
");
}
//查列表
var products = BaseDA.QueryForList("SelectAllProduct");
foreach (var pro in products)
{
ShowProduct(pro);
}
Response.Write("
");
//删除记录
int deleteResult = BaseDA.Delete("DeleteProductById", insertProductId);
Response.Write("delete影响行数:" + deleteResult + "
");
}
void ShowProduct(Product pro)
{
if (pro == null) return;
Response.Write(string.Format("{0} , {1} , {2} , {3} , {4}
",
pro.ProductId, pro.ProductName, pro.ProductCompany, pro.SignDate, pro.UpdateDate));
}
}
}
整个项目的目录结构如下: