一个简单的ibatis.net架构(包含项目模板代码下载)

 

1        基本介绍

iBATISNet Database Layer!这个框架将让你能够更好的在dotnet应用中设计和实现实体层。这个框架有两个主要的组成部分,一个是SQL Maps,另一个是Data Access Objects。下面是一个简单的iBATISNet应用架构。

2        架构预览

 

项目中总共10个项目

  External_bin 需要引用的dll

  MyCompany.Controls 项目中用到的自定义控件

  MyCompany.IbatisNet项目中IbatisNet组件

  MyCompany.MyProject.Domain 项目对象

  MyCompany.MyProject.Persistence 数据库持久化对象

  MyCompany.MyProject.Run 项目运行对象

  MyCompany.MyProject.Service 业务服务对象

  MyCompany.MyProject.UserControls 界面用户控件对象

  MyCompany.MyProject.WinFrom 界面对象

  MyCompany.Utility 项目中用到的工具集

 

3        实体对象层

MyCompany.MyProject.Domain

Entity1.cs源代码 

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  1 /* =====================================================
  2     作    者 : 陈励
  3     创建时间 : 2008-11-16 22:10:01
  4     修 改 者 : 
  5     修改时间 : 
  6     描    述 : Entity_1
  7 ===================================================== */
  8 
  9 using System;
 10 using System.Collections.Generic;
 11 using System.Text;
 12 
 13 namespace MyCompany.MyProject.Domain
 14 {
 15     /// <summary>
 16     /// 针对 Entity_1 为IBatisNet创建的类型.
 17     /// </summary>
 18     [Serializable]
 19     public sealed class Entity1
 20     {
 21         #region 私有变量
 22         private bool _isChanged;
 23         private bool _isDeleted;
 24         private string _attribute1old; // Attribute_1
 25         private string _attribute1; // Attribute_1
 26         private string _attribute2; // Attribute_2
 27         private string _attribute3; // Attribute_3
 28         private IList<Entity2> _entity2; // Entity_1
 29         #endregion
 30 
 31         #region 构造函数
 32         /// <summary>
 33         /// 构造函数
 34         /// </summary>
 35         public Entity1()
 36         {
 37             
 38         }
 39 
 40         /// <summary>
 41         /// 构造函数
 42         /// </summary>
 43         /// <param name="attribute1">Attribute_1</param>
 44         /// <param name="attribute2">Attribute_2</param>
 45         /// <param name="attribute3">Attribute_3</param>
 46         public Entity1(string attribute1, string attribute2, string attribute3)
 47         {
 48             this.Attribute1 = attribute1;
 49             this.Attribute2 = attribute2;
 50             this.Attribute3 = attribute3;
 51         }
 52 
 53         #endregion
 54 
 55         #region 公共属性
 56 
 57         /// <summary>
 58         /// Attribute_1
 59         /// </summary> 
 60         public string Attribute1Old
 61         {
 62             get
 63             {
 64                     return _attribute1old;
 65             }
 66             set
 67             {
 68                 _attribute1old = value;
 69             }
 70         }
 71 
 72         /// <summary>
 73         /// Attribute_1
 74         /// </summary> 
 75         public string Attribute1
 76         {
 77             get
 78             {
 79                 return _attribute1;
 80             }
 81             set
 82             {
 83                 if (value != null && Encoding.Default.GetByteCount(value.ToString()) > 50)
 84                     throw new ArgumentOutOfRangeException("Attribute1", value.ToString(), "Attribute_1_长度超出限制(50)!");
 85 
 86                 _isChanged |= (_attribute1 != value); 
 87                 _attribute1 = value;
 88                 if (_attribute1old == null)
 89                     _attribute1old = _attribute1;
 90             }
 91         }
 92 
 93         /// <summary>
 94         /// Attribute_2
 95         /// </summary> 
 96         public string Attribute2
 97         {
 98             get
 99             {
100                 return _attribute2;
101             }
102             set
103             {
104                 if (value != null && Encoding.Default.GetByteCount(value.ToString()) > 50)
105                     throw new ArgumentOutOfRangeException("Attribute2", value.ToString(), "Attribute_2_长度超出限制(50)!");
106 
107                 _isChanged |= (_attribute2 != value); 
108                 _attribute2 = value;
109             }
110         }
111 
112         /// <summary>
113         /// Attribute_3
114         /// </summary> 
115         public string Attribute3
116         {
117             get
118             {
119                 return _attribute3;
120             }
121             set
122             {
123                 if (value != null && Encoding.Default.GetByteCount(value.ToString()) > 50)
124                     throw new ArgumentOutOfRangeException("Attribute3", value.ToString(), "Attribute_3_长度超出限制(50)!");
125 
126                 _isChanged |= (_attribute3 != value); 
127                 _attribute3 = value;
128             }
129         }
130 
131         /// <summary>
132         /// Entity_1
133         /// </summary> 
134         public IList<Entity2> Entity2
135         {
136             get
137             {
138                 return _entity2;
139             }
140             set
141             {
142                 if (value != null)
143                 {
144                     foreach (Entity2 prt in value)
145                     {
146                         prt.Entity1 = this;
147                     }
148                 }
149                 _isChanged |= (_entity2 != value); _entity2 = value;
150             }
151         }
152 
153         /// <summary>
154         /// 表示实例是否已经发生改变.
155         /// </summary>
156         public bool IsChanged
157         {
158             get { return _isChanged; }
159         }
160 
161         /// <summary>
162         /// 表示实例是否已经被删除.
163         /// </summary>
164         public bool IsDeleted
165         {
166             get { return _isDeleted; }
167         }
168 
169         #endregion
170 
171         #region 公共方法
172 
173         /// <summary>
174         /// 调用方法,标识实例已经被删除.
175         /// </summary>
176         public void MarkAsDeleted()
177         {
178             _isDeleted = true;
179             _isChanged = true;
180         }
181         /// <summary>
182         /// 清空状态,设置为未改动.
183         /// </summary>
184         public void ClearMarks()
185         {
186             _isDeleted = false;
187             _isChanged = false;
188         }
189 
190         /// <summary>
191         /// 设置已改动.
192         /// </summary>
193         public void Changed()
194         {
195             _isChanged = true;
196         }
197 
198         #endregion
199 
200     }
201 }
202 

 

