DapperExtensions的基本用法

时间:2022-09-10 13:00:16

介绍下使用Dapper-Extensions的基本语法

//实体类
DemoEntity entity = new DemoEntity();

//根据实体主键删除
this.Delete<DemoEntity>(entity);

//根据主键ID删除
this.Delete<DemoEntity>(1);

//增加
this.Insert<DemoEntity>(entity);

//更新
bool result = this.Update<DemoEntity>(entity);

//根据主键返回实体
entity = this.GetById<DemoEntity>(1);

//返回 行数
this.Count<DemoEntity>(new { ID = 1 });

//查询所有
IEnumerable<DemoEntity> list = this.GetAll<DemoEntity>();

IList<ISort> sort = new List<ISort>();
sort.Add(new Sort { PropertyName = "ID", Ascending = false });

//条件查询
list = this.GetList<DemoEntity>(new { ID = 1, Name = "123" }, sort);

//orm 拼接条件 查询
IList<IPredicate> predList = new List<IPredicate>();
predList.Add(Predicates.Field<DemoEntity>(p => p.Name, Operator.Like, "不知道%"));
predList.Add(Predicates.Field<DemoEntity>(p => p.ID, Operator.Eq, 1));
IPredicateGroup predGroup = Predicates.Group(GroupOperator.And, predList.ToArray());

list = this.GetList<DemoEntity>(predGroup);

//分页查询
long allRowsCount = 0;
this.GetPageList<DemoEntity>(1, 10, out allRowsCount, new { ID = 1 }, sort);

RepositoryServiceBase.cs(IDataServiceRepository的实现类)

using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using DapperExtensions;
using HY.DataAccess;

namespace HY.ORM
{
public class RepositoryServiceBase : IDataServiceRepository
{
public RepositoryServiceBase()
{
}
public RepositoryServiceBase(IDBSession dbSession)
{
DBSession = dbSession;
}

public IDBSession DBSession { get; private set; }

public void SetDBSession(IDBSession dbSession)
{
DBSession = dbSession;
}

/// <summary>
/// 根据Id获取实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="primaryId"></param>
/// <returns></returns>
public T GetById<T>(dynamic primaryId) where T : class
{
return DBSession.Connection.Get<T>(primaryId as object, databaseType: DBSession.DatabaseType);
}

/// <summary>
/// 根据多个Id获取多个实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ids"></param>
/// <returns></returns>
public IEnumerable<T> GetByIds<T>(IList<dynamic> ids) where T : class
{
var tblName = string.Format("dbo.{0}", typeof(T).Name);
var idsin = string.Join(",", ids.ToArray<dynamic>());
var sql = "SELECT * FROM @table WHERE Id in (@ids)";
IEnumerable<T> dataList = DBSession.Connection.Query<T>(sql, new { table = tblName, ids = idsin });
return dataList;
}

/// <summary>
/// 获取全部数据集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IEnumerable<T> GetAll<T>() where T : class
{
return DBSession.Connection.GetList<T>(databaseType: DBSession.DatabaseType);
}

/// <summary>
/// 统计记录总数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <param name="buffered"></param>
/// <returns></returns>
public int Count<T>(object predicate, bool buffered = false) where T : class
{
return DBSession.Connection.Count<T>(predicate, databaseType: DBSession.DatabaseType);
}

/// <summary>
/// 查询列表数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <param name="sort"></param>
/// <param name="buffered"></param>
/// <returns></returns>
public IEnumerable<T> GetList<T>(object predicate = null, IList<ISort> sort = null,
bool buffered = false) where T : class
{
return DBSession.Connection.GetList<T>(predicate, sort, null, null, buffered, databaseType: DBSession.DatabaseType);
}

/// <summary>
/// 分页
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="allRowsCount"></param>
/// <param name="predicate"></param>
/// <param name="sort"></param>
/// <param name="buffered"></param>
/// <returns></returns>
public IEnumerable<T> GetPageList<T>(int pageIndex, int pageSize, out long allRowsCount,
object predicate = null, IList<ISort> sort = null, bool buffered = true) where T : class
{
if (sort == null)
{
sort = new List<ISort>();
}
IEnumerable<T> entityList = DBSession.Connection.GetPage<T>(predicate, sort, pageIndex, pageSize, null, null, buffered, databaseType: DBSession.DatabaseType);
allRowsCount = DBSession.Connection.Count<T>(predicate, databaseType: DBSession.DatabaseType);
return entityList;
}

/// <summary>
/// 插入单条记录
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <param name="transaction"></param>
/// <returns></returns>
public dynamic Insert<T>(T entity, IDbTransaction transaction = null) where T : class
{
dynamic result = DBSession.Connection.Insert<T>(entity, transaction, databaseType: DBSession.DatabaseType);
return result;
}

/// <summary>
/// 更新单条记录
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <param name="transaction"></param>
/// <returns></returns>
public bool Update<T>(T entity, IDbTransaction transaction = null) where T : class
{
bool isOk = DBSession.Connection.Update<T>(entity, transaction, databaseType: DBSession.DatabaseType);
return isOk;
}

/// <summary>
/// 删除单条记录
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="primaryId"></param>
/// <param name="transaction"></param>
/// <returns></returns>
public int Delete<T>(dynamic primaryId, IDbTransaction transaction = null) where T : class
{
var entity = GetById<T>(primaryId);
var obj = entity as T;
int isOk = DBSession.Connection.Delete<T>(obj, databaseType: DBSession.DatabaseType);
return isOk;
}

/// <summary>
/// 删除单条记录
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <param name="transaction"></param>
/// <returns></returns>
public int DeleteList<T>(object predicate = null, IDbTransaction transaction = null) where T : class
{
return DBSession.Connection.Delete<T>(predicate, transaction, databaseType: DBSession.DatabaseType);
}

/// <summary>
/// 批量插入功能
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entityList"></param>
/// <param name="transaction"></param>
public bool InsertBatch<T>(IEnumerable<T> entityList, IDbTransaction transaction = null) where T : class
{
bool isOk = false;
foreach (var item in entityList)
{
Insert<T>(item, transaction);
}
isOk = true;
return isOk;
}

/// <summary>
/// 批量更新()
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entityList"></param>
/// <param name="transaction"></param>
/// <returns></returns>
public bool UpdateBatch<T>(IEnumerable<T> entityList, IDbTransaction transaction = null) where T : class
{
bool isOk = false;
foreach (var item in entityList)
{
Update<T>(item, transaction);
}
isOk = true;
return isOk;
}

/// <summary>
/// 批量删除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ids"></param>
/// <param name="transaction"></param>
/// <returns></returns>
public bool DeleteBatch<T>(IEnumerable<dynamic> ids, IDbTransaction transaction = null) where T : class
{
bool isOk = false;
foreach (var id in ids)
{
Delete<T>(id, transaction);
}
isOk = true;
return isOk;
}

}
}

