轻量级ORM框架—Dapper

轻量级ORM框架—Dapper

Dapper是.NET下一个Micro的ORM,它和Entity Framework或NHibnate不同,属于轻量级的,并且是半自动的。也就是说实体类都要自己写。它没有复杂的配置文件,一个单文件就可以了。Dapper相当于Java里的Mybatis

个人觉得他非常好用,现在已经取代了原来的SqlHelper。优点:

  • 使用Dapper可以自动进行对象映射!
  • 轻量级,单文件。
    代码就一个SqlMapper.cs文件,编译后就40K的一个很小的Dll.
  • 支持多数据库。
    Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的数据库,当然如果你知道原理也可以让它支持Mongo db,支持多数据库的本质是因为Dapper是对IDBConnection接口进行了方法扩展,
    比如你看到的SqlMapper.cs,一旦你这样做了,
    我们也知道,SqlConnection,MysqlConnection,OracleConnection都是继承于DBConnection,
    而DBConnection又是实现了IDBConnection的接口,对吧。。。
  • Dapper原理通过Emit反射IDataReader的序列队列,来快速的得到和产生对象。性能实在高高高。
  • Dapper语法简单,如果你喜欢写原始的sql,你一定喜欢Dapper。同时团队人员也很容易上手;
  • Dapper 速度快,速度接近ADO.NET访问数据库的效率;

基本的增删改查操作

Insert

Update

public int UpdateScanOmr(TB_SCAN_OMR1 model)
{
    string sqlstr = "UPDATE TB_SCAN_OMR1 " +
        "SET FINALOMR = :FINALOMR," +
        "    SCORE = :SCORE, " +
        "    SCOREDETAIL = :SCOREDETAIL" +
        " WHERE EXAMID = :EXAMID" +
        " AND PAPERID = :PAPERID";
    using (ExamConn)
    {
        return ExamConn.Execute(sqlstr, model);
    }
}

Delete

Query

  • 无参数查询,返回列表
public List GetAllExam()
{
    var sqlstr = "SELECT * FROM TB_EXAM ORDER BY SDATE DESC";
    using (EmsConn)
    {
        return EmsConn.Query(sqlstr).ToList();
    }
}
var entityList1 = new OmrRepository().GetAllExam();
foreach (var item in entityList1)
{
    Console.WriteLine(new Guid((byte[])item.EXAMGUID).ToString() + " " + item.EXAMNUM + " " + item.EXAMNAME);
}
  • 带参数查询
public List GetScanPageListByPaperId(string paperId)
{
    var sqlstr = "SELECT * FROM TB_SCAN_PAGE T WHERE T.PAPERID = :paperId";
    using (ExamConn)
    {
        return ExamConn.Query(sqlstr, new { paperId }).ToList();
    }
}
  • 单条信息
public TB_AREA GetAreaDeail(string examId,string areaId)
{
    var sqlstr = @"SELECT * FROM TB_AREA WHERE EXAMID = :examId AND AREAID = :areaId";
    using (ExamConn)
    {
        return ExamConn.Query(sqlstr, new { examId, areaId }).Single();
    }
}

事务操作

Dapper Extensions

Dapper-Extensions也是一个开源库,他在Dapper的基础上封装了基本的CRUD操作,使得一些简单的数据库操作可以不用自己写sql语句。使用起来更方面。
Dapper Extensions是github上的一个开源库是对StackOVerflow开发的Dapper ORM的一个扩展。它增加了基础的CRUD操作((Get, Insert, Update, Delete)),对更高级的查询场景,该类库还提供了一套谓词系统。它的目标是保持POCOs的纯净,不需要额外的attributes和类的继承。

Features

  • 零配置
  • 自动映射POCOs的CRUD操作
  • GetList, Count等方法可以用于更高级的场景。
  • GetPage for returning paged result sets.支持分页
  • 自动支持Guid和Integer类型的主键,(也可以手动指定其他键类型)
  • 通过使用ClassMapper可以使保证POCOs纯净。 (Attribute Free!)
  • 可以通过使用ClassMapper来自定义entity-table映射
  • 支持联合主键
  • POCO类名默认与数据表名相匹配,也可以自定义
  • 易于使用的 Predicate System适用于高级场合
  • 在生成SQL语句时正确转义表名和类名 (Ex: SELECT FirstName FROM Users WHERE Users.UserId = @ UserId_0)
  • 覆盖单元测试(覆盖了150+个单元测试)

Tdf.Dapper

more extensions for dapper

The full list of extension methods in Tdf.Dapper right now are:

Task ExecuteScalarAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?));
Task> QueryAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?));
Task> QueryAsyncDynamic(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?));
Task ExecuteAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?));
Task GetAsync(int id, IDbTransaction transaction = null, int? commandTimeout = null);
Task GetAsync(long id, IDbTransaction transaction = null, int? commandTimeout = null);
Task GetAsync(System.Guid id, IDbTransaction transaction = null, int? commandTimeout = null);
Task GetAsync(string id, IDbTransaction transaction = null, int? commandTimeout = null);
Task InsertAsync(T model, IDbTransaction transaction = null, int? commandTimeout = null);
Task DeleteAsync(T model, IDbTransaction transaction = null, int? commandTimeout = null);
Task UpdateAsync(T model, IDbTransaction transaction = null, int? commandTimeout = null);
Task GetPageAsync(string sql, object p = null, string sqlTotal = "", object p2 = null);
Task GetPageAsync(PageInput model, string tableName, string sqlWhere, dynamic pms1, dynamic pms2);

Features

For these extensions to work, the entity in question MUST have a key-property, a property named "id" or decorated with a [Key] attribute.