4        数据层

  MyCompany.MyProject.Persistence 

Interfaces 数据层接口

IEntity1Dao.cs 源代码

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> 1 /* =====================================================
 2     作    者 : 陈励
 3     创建时间 : 2008-11-16 22:10:01
 4     修 改 者 : 
 5     修改时间 : 
 6     描    述 : Entity_1
 7 ===================================================== */
 8 
 9 using System;
10 using System.Collections.Generic;
11 using System.Text;
12 
13 using MyCompany.MyProject.Domain;
14 
15 namespace MyCompany.MyProject.Persistence.Interfaces
16 {
17     /// <summary>
18     /// Entity1: Entity_1的数据库操作接口.
19     /// </summary>
20     public interface IEntity1Dao
21     {
22         /// <summary>
23         /// 获取Entity_1列表(不包含父对象,仅返回对象本身)
24         /// </summary>
25         /// <param name="entity1">Entity_1</param>
26         /// <returns>Entity_1集合</returns>
27         IList<Entity1> GetBaseEntity1(Entity1 entity1);
28 
29         /// <summary>
30         /// 获取Entity_1列表(不包含父对象,仅返回对象本身)
31         /// </summary>
32         /// <param name="attribute1">Attribute_1</param>
33         /// <returns>Entity_1集合</returns>
34         IList<Entity1> GetBaseEntity1ByAttribute1(string attribute1);
35 
36         /// <summary>
37         /// 获取Entity_1列表(包含父对象)
38         /// </summary>
39         /// <param name="entity1">Entity_1</param>
40         /// <returns>Entity_1集合</returns>
41         IList<Entity1> GetEntity1(Entity1 entity1);
42 
43         /// <summary>
44         /// 获取Entity_1列表(包含父对象)
45         /// </summary>
46         /// <param name="attribute1">Attribute_1</param>
47         /// <returns>Entity_1集合</returns>
48         IList<Entity1> GetEntity1ByAttribute1(string attribute1);
49 
50         /// <summary>
51         /// 插入Entity_1
52         /// </summary>
53         /// <param name="entity1">Entity_1</param>
54         void InsertEntity1(Entity1 entity1);
55 
56         /// <summary>
57         /// 更新Entity_1
58         /// </summary>
59         /// <param name="entity1">Entity_1</param>
60         int UpdateEntity1(Entity1 entity1);
61 
62         /// <summary>
63         /// 删除Entity_1
64         /// </summary>
65         /// <param name="attribute1">Attribute_1</param>
66         int DeleteEntity1ByAttribute1(string attribute1);
67 
68     }
69 }
70 

 

MapperDao 数据层操作对象

Entity1SqlMapDao.cs源代码  

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> 1 /* =====================================================
 2     作    者 : 陈励
 3     创建时间 : 2008-11-16 22:10:01
 4     修 改 者 : 
 5     修改时间 : 
 6     描    述 : Entity_1
 7 ===================================================== */
 8 
 9 using System;
