前言
上一篇随笔写了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 { //失败 } } }