(新)自己动手写ORM框架(1)-增删查改的使用

   之前写过一个系列文章自己动手写ORM框架,经过在多个项目的中的使用,对这套代码进行了许多改进,下面是使用方法:

 新增学员信息代码预览:

 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("新增成功!");   }

 

  修改学员信息:

  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);

 

  查询:

   //查询所有学员信息

  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; }

    }

}

 

  最后是配置文件:

  <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>

  只需加连接字符串和数据库类型,因为支持这里可以支持多种数据库。

 

 源码托管在github上,需要的可以去下载。

点击下载:https://github.com/wangwei123/easy4net

 

 

你可能感兴趣的:(orm)