10 using System.Collections.Generic;
11 using System.Text;
12 
13 using MyCompany.MyProject.Domain;
14 using MyCompany.MyProject.Persistence.Interfaces;
15 using MyCompany.IbatisNet;
16 
17 namespace MyCompany.MyProject.Persistence.MapperDao
18 {
19     /// <summary>
20     /// Entity1数据库持久化处理类
21     /// </summary>
22     public class Entity1SqlMapDao : BaseSqlMapDao, IEntity1Dao
23     {
24         /// <summary>
25         /// 获取Entity_1列表(不包含父对象,仅返回对象本身)
26         /// </summary>
27         /// <param name="entity1">Entity_1</param>
28         /// <returns>Entity_1集合</returns>
29         public IList<Entity1> GetBaseEntity1(Entity1 entity1)
30         {
31             return ExecuteQueryForList<Entity1>("GetBaseEntity1", entity1);
32         }
33 
34         /// <summary>
35         /// 获取Entity_1列表,不包含父对象,仅返回对象本身。
36         /// </summary>
37         /// <param name="attribute1">Attribute_1</param>
38         /// <returns>Entity_1集合</returns>
39         public IList<Entity1> GetBaseEntity1ByAttribute1(string attribute1)
40         {
41             return ExecuteQueryForList<Entity1>("GetBaseEntity1ByAttribute1", attribute1);
42         }
43 
44         /// <summary>
45         /// 获取Entity_1列表(包含父对象)
46         /// </summary>
47         /// <param name="entity1">Entity_1</param>
48         /// <returns>Entity_1集合</returns>
49         public IList<Entity1> GetEntity1(Entity1 entity1)
50         {
51             return ExecuteQueryForList<Entity1>("GetEntity1", entity1);
52         }
53 
54         /// <summary>
55         /// 获取Entity_1列表(包含父对象)
56         /// </summary>
57         /// <param name="attribute1">Attribute_1</param>
58         /// <returns>Entity_1集合</returns>
59         public IList<Entity1> GetEntity1ByAttribute1(string attribute1)
60         {
61             return ExecuteQueryForList<Entity1>("GetEntity1ByAttribute1", attribute1);
62         }
63 
64         /// <summary>
65         /// 插入Entity1
66         /// </summary>
67         /// <param name="Entity1">Entity_1</param>
68         public void InsertEntity1(Entity1 entity1)
69         {
70             ExecuteInsert("InsertEntity1", entity1);
71             entity1.Attribute1Old = entity1.Attribute1;
72         }
73 
74         /// <summary>
75         /// 更新Entity1
76         /// </summary>
77         /// <param name="Entity1">Entity_1</param>
78         public int UpdateEntity1(Entity1 entity1)
79         {
80             int i = ExecuteUpdate("UpdateEntity1", entity1);
81             entity1.Attribute1Old = entity1.Attribute1;
82             return i;
83         }
84 
85         /// <summary>
86         /// 删除Entity_1
87         /// </summary>
88         /// <param name="attribute1">Attribute_1</param>
89         public int DeleteEntity1ByAttribute1(string attribute1)
90         {
91             return ExecuteDelete("DeleteEntity1ByAttribute1", attribute1);
92         }
93     }
94 }
95 

 

Maps 数据对应SQL Maps

Entity1.xml 源代码 

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <!-- =====================================================
 3     作    者 : 陈励
 4     创建时间 : 2008-11-16 22:10:01
 5     修 改 者 : 
 6     修改时间 : 
 7     描    述 : Entity_1
 8 ===================================================== -->
 9 <sqlMap namespace="Entity1Map" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
