今天闲来无事,做了个的ORM
感觉有点像ActiveRecord作业风格,又有点像iBatis
先上数据表结构图:
然后建立一个简单的POCO类,字段名称和类型应与数据库对应。
1 using JDML.TestPoco;
2 using JDML.StaticExtensions;
3
4 namespace JDML.TestPoco
5 {
6 public class User_Role
7 {
8 public int ? User_Role_ID { get ; set ; }
9 public string User_Role_Name { get ; set ; }
10 public string User_Role_Permission { get ; set ; }
11 public string User_Role_Remark { get ; set ; }
12 public DateTime ? UpdateTime { get ; set ; }
13 }
14 }
15
基本操作
Insert:
1 User_Role user_role = new User_Role() { User_Role_Name = " Manage " };
2 user_role.UpdateTime = DateTime.Now;
3 user_role.Insert();
4
无需配置,一跳数据就添加到了User_Role表中了。
Update:
1 User_Role role = new User_Role() { User_Role_ID = 17 };
2 role.Load();
3 role.User_Role_Name = " zjj " ;
4 role.User_Role_Permission = " agg " ;
5 role.Update();
6
读取:
Read:
单条记录:
1 User_Role role = new User_Role() { User_Role_ID = 17 };
2 role.Load();
3
多条:
1 User_Role role = new User_Role();
2 // role.ID = 1;
3 role.User_Role_Name = " Sales " ;
4 IList < Object > list = role.Find();
5 foreach (Object one in list)
6 {
7 UserRole onerole = one as UserRole;
8 Console.WriteLine( " Role_ID: " + onerole.User_Role_ID + " , Role_Name: " + onerole.User_Role_Name + " , Role_Permission: " + onerole.User_Role_Permission + " , UpdateTime: " + onerole.UpdateTime.GetValueOrDefault().ToString());
9 }
10
表映射:
1 using JDML.Attributes;
2
3 namespace JDML.TestPoco
4 {
5
6 [Table( " User_Role " )]
7 public class UserRole
8 {
9 // [Field("User_Role_ID", true)]
10 [Field(FieldName = " User_Role_ID " , PrimaryKey = true )]
11 public int ? ID { get ; set ; }
12 [Field(FieldName = " User_Role_Name " , PrimaryKey = true )]
13 public string Name { get ; set ; }
14 [Field( " User_Role_Permission " )]
15 public string Permission { get ; set ; }
16 public DateTime ? UpdateTime { get ; set ; }
17 }
18 }
19
只要用Table和Field特性定义表名称以及字段名称,就能自己映射。
让我们再来做点复杂点的查询:
添加特性声明:
1 [SQL(MappingClass.SQLCmdType.Select, " byID " , " Select * from User_Role where User_Role_ID > @ID " )]
2 public class UserRole
3
查询并显示:
1 UserRole role = new UserRole();
2 role.ID = 50 ;
3 IList < Object > list = role.Find( " byID " );
4 foreach (Object one in list)
5 {
6 UserRole onerole = one as UserRole;
7 Console.WriteLine( " Role_ID: " + onerole.ID + " , Role_Name: " + onerole.Name + " , Role_Permission: " + onerole.Permission + " , UpdateTime: " + onerole.UpdateTime.GetValueOrDefault().ToString());
8 }
9
这样,ID大于50的所有User_Role表中的行都会输出到vs的输出栏中。
更复杂点:
不与表一对一映射:
像 iBatis一样,自定义 Select Insert Update Delete 操作的SQL语句:
1 [SQL(MappingClass.SQLCmdType.Insert, " Insert Into User_Role (User_Role_Name,User_Role_Permission) values (@Name,@Permission) " )]
2 [SQL(MappingClass.SQLCmdType.Select, " Select * from User_Role where User_Role_ID=@ID " )]
3 [SQL(MappingClass.SQLCmdType.Select, " byName " , " Select * from User_Role where User_Role_Name=@Name " )]
4 [SQL(MappingClass.SQLCmdType.Update, " update User_Role set User_Role_Permission=@Permission where User_Role_ID=@ID " )]
5 [SQL(MappingClass.SQLCmdType.Delete, " delete from User_Role where User_Role_ID=@ID " )]
6
然后,再来进行调用试试看:
UserRole role = new UserRole() { ID = 8 };
role.Load();
看User_Role_ID 为 8 的行,载入了!
总共有public boolLoad() 载入单条数据,数据行小于1,返回null,数据行大于2,报错
Public bool Find(string SqlCmdName = “”); 其中sqlcmdname参数为上面定义的
[SQL(MappingClass.SQLCmdType.Select, "byName","Select * from User_Role whereUser_Role_Name=@Name")]
Bool Insert() bool Update() bool Delete()则分别调用
[SQL(MappingClass.SQLCmdType.Insert, "InsertInto User_Role (User_Role_Name,User_Role_Permission) values(@Name,@Permission)")]
[SQL(MappingClass.SQLCmdType.Update, "updateUser_Role set User_Role_Permission=@Permission where User_Role_ID=@ID")]
[SQL(MappingClass.SQLCmdType.Delete,"delete from User_Role whereUser_Role_ID=@ID")]
如果SQL是这样的
[SQL(MappingClass.SQLCmdType.Delete, “byName”,"delete from User_Role where User_Role_Name=@Name")]
则可以这样调用: bool successful =role.Delete(“byName”);
不好意思,代码多了点。框架还没完成。
等完成了会拿出来Share的!!
多谢支持了!!