[Table("TB_COURSE")]
public class Course
{
    [Key]
    public int COURSEID { get; set; }
    public string COURSECODE { get; set; }
    public string COURSENAME { get; set; }
    public string DESCRIPTION { get; set; }
    public int DIS_ORDER { get; set; }
}
[Table("Act_Action")]
public class Action
{
    [Key]
    public Guid ActionId { get; set; }
    public string ActionName { get; set; }
    public int ActionValue { get; set; }
}

Get methods

or a list of all entities in the table.

var entity = await new DapperRepositoryBase().GetAllAsync();
foreach (var item in entity)
{
    Console.WriteLine(item.ActionId + " " + item.ActionName + " " + item.ActionValue);
}
var course = await new DapperRepositoryBase().GetAllAsync();
foreach (var item in course)
{
    Console.WriteLine(item.COURSEID + " " + item.COURSENAME);
}

Get one specific entity based on id

var actionId = new Guid("3F4DB83E-4D24-4875-8AC6-D4DA2F5F21B3");
var entity = await new DapperRepositoryBase().GetAsync(actionId);
if (entity != null)
{
    Console.WriteLine(entity.ActionId + " " + entity.ActionName + " " + entity.ActionValue);
}
var list = await new DapperRepositoryBase().QueryAsync("select top 1 ActionId,ActionName,ActionValue from Act_Action where ActionId=@ActionId and ActionValue=8", new { ActionId = "66307CAB-3394-4675-8FB3-5A8C90F6CD70" });
if (list != null && list.Count() > 0)
{
    Console.WriteLine("Ok");
}
// 强类型查询
var courseId = 4;
var course = await new DapperRepositoryBase().QueryAsync("SELECT * FROM TB_COURSE T WHERE COURSEID = :courseId", new { courseId });
foreach (var item in course)
{
    Console.WriteLine(item.COURSEID + " " + item.COURSENAME);
}

Console.Read();
```

**分页查询**

分页采用的Sql

```
SELECT  *
FROM    ( SELECT    row_number() OVER ( ORDER BY ActionValue ) Id ,
                    *
          FROM      ( SELECT    *
                      FROM      Act_Action
                    ) TempA
        ) AS TempInfo
WHERE   Id <= 1 * 2
        AND Id > ( 1 - 1 ) * 2
```

```
var model = new PageInput();
model.OrderStr = "ActionValue";
model.PageIndex = 0;  // 当前索引,第一页
model.PageSize = 2;   // 每页多少条
model.Offset = model.PageIndex * model.PageSize;

var sqlWhere = new System.Text.StringBuilder();
dynamic pms1 = new System.Dynamic.ExpandoObject();
dynamic pms2 = new System.Dynamic.ExpandoObject();

//sqlWhere.Append(" where ActionValue=@ActionValue");
//pms1.ActionValue = 8;

//sqlWhere.Append(" and ActionName like @ActionName");
//pms1.ActionName = string.Format("%{0}%", "查询");

pms2 = pms1;

pms2.OrderStr = model.OrderStr;

var pageOutput = await new DapperRepositoryBase().GetPageAsync(model, "Act_Action", sqlWhere.ToString(), pms1, pms2) as PageOutput;

var list = new List();
     foreach (dynamic item in pageOutput.Records)
{
    list.Add(new Action
    {
        ActionId = item.ActionId,
        ActionName = item.ActionName,
        ActionValue = item.ActionValue

    });
}

foreach (var item in list)
{
    Console.WriteLine(item.ActionId + " " + item.ActionName + " " + item.ActionValue);
}
```

##  Insert  methods

```
var action = new Action();
action.ActionName = "删除";
action.ActionValue = 4;

var result = await new DapperRepositoryBase().InsertAsync(action);
if (result == 0)
{
    Console.WriteLine("添加成功");
}
```

```
var list = new List();

list.Add(new Action()
{
    ActionName = "编辑",
    ActionValue = 2
});

var count = await new DapperRepositoryBase().ExecuteAsync("insert into Act_Action(ActionName,ActionValue) values(@ActionName,@ActionValue)", list);
if (count > 0)
{

    Console.WriteLine("成功添加了 {0} 个记录 ", count);
}
else
{
    Console.WriteLine("操作失败,请重试");
}
```

## Delete methods
**a list of entities**

```
var action = new Action();
action.ActionId = new Guid("7ACDAFAF-E3B0-4458-A4F3-6D378F063C52");

var result = await new DapperRepositoryBase().DeleteAsync(action);
if (result)
{
    Console.WriteLine("删除成功");
}
else
{
    Console.WriteLine("删除失败");
}
```

## Update methods
update a list of entities.

```
// F77F6678-3A7C-4848-94AF-B737A2BC7DD7 编辑 2
// F77F6678-3A7C-4848-94AF-B737A2BC7DD7 导入 64
var action = new Action();
action.ActionId = new Guid("F77F6678-3A7C-4848-94AF-B737A2BC7DD7");
action.ActionName = "导入";
action.ActionValue = 64;

var result = await new DapperRepositoryBase().UpdateAsync(action);
if (result != null)
{
    Console.WriteLine("更新成功");
}
else
{
    Console.WriteLine("更新失败");
}
```

```
int i = await new DapperRepositoryBase().ExecuteAsync("update Act_Action set ActionName=@ActionName,ActionValue=@ActionValue where ActionId = @ActionId", new
{
    ActionName = "查询",
    ActionValue = 8,
    ActionId = "66307CAB-3394-4675-8FB3-5A8C90F6CD70"
});

Console.WriteLine("更新了 {0} 条数据", i);
```

## SqlServer连接配置

```

  


  

```

## Oracle连接配置

```

  


  

```

你可能感兴趣的:(轻量级ORM框架—Dapper)