10   <alias>
11     <typeAlias alias="Entity1" assembly="MyCompany.MyProject.Domain.dll" type="MyCompany.MyProject.Domain.Entity1" />
12   </alias>
13   <resultMaps>
14     <resultMap id="Entity1BaseResult" class="Entity1">
15       <result property="Attribute1" column="ATTRIBUTE_1_ENTITY_1"  des="Attribute_1" /> 
16       <result property="Attribute2" column="ATTRIBUTE_2_ENTITY_1"  des="Attribute_2" /> 
17       <result property="Attribute3" column="ATTRIBUTE_3_ENTITY_1"  des="Attribute_3" /> 
18     </resultMap>
19 
20     <resultMap id="Entity1Result" class="Entity1" extends="Entity1BaseResult" >
21     </resultMap>
22   </resultMaps>
23   <statements>
24 
25     <select id="GetBaseEntity1" parameterClass="Entity1" resultMap="Entity1BaseResult">
26       <![CDATA[ SELECT Entity_1.Attribute_1 AS ATTRIBUTE_1_ENTITY_1, Entity_1.Attribute_2 AS ATTRIBUTE_2_ENTITY_1, Entity_1.Attribute_3 AS ATTRIBUTE_3_ENTITY_1
27       FROM Entity_1 ]]>
28       <dynamic prepend="WHERE">
29         <isNotEmpty prepend="AND"  property="Attribute1">
30           <![CDATA[ Entity_1.Attribute_1 = #Attribute1# ]]>
31         </isNotEmpty>
32         <isNotEmpty prepend="AND"  property="Attribute2">
33           <![CDATA[ Entity_1.Attribute_2 = #Attribute2# ]]>
34         </isNotEmpty>
35         <isNotEmpty prepend="AND"  property="Attribute3">
36           <![CDATA[ Entity_1.Attribute_3 = #Attribute3# ]]>
37         </isNotEmpty>
38       </dynamic>
39     </select>
40 
41     <select id="GetBaseEntity1ByAttribute1" parameterClass="string" resultMap="Entity1BaseResult">
42       <![CDATA[ SELECT Entity_1.Attribute_1 AS ATTRIBUTE_1_ENTITY_1, Entity_1.Attribute_2 AS ATTRIBUTE_2_ENTITY_1, Entity_1.Attribute_3 AS ATTRIBUTE_3_ENTITY_1
43       FROM Entity_1
44       WHERE Entity_1.Attribute_1 = #value# ]]>
45     </select>
46 
47     <select id="GetEntity1" parameterClass="Entity1" resultMap="Entity1Result">
48       <![CDATA[ SELECT Entity_1.Attribute_1 AS ATTRIBUTE_1_ENTITY_1, Entity_1.Attribute_2 AS ATTRIBUTE_2_ENTITY_1, Entity_1.Attribute_3 AS ATTRIBUTE_3_ENTITY_1
49       FROM Entity_1 ]]>
50       <dynamic prepend="WHERE">
51         <isNotEmpty prepend="AND"  property="Attribute1">
52           <![CDATA[ Entity_1.Attribute_1 = #Attribute1# ]]>
53         </isNotEmpty>
54         <isNotEmpty prepend="AND"  property="Attribute2">
55           <![CDATA[ Entity_1.Attribute_2 = #Attribute2# ]]>
56         </isNotEmpty>
57         <isNotEmpty prepend="AND"  property="Attribute3">
58           <![CDATA[ Entity_1.Attribute_3 = #Attribute3# ]]>
59         </isNotEmpty>
60       </dynamic>
61     </select>
62 
63     <select id="GetEntity1ByAttribute1" parameterClass="string" resultMap="Entity1Result">
64       <![CDATA[ SELECT Entity_1.Attribute_1 AS ATTRIBUTE_1_ENTITY_1, Entity_1.Attribute_2 AS ATTRIBUTE_2_ENTITY_1, Entity_1.Attribute_3 AS ATTRIBUTE_3_ENTITY_1
65       FROM Entity_1
66       WHERE Entity_1.Attribute_1 = #value# ]]>
67     </select>
68 
69     <insert id="InsertEntity1" parameterClass="Entity1">
70       <![CDATA[ INSERT INTO Entity_1
71       (Attribute_1, Attribute_2, Attribute_3)
72       VALUES(#Attribute1#, #Attribute2#, #Attribute3#) ]]>
73     </insert>
74 
75     <update id="UpdateEntity1" parameterClass="Entity1">
76       <![CDATA[ UPDATE Entity_1
77       SET Attribute_1 = #Attribute1#, Attribute_2 = #Attribute2#, Attribute_3 = #Attribute3#
78       WHERE  Entity_1.Attribute_1 = #Attribute1Old# ]]>
79     </update>
80 
81     <delete id="DeleteEntity1ByAttribute1" parameterClass="string">
82       <![CDATA[ DELETE FROM Entity_1
83       WHERE Entity_1.Attribute_1 = #value# ]]>
84     </delete>
85 
86   </statements>
87 </sqlMap>
88 

 

5        业务层

MyCompany.MyProject.Service 

Entity1Service.cs 源代码 

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  1 /* =====================================================
  2     作    者 : 陈励
  3     创建时间 : 2008-11-16 22:10:01
  4     修 改 者 : 
  5     修改时间 : 
  6     描    述 : Entity_1
  7 ===================================================== */
  8 
  9 using System;
 10 using System.Collections.Generic;
 11 using System.Text;
 12 
 13 using IBatisNet.DataAccess;
 14 
 15 using MyCompany.MyProject.Persistence.Interfaces;
 16 using MyCompany.MyProject.Persistence.MapperDao;
 17 using MyCompany.MyProject.Domain;
 18 using MyCompany.IbatisNet;
 19 
 20 namespace MyCompany.MyProject.Service
 21 {
 22     public class Entity1Service
 23     {
 24         #region 私有字段
 25 
 26         private static Entity1Service _instance = new Entity1Service();
 27         private IDaoManager _daoManager = null;
 28         private IEntity1Dao _iEntity1Dao = null;
 29         private IEntity2Dao _iEntity2Dao = null;
 30 
 31         #endregion
 32 
 33         #region 构造函数
 34 
 35         private Entity1Service() 
 36         {
 37             _daoManager = ServiceConfig.GetInstance().DaoManager;
 38             _iEntity1Dao = _daoManager.GetDao(typeof(IEntity1Dao)) as IEntity1Dao;
 39             _iEntity2Dao = _daoManager.GetDao(typeof(IEntity2Dao)) as IEntity2Dao;
 40         }
 41 
 42         #endregion
 43 
 44         #region 公共方法
 45 
 46         public static Entity1Service GetInstance()
 47         {
 48             return _instance;
 49         }
 50 
 51         /// <summary>
 52         /// 获取Entity_1列表(不包含父对象,仅返回对象本身)
 53         /// </summary>
 54         /// <param name="entity1">Entity_1</param>
 55         /// <returns>Entity_1集合</returns>
 56         public IList<Entity1> GetBaseEntity1(Entity1 entity1)
 57         {
 58             return _iEntity1Dao.GetBaseEntity1(entity1);
 59         }
 60 
 61         /// <summary>
 62         /// 获取Entity_1列表(不包含父对象,仅返回对象本身)
 63         /// </summary>
 64         /// <param name="attribute1">Attribute_1</param>
 65         /// <returns>Entity_1集合</returns>
 66         public IList<Entity1> GetBaseEntity1ByAttribute1(string attribute1)
 67         {
 68             return _iEntity1Dao.GetBaseEntity1ByAttribute1(attribute1);
 69         }
 70 
 71         /// <summary>
 72         /// 获取Entity_1列表(包含父对象)
 73         /// </summary>
 74         /// <param name="entity1">Entity_1</param>
 75         /// <returns>Entity_1集合</returns>
 76         public IList<Entity1> GetEntity1(Entity1 entity1)
 77         {
 78             return _iEntity1Dao.GetEntity1(entity1);
 79         }
 80 
 81         /// <summary>
 82         /// 获取Entity_1列表(包含父对象)
 83         /// </summary>
 84         /// <param name="attribute1">Attribute_1</param>
 85         /// <returns>Entity_1集合</returns>
 86         public IList<Entity1> GetEntity1ByAttribute1(string attribute1)
 87         {
 88             return _iEntity1Dao.GetEntity1ByAttribute1(attribute1);
 89         }
 90 
 91         /// <summary>
 92         /// 插入Entity_1
 93         /// </summary>
 94         /// <param name="entity1">Entity_1</param>
 95         public void InsertEntity1(Entity1 entity1)
 96         {
 97             try
 98             {
 99                 _daoManager.BeginTransaction();
100                 _iEntity1Dao.InsertEntity1(entity1);
101                 if (entity1.Entity2 != null)
102                 {
103                     foreach (Entity2 entity2 in entity1.Entity2)
104                     {
105                         _iEntity2Dao.InsertEntity2(entity2);
106                     }
107                 }
108                 _daoManager.CommitTransaction();
109             }
110             catch (Exception ex)
111             {
112                 _daoManager.RollBackTransaction();
113                 throw ex;
114             }
115         }
116 
117         /// <summary>
118         /// 更新Entity_1
119         /// </summary>
120         /// <param name="entity1">Entity_1</param>
121         public int UpdateEntity1(Entity1 entity1)
122         {
123             return _iEntity1Dao.UpdateEntity1(entity1);
124         }
125 
126         /// <summary>
127         /// 删除Entity_1
128         /// </summary>
129         /// <param name="attribute1">Attribute_1</param>
130         public int DeleteEntity1ByAttribute1(string attribute1)
131         {
132             return _iEntity1Dao.DeleteEntity1ByAttribute1(attribute1);
133         }
134 
135         #endregion
136     }
137 }
138 

 

6        界面层

MyCompany.MyProject.UserControls 界面用户控件

MyCompany.MyProject.WinFrom 界面层

 

 

7        项目源码下载

源码下载:http://files.cnblogs.com/ctp0925/IbatisNet项目实例.rar

工具下载:http://files.cnblogs.com/ctp0925/CSharp.Net项目生成器(ibatis).rar

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值