什么是Dapper
Dapper是一个简单的.NET对象映射器,在速度方面具有"King of Micro ORM"的头衔,几乎与使用原始的ADO.NET数据读取器一样快。ORM是一个对象关系映射器,它负责数据库和编程语言之间的映射。
Dapper是如何工作的
它可以分为三个步骤:
Dapper通过扩展IDbConnection提供一些有用的扩展方法去查询您的数据库。Dapper.Contrib是对Dapper的进一步封装,使对象的基本增删改查等操作进一步简化。
引入NuGet包
Dapper通过NuGet安装:https://www.nuget.org/packages/Dapper
方法
Dapper会用以下几个方法扩展您的IDbConnection接口:
执行sql返回一个对象
public static T ExecuteReaderReturnT<T>(string sql, object param = null, bool useWriteConn = false, IDbTransaction transaction = null)
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(useWriteConn))
{
conn.Open();
return conn.QueryFirstOrDefault<T>(sql, param, commandTimeout: commandTimeout);
}
}
else
{
var conn = transaction.Connection;
return conn.QueryFirstOrDefault<T>(sql, param, commandTimeout: commandTimeout, transaction: transaction);
}
}
执行sql,返回影响行数
public static int ExecuteSqlInt(string sql, object param = null, IDbTransaction transaction = null)
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(true))
{
conn.Open();
return conn.Execute(sql, param, commandTimeout: commandTimeout, commandType: CommandType.Text);
}
}
else
{
var conn = transaction.Connection;
return conn.Execute(sql, param, transaction: transaction, commandTimeout: commandTimeout, commandType: CommandType.Text);
}
}
根据id获取实体
public static T GetById<T>(int id, IDbTransaction transaction = null, bool useWriteConn = false) where T : class
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(useWriteConn))
{
conn.Open();
return conn.Get<T>(id, commandTimeout: commandTimeout);
}
}
else
{
var conn = transaction.Connection;
return conn.Get<T>(id, transaction: transaction, commandTimeout: commandTimeout);
}
}
插入实体
public static long ExecuteInsert<T>(T item, IDbTransaction transaction = null) where T : class
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(true))
{
conn.Open();
var res = conn.Insert<T>(item, commandTimeout: commandTimeout);
return res;
}
}
else
{
var conn = transaction.Connection;
return conn.Insert(item, transaction: transaction, commandTimeout: commandTimeout);
}
}
更新实体
public static bool ExecuteUpdate<T>(T item, IDbTransaction transaction = null) where T : class
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(true))
{
conn.Open();
return conn.Update(item, commandTimeout: commandTimeout);
}
}
else
{
var conn = transaction.Connection;
return conn.Update(item, transaction: transaction, commandTimeout: commandTimeout);
}
}
分页查询
public static List<T> ExecutePageList<T>(string sql, string sort, int pageIndex, int pageSize, bool useWriteConn = false, object param = null)
{
string pageSql = @"SELECT TOP {0} * FROM (SELECT ROW_NUMBER() OVER (ORDER BY {1}) _row_number_,* FROM
({2})temp )temp1 WHERE temp1._row_number_>{3} ORDER BY _row_number_";
string execSql = string.Format(pageSql, pageSize, sort, sql, pageSize * (pageIndex - 1));
using (IDbConnection conn = GetConnection(useWriteConn))
{
conn.Open();
return conn.Query<T>(execSql, param, commandTimeout: commandTimeout).ToList();
}
}
[Table("test")]
public class Test
{
[Key]
public int Id {
get; set; }
[ExplicitKey]
public string Sn {
get; set; }
public string Label {
get; set; }
public string Style {
get; set; }
public int Count {
get; set; }
[Computed]
public int Doublecount => Count * 2;
[Write(false)]
public int IgnoreCol {
get; set; }
public string More_info {
get; set; }
}
如上所示,test类中包含了Contrib的属性配置
其中,Key和ExplicitKey这两项都是指定列为主键的。区别是打上Key特性的列在插入时是不能指定值的,只能是数据库自动增长列,而ExplicitKey特性可以允许在插入时指定值,比如用guid为主键类型,则主键在插入时必须已经生成好。