Dapper扩展之~~~Dapper.Contrib
平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill
上一篇文章:Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
官方地址:https://github.com/StackExchange/dapper-dot-net/tree/master/Dapper.Contrib
实战案例:https://github.com/dunitian/LoTCode/tree/master/PawChina(更新ing)
注意点:Model里面的Table和Key是Dapper.Contrib.Extensions命名空间下的~~~~如果不是~~请看下篇文章(点我)
用法很简单,贴一下帮助类:
/// <summary> /// 扩展方法 /// </summary> public abstract partial class DapperDataAsync { #region 查询系 /// <summary> /// 获取Model-Key为int类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public static async Task<T> GetAsync<T>(int id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new() { using (var conn = ConnFactory.GetConnection()) { return await conn.GetAsync<T>(id, transaction, commandTimeout); } } /// <summary> /// 获取Model-Key为long类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public static async Task<T> GetAsync<T>(long id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new() { using (var conn = ConnFactory.GetConnection()) { return await conn.GetAsync<T>(id, transaction, commandTimeout); } } /// <summary> /// 获取Model-Key为Guid类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public static async Task<T> GetAsync<T>(System.Guid id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new() { using (var conn = ConnFactory.GetConnection()) { return await conn.GetAsync<T>(id, transaction, commandTimeout); } } /// <summary> /// 获取Model-Key为string类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public static async Task<T> GetAsync<T>(string id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new() { using (var conn = ConnFactory.GetConnection()) { return await conn.GetAsync<T>(id, transaction, commandTimeout); } } /// <summary> /// 获取Model集合(没有Where条件) /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static async Task<IEnumerable<T>> GetAllAsync<T>() where T : class, new() { using (var conn = ConnFactory.GetConnection()) { return await conn.GetAllAsync<T>(); } } #endregion #region 增删改 /// <summary> /// 插入一个Model /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="sqlAdapter"></param> /// <returns></returns> public static async Task<int> InsertAsync<T>(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new() { using (var conn = ConnFactory.GetConnection()) { return await conn.InsertAsync<T>(model, transaction, commandTimeout); } } /// <summary> /// 更新一个Model /// </summary> /// <typeparam name="T"></typeparam> /// <param name="connection"></param> /// <param name="entityToUpdate"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public static async Task<T> UpdateAsync<T>(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new() { using (var conn = ConnFactory.GetConnection()) { bool b = await conn.UpdateAsync<T>(model, transaction, commandTimeout); if (b) { return model; } else { return null; } } } #endregion #region 分页查询 /// <summary> /// 分页查询(为什么不用out,请参考:http://www.cnblogs.com/dunitian/p/5556909.html) /// </summary> /// <param name="sql">查询语句</param> /// <param name="p">动态参数</param> /// <param name="sqlTotal">total语句</param> /// <param name="p2">Total动态参数</param> /// <returns></returns> public static async Task<string> PageLoadAsync<T>(string sql, object p = null, string sqlTotal = "", object p2 = null) { var rows = await QueryAsync<T>(sql.ToString(), p); var total = rows.Count(); if (!sqlTotal.IsNullOrWhiteSpace()) { total = await ExecuteScalarAsync<int>(sqlTotal, p2); } return new { rows = rows, total = total }.ObjectToJson(); } #endregion }