本文手把手实现一个简单的ORM框架,仅作为学习,免去多余的功能,力求简单。本文使用VS2022,数据库是MSSQLServer。 一、什么是ORM? ORM即对象关系映射,英文叫:ObjectRelationalMapping。它的作用是在将关系型数据库中的数据表和业务实体类之间作一个映射。ORM使得我们只需操作对象的方法和属性,即可完成对数据库的增删查改,避免了与复杂繁琐的SQL语句打交道。 ORM隐藏了访问数据库的细节,使得新手可以简单地操作数据库,但同时有些好学的技术人员希望了解ORM到底是怎么实现的。 二、ORM实现原理 我们都知道,操作数据库需要编写SQL语句,SQL语句有增删查改事务存储过程等。SQL语句都有固定的要求格式,而语句中的字段则是根据数据表字段的不同而不同。 (1)定义一个类,此类的属性与数据表的字段一一对应。 (2)利用C中的反射,获得类的字段名称,类名等信息。 (3)当要向数据库新增数据时,将字段名、类名组装成SQL语句,再连接数据库进行执行,其他删除、查询、修改的操作也类似。 三、准备工作创建数据表scheduleList 本实例使用VS2022,基于NET6,microsoftSQLServer2012。新建一数据库,名称随意,并新建一数据表,名称scheduleList。有三个字段ID、title、和createTime,分别表示是主键ID,标题和创建时间。 四、定义scheduleList类 定义一个类,名为scheduleList。代码如下:scheduleList。csVer变更日期作者V1。02022032812:33:21VX:lwdredQQ:463183376usingSnamespaceModel。schedule{summarysummary〔Serializable〕〔EntityHelper。Model。Property(scheduleList)〕publicclassscheduleList{privatelongid0;summaryIDsummary〔EntityHelper。Model。Property(EntityHelper。Model。ColumnKeyType。Identity)〕publiclongid{set{}get{}}privatestringtitlestring。Esummary标题summarypublicstringtitle{set{}get{}}privateDateTimecreatetimeDateTime。Nsummary创建时间summarypublicDateTimecreateTime{set{}get{}}}} 类scheduleList,有三个字段ID、title、和createTime,分别含义是主键ID,标题和创建时间。为简单起见,本ORM并没有作别名设计,所以注意三个字段名与数据库表中的字段需一摸一样。 其中以下语句是用来标记该字段ID作为主键。〔EntityHelper。Model。Property(EntityHelper。Model。ColumnKeyType。Identity)〕 以下语句用来标记当前类对应的数据表名是:scheduleList。〔EntityHelper。Model。Property(scheduleList)〕 五、通过反射从类信息中获得字段名称和数据表名称定义实体scheduleListschedulenewscheduleList();schedule。id1;schedule。title标题;schedule。createTimeDateTime。N 使用C中的反射(System。Reflection),从实体中获得字段列表,将字段一一填充至SQL语句中,SQL语句就组装好了。 通过反射获得实体字段Typetypetypeof(scheduleList);PropertyInfo〔〕pistype。GetProperties();foreach(PropertyInfopiinpis){Console。WriteLine(pi。Name);} 通过反射获得数据表的表名PropertyAttributeproperty(PropertyAttribute)(type。GetCustomAttributes(typeof(PropertyAttribute),false)〔0〕);Console。WriteLine(property。tableName); 新增操作的完整代码:region新增summary新增summarytypeparamnameT实体类型typeparamparamnamemodel实体数据paramparamnameisIncludeKeyColumn主键自增时:false、显式插入主键值:tureparamparamnameconnString连接字符串paramreturnsreturnspublicboolInsertT(Tmodel,boolisIncludeKeyColumn,stringconnString)whereT:class{Typetypetypeof(T);if(typenull){}PropertyInfo〔〕pistype。GetProperties();if(pisnullpis。Length0){}获取表名stringtableNameGetTableName(type);if(string。IsNullOrEmpty(tableName)){}Lif(isIncludeKeyColumnfalse){主键自增columnsGetTableColumnsList(pis,ColumnKeyType。IdentityColumnKeyType。ExtendColumnKeyType。WidthoutAdd);}else{显式插入主键值columnsGetTableColumnsList(pis,ColumnKeyType。ExtendColumnKeyType。WidthoutAdd);}if(columnsnullcolumns。Count0){}生成INSERT语句StringBuildersqlTextnewStringBuilder();sqlText。Append(INSERTINTO);sqlText。Append(tableName);sqlText。Append(();for(inti0;icolumns。Ci){sqlText。Append(columns〔i〕);if(icolumns。Count1){sqlText。Append(,);}}sqlText。Append()VALUES();for(inti0;icolumns。Ci){sqlText。AppendFormat({0},columns〔i〕);if(icolumns。Count1){sqlText。Append(,);}}sqlText。Append(););SqlParameter〔〕parasnewSqlParameter〔columns。Count〕;for(inti0;iparas。Li){PropertyInfopropertyInfotype。GetProperty(columns〔i〕);paras〔i〕newSqlParameter(columns〔i〕,GetMySqlDbType(propertyInfo。PropertyType),1);paras〔i〕。ValuepropertyInfo。GetValue(model,null);}returnSqlHelper。ExecuteNonQuery(connString,sqlText。ToString(),paras);}endregion 其它操作不再一一列出。 我已将代码上传,下载码是:4DACA2657F 下载码是啥?如何下载》三味书屋基于net6的简单ORM编码实现