自己动手写ORM框架
提起ORM框架,大家都很熟悉,网上流行的ORM框架有很多,其中出名的有一些,不出名的更是数不胜数。
下面是自己实现的一个简单的ORM框架,实现了常用的增删查改功能,供大家研究ORM实现原理。
还有一些性能优化相关的功能没有做,这里并不想重复造轮子 仅供学习参考
功能描述:
1.支持Update语句
2.支持Insert语句
3. 支持Delete语句
4.支持事务
5.支持自定义增删查改语句
6.支持单表查询 ,多表查询
7.支持排序,分页语句
8.结果集可以是DataTable 也可 以是 List<T> 泛型集合,还可以是实体对象
9.结果集支持foreach 遍历
10.支持Oracle和MSSQLServer数据库
11.需要支持其他数据库可以参照 Oracle/MSSQLServer 实现相关扩展
大部分常用功能下面代码已做了演示
示例:
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using DBMapper; 5 using System.Data; 6 7 namespace DbMapperTest 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 try 14 { 15 string str_ticks=System.DateTime.Now.Ticks.ToString(); 16 int jlbh = int.Parse(str_ticks.Substring(str_ticks.Length - 8, 8)); 17 //---oracle测试 18 using (var dbMap = DbMapper.CreateDbMapper(DbMapper.PROVIDER_ORACLE, 19 "Password=test;User ID=test;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)" 20 + "(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=testdb)));")) 21 22 //----sqlserver测试 23 //using(var dbMap=DbMapper.CreateDbMapper(DbMapper.PROVIDER_SQLSERVER,"Data Source=DINT-PC;Integrated Security=True; database=TEST")) 24 { 25 //插入记录 26 dbMap.Insert<Person>(new Person 27 { 28 JLBH = jlbh, 29 NAME = "索额图", 30 ADDRESS = "皇宫", 31 AGE = 30, 32 PNO = "zzzzzzzzzzzzzzzzz" 33 }); 34 35 dbMap.Insert<Course>(new Course() 36 { 37 JLBH = jlbh, 38 NAME = "计算机科学与技术", 39 TIME = DateTime.Now.ToString() 40 }); 41 dbMap.Insert<PersonCourse>(new PersonCourse() 42 { 43 COUR_ID = jlbh, 44 PERSON_ID = jlbh, 45 }); 46 dbMap.Execute(false); 47 if(dbMap.Query<Person>("select * from Person where JLBH=:JLBH",new Person{JLBH=jlbh}).Count>0) 48 Console.WriteLine("插入Person记录{0}成功", jlbh); 49 50 if (dbMap.Query<Course>("select * from Course where JLBH=:JLBH", new Course { JLBH = jlbh }).Count > 0) 51 Console.WriteLine("插入Course记录{0}成功", jlbh); 52 53 if (dbMap.Query<PersonCourse>("select * from PERSON_COURSE where PERSON_ID=:PERSON_ID", new PersonCourse { PERSON_ID = jlbh }).Count > 0) 54 Console.WriteLine("插入PersonCourse记录{0}成功", jlbh); 55 56 57 /*----------------修改--------------------------*/ 58 Console.WriteLine(""); 59 Console.WriteLine("-----------------修改-----------------"); 60 dbMap.Update<Course>(new Course { 61 JLBH=jlbh, 62 NAME = "计算机科学与技术(修正版)" 63 }); 64 int aff=dbMap.Execute(); 65 if (aff > 0) { 66 Console.WriteLine("插入Course记录{0}成功", jlbh); 67 } 68 69 70 /*----------------单记录查询---------------*/ 71 Console.WriteLine(""); 72 Console.WriteLine("----------------单记录查询---------------"); 73 //动态对象传递参数 .NET4.0及以上版本才支持的 74 Person aPerson = dbMap.Query<Person>("select * from Person where JLBH=:JLBH", 75 new { 76 JLBH=jlbh 77 }).First; 78 Console.WriteLine("{0} {1} {2} {3}", aPerson.JLBH, aPerson.NAME, aPerson.PNO, aPerson.ADDRESS); 79 80 /*------------------------foreach遍历结果集--------------------------------*/ 81 Console.WriteLine(""); 82 Console.WriteLine("--------foreach遍历结果集-------------"); 83 foreach (Person cc in dbMap.Query<Person, Course, PersonCourse>(@"select P.JLBH PERSON#JLBH,P.NAME PERSON#NAME,P.PNO,P.ADDRESS,P.AGE, 84 C.NAME COURSE#NAME,C.TIME from PERSON P,COURSE C,PERSON_COURSE PC 85 where P.JLBH=PC.PERSON_ID and C.JLBH=PC.COUR_ID", null, null, null).OrderBy("PERSON#JLBH", true).Take(5)) 86 { 87 Console.WriteLine("{0} {1} {2}", cc.JLBH, cc.NAME, cc.PNO); 88 } 89 90 /*------------------------------返回结果数量 Count属性-------------------------------------*/ 91 Console.WriteLine(""); Console.WriteLine(""); 92 Console.WriteLine("-------------------返回结果数量 Count属性-----------------"); 93 int countPersons = dbMap.Query<Person>("select * from Person", null).Count; 94 Console.WriteLine(countPersons.ToString()); 95 96 /*---------------------------多表查询----------------------------------------*/ 97 Console.WriteLine(""); Console.WriteLine(""); 98 Console.WriteLine("--------------------多表查询----------------------"); 99 100 DataTable at = dbMap.Query<Object>(@"select P.JLBH PERSON#JLBH,P.NAME PERSON#NAME,P.PNO,P.ADDRESS,P.AGE, 101 C.NAME COURSE#NAME,C.TIME from PERSON P,COURSE C,PERSON_COURSE PC 102 where P.JLBH=PC.PERSON_ID and C.JLBH=PC.COUR_ID", null).OrderBy("PERSON#JLBH", false).Skip(1).Take(5).ToTable(); 103 foreach (DataRow arow in at.Rows) 104 { 105 Console.WriteLine("{0} {1} {2} {3} {4}", arow["PERSON#JLBH"], arow["PERSON#NAME"], arow["COURSE#NAME"], arow["TIME"], arow["PNO"]); 106 } 107 108 109 /*-------------------------返回结果集-----------------------------*/ 110 Console.WriteLine(""); Console.WriteLine(""); 111 Console.WriteLine("--------------------返回结果集----------------------"); 112 List<Person> lstPersons = dbMap.Query<Person>("select * from Person", null).OrderBy("JLBH", true).Skip(0).Take(5).ToList(); 113 foreach (Person p in lstPersons) 114 { 115 Console.WriteLine("{0} {1} {2} {3} {4}", p.JLBH, p.NAME, p.PNO, p.AGE, p.ADDRESS); 116 } 117 } 118 } 119 catch(Exception ex){ 120 Console.WriteLine(ex.Message); 121 } 122 } 123 } 124 125 /*还差一个根据表自动生成对象的工具 这里手动生成*/ 126 class Person : IDbTableNaming { 127 [PrimaryKey]//标示该列为主键 Update 函数需要 128 public int JLBH { get; set; } 129 public string NAME { get; set; } 130 public string PNO { get; set; } 131 public string ADDRESS { get; set; } 132 public int AGE { get; set; } 133 string IDbTableNaming.DBTableName//映射到数据库中的表名 如果没有 则默认为类名 134 { 135 get { return "PERSON"; } 136 } 137 } 138 class Course : IDbTableNaming { 139 [PrimaryKey] 140 public int JLBH { get; set; } 141 public string NAME { get; set; } 142 public string TIME { get; set; } 143 string IDbTableNaming.DBTableName 144 { 145 get { return "COURSE"; } 146 } 147 } 148 class PersonCourse:IDbTableNaming { 149 public int PERSON_ID { get; set; } 150 public int COUR_ID { get; set; } 151 string IDbTableNaming.DBTableName 152 { 153 get { return "PERSON_COURSE"; } 154 } 155 } 156 }