平台之大势何人能挡? 带着你的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命名空间下的~~~~如果不是~~请看下篇文章(点我)
用法很简单,贴一下帮助类:
///
/// 扩展方法
///
public abstract partial class DapperDataAsync
{
#region 查询系
///
/// 获取Model-Key为int类型
///
///
///
///
///
///
public static async Task GetAsync(int id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
using (var conn = ConnFactory.GetConnection())
{
return await conn.GetAsync(id, transaction, commandTimeout);
}
}
///
/// 获取Model-Key为long类型
///
///
///
///
///
///
public static async Task GetAsync(long id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
using (var conn = ConnFactory.GetConnection())
{
return await conn.GetAsync(id, transaction, commandTimeout);
}
}
///
/// 获取Model-Key为Guid类型
///
///
///
///
///
///
public static async Task GetAsync(System.Guid id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
using (var conn = ConnFactory.GetConnection())
{
return await conn.GetAsync(id, transaction, commandTimeout);
}
}
///
/// 获取Model-Key为string类型
///
///
///
///
///
///
public static async Task GetAsync(string id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
using (var conn = ConnFactory.GetConnection())
{
return await conn.GetAsync(id, transaction, commandTimeout);
}
}
///
/// 获取Model集合(没有Where条件)
///
///
///
public static async Task> GetAllAsync() where T : class, new()
{
using (var conn = ConnFactory.GetConnection())
{
return await conn.GetAllAsync();
}
}
#endregion
#region 增删改
///
/// 插入一个Model
///
///
///
///
///
///
///
public static async Task InsertAsync(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
using (var conn = ConnFactory.GetConnection())
{
return await conn.InsertAsync(model, transaction, commandTimeout);
}
}
///
/// 更新一个Model
///
///
///
///
///
///
///
public static async Task UpdateAsync(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
using (var conn = ConnFactory.GetConnection())
{
bool b = await conn.UpdateAsync(model, transaction, commandTimeout);
if (b) { return model; }
else { return null; }
}
}
#endregion
#region 分页查询
///
/// 分页查询(为什么不用out,请参考:http://www.cnblogs.com/dunitian/p/5556909.html)
///
/// 查询语句
/// 动态参数
/// total语句
/// Total动态参数
///
public static async Task PageLoadAsync(string sql, object p = null, string sqlTotal = "", object p2 = null)
{
var rows = await QueryAsync(sql.ToString(), p);
var total = rows.Count();
if (!sqlTotal.IsNullOrWhiteSpace()) { total = await ExecuteScalarAsync(sqlTotal, p2); }
return new { rows = rows, total = total }.ObjectToJson();
}
#endregion
}