using System;
using System.Collections.Generic;
using System.Data;
using Dapper;
using DapperExtensions;
using HY.DataAccess;

namespace HY.ORM
{
/// <summary>
/// Repository基类
/// </summary>
public class RepositoryBase : RepositoryServiceBase, IDataRepository
{

public RepositoryBase()
{
}

public new void SetDBSession(IDBSession dbSession)
{
base.SetDBSession(dbSession);
}

public RepositoryBase(IDBSession dbSession)
: base(dbSession)
{
}

/// <summary>
/// 根据条件筛选出数据集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <param name="buffered"></param>
/// <returns></returns>
public IEnumerable<T> Get<T>(string sql, dynamic param = null, bool buffered = true) where T : class
{

return DBSession.Connection.Query<T>(sql, param as object, DBSession.Transaction, buffered);
}

/// <summary>
/// 根据条件筛选数据集合
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <param name="buffered"></param>
/// <returns></returns>
public IEnumerable<dynamic> Get(string sql, dynamic param = null, bool buffered = true)
{
return DBSession.Connection.Query(sql, param as object, DBSession.Transaction, buffered);
}

/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="allRowsCount"></param>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <param name="allRowsCountSql"></param>
/// <param name="allRowsCountParam"></param>
/// <param name="buffered"></param>
/// <returns></returns>
public IEnumerable<T> GetPage<T>(int pageIndex, int pageSize, out long allRowsCount, string sql, dynamic param = null, string allRowsCountSql = null, dynamic allRowsCountParam = null, bool buffered = true) where T : class
{
IEnumerable<T> entityList = DBSession.Connection.GetPage<T>(pageIndex, pageSize, out allRowsCount, sql, param as object, allRowsCountSql, null, null, buffered, databaseType: DBSession.DatabaseType);
return entityList;
}

/// <summary>
/// 根据表达式筛选
/// </summary>
/// <typeparam name="TFirst"></typeparam>
/// <typeparam name="TSecond"></typeparam>
/// <typeparam name="TReturn"></typeparam>
/// <param name="sql"></param>
/// <param name="map"></param>
/// <param name="param"></param>
/// <param name="transaction"></param>
/// <param name="buffered"></param>
/// <param name="splitOn"></param>
/// <param name="commandTimeout"></param>
/// <returns></returns>
public IEnumerable<TReturn> Get<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map,
dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id",
int? commandTimeout = null)
{
return DBSession.Connection.Query(sql, map, param as object, transaction, buffered, splitOn);
}

/// <summary>
/// 根据表达式筛选
/// </summary>
/// <typeparam name="TFirst"></typeparam>
/// <typeparam name="TSecond"></typeparam>
/// <typeparam name="TReturn"></typeparam>
/// <param name="sql"></param>
/// <param name="map"></param>
/// <param name="param"></param>
/// <param name="transaction"></param>
/// <param name="buffered"></param>
/// <param name="splitOn"></param>
/// <param name="commandTimeout"></param>
/// <returns></returns>
public IEnumerable<TReturn> Get<TFirst, TSecond, TThird, TReturn>(string sql, Func<TFirst, TSecond, TThird, TReturn> map,
dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id",
int? commandTimeout = null)
{
return DBSession.Connection.Query(sql, map, param as object, transaction, buffered, splitOn);
}

/// <summary>
/// 获取多实体集合
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <param name="transaction"></param>
/// <param name="commandTimeout"></param>
/// <param name="commandType"></param>
/// <returns></returns>
public SqlMapper.GridReader GetMultiple(string sql, dynamic param = null, IDbTransaction transaction = null,
int? commandTimeout = null, CommandType? commandType = null)
{
return DBSession.Connection.QueryMultiple(sql, param as object, transaction, commandTimeout, commandType);
}

/// <summary>
/// 执行sql操作
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public int Execute(string sql, dynamic param = null, IDbTransaction transaction = null)
{
return DBSession.Connection.Execute(sql, param as object, transaction);
}

}
}

