Dapper.Extension的基本使用


前言

     上一篇隨筆寫了Dapper的簡單的使用,這次寫一下Dapper.Extension的使用,它是Dapper的簡單的封裝擴展,可以通過實例化的對象賦值后進行增刪改的操作以及分頁,但是卻不能進行多表查詢的操作,這個需要自己來擴展吧。事務的操作的話和Dapper是一樣的。

1.獲取單個實體  

  這個比較簡單直接傳入id就可以獲取返回的對象,如下所示:

        public Student ExtGet()
        {
            using(IDbConnection conn = new SqlConnection(sqlconnection))
            {
                return conn.Get<Student>(1);//這里只能傳id查詢,並且Student的字段必須與表字段匹配,多一個少一個都會報錯.所以獲取單個的對象感覺還是Dapper的原生語句好
                //return conn.Get<Student>(" select id,score,courseId,studentId from Student where id = 1 ");//會報nvarchar轉換int報錯
            }
        }

2.獲取多個實體List

  具體代碼以及注釋如下所示:

        public List<Student> ExtList()
        {
            using (IDbConnection conn = new SqlConnection(sqlconnection))
            {
                IList<ISort> sortlist = new List<ISort>();
                sortlist.Add(new Sort { PropertyName = "id", Ascending = false });//排序條件

                IList<IPredicate> preList = new List<IPredicate>();
                preList.Add(Predicates.Field<Student>(o => o.id, Operator.Eq, 2));//搜索條件,Operator有很多種的類型如eq是等於、like相當於是sql的like用法還有Lt、Le等

                BetweenValues betweenValues = new BetweenValues();//搜索條件,between搜索兩個值之間的數據
                betweenValues.Value1 = 1;
                betweenValues.Value2 = 6;
                preList.Add(Predicates.Between<Student>(o => o.id, betweenValues));

                var subPre = Predicates.Field<Student>(o => o.id, Operator.Eq, 3);//搜索條件,判斷是否存在一個條件,后面的布爾參數如果是true表明不存在,false是存在
                preList.Add(Predicates.Exists<Student>(subPre,true));

                preList.Add(Predicates.Property<Student,Student>(o => o.id , Operator.Eq , t => t.id));//判斷兩個字段的關系,比如是否相等、大於、小於等和Field差不多,並不能用於兩張表的字段判斷

                IPredicateGroup predGroup = Predicates.Group(GroupOperator.Or, preList.ToArray());//確認多個搜索條件的連接方式AND 或者 OR
                var list = conn.GetList<Student>(predGroup, sortlist).ToList<Student>();
                return list;
                //以上代碼生成的sql語句如下
                //exec sp_executesql N'SELECT [Student].[id], [Student].[name], [Student].[sex], [Student].[tel] FROM [Student] 
                //WHERE (([Student].[id] = @id_0) 
                //OR
                // ([Student].[id] BETWEEN @id_1 AND @id_2) 
                //OR
                // (NOT EXISTS (SELECT 1 FROM [Student] WHERE ([Student].[id] = @id_3))) 
                //OR 
                //([Student].[id] = [Student].[id])) 
                //ORDER BY [Student].[id] DESC',N'@id_0 int,@id_1 int,@id_2 int,@id_3 int',@id_0=2,@id_1=1,@id_2=6,@id_3=3
            }
        }

3.分頁

  分頁具體和GetList的方法是一樣的,只是多了頁碼和每頁數據數量參數,具體代碼如下:

        public List<Student> ExtPageList()
        {
            using (IDbConnection conn = new SqlConnection(sqlconnection))
            {
                IList<ISort> sortlist = new List<ISort>();
                sortlist.Add(new Sort { PropertyName = "id", Ascending = false });//排序條件

                IList<IPredicate> preList = new List<IPredicate>();
                preList.Add(Predicates.Field<Student>(o => o.id, Operator.Eq, 2));
                IPredicateGroup predGroup = Predicates.Group(GroupOperator.Or, preList.ToArray());
                return conn.GetPage<Student>(predGroup,sortlist,1,10).ToList();
            }
        }

4.添加

        /// <summary>
        /// 添加,只能單條添加
        /// 通過傳入組裝好的類對象,就可以插入數據
        /// 如果沒有數據會是NULL或者是數據庫字段的默認值
        /// </summary>
        public void ExtAdd()
        {
            using (IDbConnection conn = new SqlConnection(sqlconnection))
            {
                Student student = new Student();
                student.name = "測試學生";
                student.sex = 1;
                //student.tel= "13222222222";
                int id = conn.Insert<Student>(student);//返回自增主鍵id
            }
        }

5.更新

        /// <summary>
        /// 更新,只能單條更新
        /// 如果有字段沒有賦值的情況下,會導致數據庫字段更新為NULL,
        /// 所以需要注意,保證數據的完整
        /// </summary>
        public void ExtUpdate()
        {
            using (IDbConnection conn = new SqlConnection(sqlconnection))
            {
                Student student = new Student();
                student.id = 1013;
                student.name = "測試學生";
                student.sex = 1;
                //student.tel= "13222222222";
                if (conn.Update<Student>(student))
                {
                    //成功
                }
                else 
                {
                    //失敗
                }
            }
        }

6.刪除

        /// <summary>
        /// 刪除,只能單條刪除
        /// 這邊可以通過傳入有id的實體或者Predicates對象來指定刪除的條件,
        /// Predicates的話和List那部分的操作是一樣的,可以參考ExtList種詳細的使用
        /// </summary>
        public void ExtDel()
        {
            using (IDbConnection conn = new SqlConnection(sqlconnection))
            {
                Student student = new Student();
                student.id = 1020;
                var pre = Predicates.Field<Student>(o => o.id, Operator.Eq, 1017);
                if (conn.Delete<Student>(student))//或者conn.Delete<Student>(pre)
                {
                    //成功
                }
                else 
                {
                    //失敗
                }
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM