介绍下使用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);
}
}
}