(新)本身動手寫ORM框架(1)-增刪查改的使用

   以前寫過一個系列文章本身動手寫ORM框架,通過在多個項目的中的使用,對這套代碼進行了許多改進,下面是使用方法:git

 新增學員信息代碼預覽:github

 DBHelper db = DBHelper.getInstance();
 Student stu = new Student();
  stu.Name = "張三";
  stu.Gender = "";
  stu.Age = 28;
  stu.Address = "上海市徐彙區";
  
int count = db.Save<Student>(stu);   if (count > 0)   {   MessageBox.Show("新增成功!");   }

 

  修改學員信息:sql

  stu.UserID = 1;
  stu.Name = "李四";
  stu.Age = 22;
  db.Update<Student>(stu);

 

  刪除學員信息:數據庫

  Student student = m_stuList[i];

  //對象刪除
  db.Remove<Student>(student);

  //ID刪除
  db.Remove<Student>(student.UserID);

 

  查詢:app

   //查詢全部學員信息
  List<Student> list = DB.FindAll<Student>();

  //根據ID查詢
  Student student = DB.FindById<Student>(5);

  //自定義SQL查詢
  List<Student> list1 = DB.FindBySql<Student>("SELECT * FROM U_Student WHERE U_Age < 28");

  //按某個列查詢
  List<Student> list2 = DB.FindByProperty<Student>("U_Name", "張三");

  //按精確條件查詢,這裏是SELECT xxx FROM U_Student WHERE U_Name LIKE '%張%' OR U_Age < 28
  DbCondition cond1 = new DbCondition().Where().Like("U_Name", "").OrLessThan("U_Age", 28);
  List<Student> list3 = DB.Find<Student>(cond1);

  //關聯查詢,這個不用多說了,會SQL的都知道,查詢條件是 WHERE U_Name LIKE '張%'
  DbCondition cond2 = new DbCondition("SELECT s.*,c.teacher,c.className FROM U_Student s INNER JOIN U_Class c ON s.classID = c.ID").Where().RightLike("U_Name", "");
  List<Student> list4 = DB.Find<Student>(cond2);

  //這裏是查詢 SELECT count(0) FROM U_Student WHERE U_Name = '張三' AND U_Age = 28
  DbCondition cond3 = new DbCondition().Where("U_Name", "張三").And("U_Age", 28);
  int count = DB.FindCount<Student>(cond3);

 

實體類配置:框架

namespace Entiry
{
    [Serializable]
    [Table(Name = "U_Student")]
    public class Student
    {
        //主鍵 INDENTITY自動增加標識
        [Id(Name = "UserID", Strategy = GenerationType.INDENTITY)]
        public int UserID { get; set; }

        //對應數據庫中的名字爲U_Name
        [Column(Name = "U_Name")]
        public string Name { get; set; }

        [Column(Name = "U_Age")] // int? 容許int爲NULL時不會報錯
        public int? Age { get; set; }

        [Column(Name = "U_Gender")]
        public string Gender { get; set; }

        [Column(Name = "U_Address")]
        public string Address { get; set; }

        [Column(Name = "U_CreateTime")]
        public DateTime? CreateTime { get; set; }

        [Column(Name = "ClassID")]
        public int? ClassID { get; set; }

        //下面2列 ClassName和Teacher字段是屬於班級表中的班級名稱和班主任
        //可是由於是外鍵表,關聯的班級編號:ClassID,因此作關聯查詢能夠加這2個屬性
        //可是修改和插入則不須要這2列,只作查詢,因此加上IsInsert=false,IsUpdate=false
        [Column(Name = "ClassName",IsInsert=false,IsUpdate=false)]
        public string ClassName { get; set; }

        [Column(Name = "Teacher", IsInsert = false, IsUpdate = false)]
        public string Teacher { get; set; }
    }
}

 

  最後是配置文件:sqlserver

  <configuration>
    <appSettings>
      <add key="DbType" value="sqlserver"/>
      <add key="connectionString" value="Data Source=.;Initial Catalog=OrmDB;User ID=test;Password=test;Trusted_Connection=no;Min Pool Size=10;Max Pool Size=100;"/>
  </appSettings>spa

  只需加鏈接字符串和數據庫類型,由於支持這裏能夠支持多種數據庫。code

 

 源碼託管在github上,須要的能夠去下載。server

點擊下載:https://github.com/wangwei123/easy4net

相關文章
相關標籤/搜索