DapperExtensions的基本用法
介绍下使用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的基本用法的更多相关文章
- Stackoverflow/dapper的Dapper-Extensions用法(二)
之前翻译了Dapper-Extensions项目首页的readme.md,大家应该对这个类库的使用有一些了解了吧,接下来是wiki的文档翻译,主要提到了AutoClassMapper.KeyTypes ...
- Stackoverflow/dapper的Dapper-Extensions用法(一)
Dapper-Extensions Dapper Extensions is a small library that complements Dapper by adding basic CRUD ...
- dapper的Dapper-Extensions用法(一)
dapper的Dapper-Extensions用法(一) Dapper-Extensions Dapper Extensions is a small library that complement ...
- dapper-dot-net用法及其扩展系列
dapper-dot-net用法及其扩展系列 虽然已经一段时间没写.net了,但是昨天看了下dapper和Dapper-Extensions在github仍然有更新,他们的受欢迎程度可想而知.所以想把 ...
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
随机推荐
- Python之路,day4-Python基础
1.集合2.元组 只读列表,只有count,index2个方法3.字典key-value对 1.特性 2.查询速度快,比列表快python中的hash在同一程序下值相同python字典中的hash只有 ...
- NFS文件系统制作
内核: linux-3.0 u-boot: 2010.09 开发板: fl2440(s3c2440主芯片) 交叉编译器: 2011.11 ...
- Vector 和 ArrayList 区别
1.Vector是多线程安全的,而ArrayList不是,如果只有一个线程会访问到集合,那最好是使用ArrayList,因为它不考虑线程安全,效率会高些:Vector是旧的,是java一诞生就提供了的 ...
- C#遍历指定文件夹中的所有文件和子文件夹
参考:http://www.cnblogs.com/skylaugh/archive/2012/09/23/2698850.html DirectoryInfo TheFolder=new Direc ...
- 总结.NET 中什么时候用 Static
静态类和类成员用于创建无需创建类的实例就能够访问的数据和函数.当类中没有依赖对象标识的数据或行为时,就可以使用静态类.静态类成员是可独立于任何对象标识的数据和行为,即无论对象发生什么更改,这些数据和函 ...
- 浅入浅出EmguCv(二)EmguCv打开指定图片
从这篇文章开始,会介绍一些通过EmguCv实现的一些简单的功能,这个内容的更新会跟我学习OpenCv的进度有关,最近在看一本关于OpenCv的书——<学习OpenCv>,主要例子还是通过这 ...
- mac版微信web开发者工具(小程序开发工具)无法显示二维码 解决方案
微信小程序概念的提出,绝对可以算得上中国IT界惊天动地的一件大事,这可能意味着一场新的开发热潮即将到来, 我也怀着激动的心情准备全身心投入其中,不过截止目前,在官方网站上下载的最新版本都无法使用,打开 ...
- 【java】之3种方式实现Object和Map之间的转换
利用commons.BeanUtils实现Obj和Map之间转换,这种是最简单,也是最经常用的 public static Object mapToObject(Map<String, Obje ...
- ngCordova
参见:http://blog.csdn.net/Luo_xinran/article/details/52164480 ngCordova是基于Cordova封装的AngularJS的调用本地设备接口 ...
- Creating Classes 创建类
The dojo/_base/declare module is the foundation of class creation within the Dojo Toolkit. declare a ...