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 ...
随机推荐
- CSS中的class与id区别及用法
转自http://www.divcss5.com/rumen/r3.shtml及http://www.jb51.net/css/35927.html 我们平常在用DIV CSS制作Xhtml网页页面时 ...
- ecs CentOS 7 安装 mysql (mariadb)
检查之前是否已经安装 rpm -qa | grep mariadb 如果已安装,卸载 yum remove mysql mysql-server mysql-libs compat-mysql51 开 ...
- awk 的使用
awk [-F field-separator] 'commands' input-file(s) 其中,commands 是真正awk命令,[-F域分隔符]是可选的.input-file(s) 是待 ...
- 在Ubuntu上安装LAMP服务器
1.安装Ubuntu上安装LAMP apt-get install lamp-server^ 2.安装过程中设置MySql密码 3.测试 创建index.php var/www/html/index. ...
- [Linux] - CentOS中文乱码解决办法
CentOS 7 终端中文乱码解决办法: 1.使用vim编辑locale.config文件: vim /etc/locale.conf 2.将LANG="en_US.UTF-8"修 ...
- [PHP] - Laravel - 修改laravel_session的cookie名称
修改Cookie laravel_session的名称方法: 打开文件:config\session.php 找到值:laravel_session 修改为你所需要的cookie名称即可. 当然,还有 ...
- 为模版设计师而生的Twig(上)-Twig使用指南
原文地址:http://my.oschina.net/veekit/blog/268828 1. 概要 模板是一个简单的文本文件.它可以生成任何基于文本的格式(HTML.XML.CSV等).它不具有特 ...
- MyBatis学习(三)、动态SQL语句
三.动态SQL语句 有些时候,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息.使用Orac ...
- Keepalive双主搭建配置
Keepalive 双主搭建配置 keepalived保证双主数据库的可用性 环境说明 192.168.1.10 keepalive 主1 192.168.1.20 keepalive 主2 19 ...
- javascript Date
转:http://blog.csdn.net/vbangle/article/details/5643091# // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日( ...