介绍下使用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. Stackoverflow/dapper的Dapper-Extensions用法(二)

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

  2. Stackoverflow/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. [转载]C#中MessageBox.Show用法以及VB.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. 手机web——自适应网页设计(html/css控制)

    一. 允许网页宽度自动调整: "自适应网页设计"到底是怎么做到的? 其实并不难. 首先,在网页代码的头部,加入一行viewport元标签. <meta name=" ...

  2. SQL 行转列和列转行

    SQL 行转列和列转行 行列互转,是一个经常遇到的需求.实现的方法,有case when方式和2005之后的内置pivot和unpivot方法来实现. 在读了技术内幕那一节后,虽说这些解决方案早就用过 ...

  3. coffeeScript中类的多态[学习篇]

    类的一大应用就是多态.多态是一个面向对象编程的高级术语----“一个东西可编程很多不同的东西,但不是任何东西”.[引自coffeescript深入浅出] class Shape constructor ...

  4. 【转】nginx中的ngx_cdecl

    http://blog.csdn.net/midion9/article/details/50605337 看nginx的代码时,发现有些函数返回值之后,还有一个ngx_cdecl关键字,如:   1 ...

  5. CentOS MySQL 配置

    问题: MySQL安装后root用户无法连接,提示 ERROR 1045 (28000): Access denied for user ’root’@’localhost’ (using passw ...

  6. javascript生成对象的三种方法

    /** js生成对象的三种方法*/ // 1.通过new Object,然后添加属性 示例如下: var people1 = new Object(); people1.name = 'xiaohai ...

  7. PHP MYSQL单向同步方案

    项目上有个需求,需要从外网服务器的MYSQL同步到本地服务器上. 思路如下: 1 在外网MYSQL中创建表 table_change_log ,记录表中数据的更改记录. DROP TABLE IF E ...

  8. WebApi 学习随笔(一)

    引用MSDN: ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad ...

  9. load()方法---------jQuery动态加载html

    jquery代码 $("#div").load("test.html"); test.html   ----------------被加载页面(有<HTM ...

  10. 如何让input框自动获得焦点

    项目中有个需求  一个用扫描枪输入的input框 为了避免每次都需要人为点击 需要做成当打开页面时该input框自动获取焦点 <input type="text" name= ...