DapperExtensions的基本用法的更多相关文章

  1. *&sol;dapper的Dapper-Extensions用法(二)

    之前翻译了Dapper-Extensions项目首页的readme.md,大家应该对这个类库的使用有一些了解了吧,接下来是wiki的文档翻译,主要提到了AutoClassMapper.KeyTypes ...

  2. *&sol;dapper的Dapper-Extensions用法(一)

    Dapper-Extensions Dapper Extensions is a small library that complements Dapper by adding basic CRUD ...

  3. dapper的Dapper-Extensions用法(一)

    dapper的Dapper-Extensions用法(一) Dapper-Extensions Dapper Extensions is a small library that complement ...

  4. dapper-dot-net用法及其扩展系列

    dapper-dot-net用法及其扩展系列 虽然已经一段时间没写.net了,但是昨天看了下dapper和Dapper-Extensions在github仍然有更新,他们的受欢迎程度可想而知.所以想把 ...

  5. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  6. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  7. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  8. &lbrack;转载&rsqb;C&num;中MessageBox&period;Show用法以及VB&period;NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  9. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

随机推荐

  1. 烂泥:智能DNS使用与配置

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 公司的业务现在已经扩展到海外,对外提供的统一接口都是通过域名来解析的,但是海外用户访问国 ...

  2. 【原】无规矩&comma;不方圆——说一说正则里的exec&lpar;&rpar;和test&lpar;&rpar;

    今天一大早遇就遇到一件诡异的事儿,可能是思绪还没有澄静下来,一下子没反应过来.事情是这样的: 模板: <input class="name" type="text& ...

  3. WP7系统托盘和应用程序栏

    (一)系统托盘和应用程序栏系统托盘(1)显示系统级别的状态信息(2)Apps能隐藏和显示系统托盘Micosoft.Phone.Shell.SystemTray.IsVisible=true;应用程序栏 ...

  4. js实现表格

    主要方法如下,然后今天学到了js的几个函数知识点. 1.eval()函数: 定义和用法eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法eval(string) 其 ...

  5. java附件上传下载大字段版

    public int up2(Map map) { StringBuffer insertSQL = new StringBuffer(); insertSQL.append("insert ...

  6. 染色&lbrack;SDOI2011&rsqb;

    题目描述 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如"11 ...

  7. 我的第一个python web开发框架(41)——总结

    我的第一个python web开发框架系列博文从17年6.7月份开始写(存了近十章稿留到9月份才开始发布),到今天结束,一年多时间,想想真不容易啊. 整个过程断断续续,中间有段时间由于工作繁忙停了好长 ...

  8. git学习02 - log查看&amp&semi;版本回退

    1.查看更新记录 git log /  git log --pretty=oneline D:\learn\git_test>git log commit a915e7b12076673d778 ...

  9. SQL SERVER2014 安装 Error code 0x858C001B&period;

    原因是语言版本不一致,SQL SERVER是中文简体版,操作系统是英文版,在操作系统.控制面板,区域语言设置为中文就Ok啦. TITLE: SQL Server Setup failure.----- ...

  10. uiwebview与objective-c

    利用oc调用js很简单, 系统直接提供了方法stringByEvaluatingJavaScriptFromString [webView stringByEvaluatingJavaScriptFr ...