不用写语句的轻量级orm_Dapper,.Net下无敌的ORM

Dapper简单介绍:

Dapper is a single file you can drop in to your project that will extend your IDbConnection interface.

Dapper是一个轻型的开源ORM类,代码就一个SqlMapper.cs文件,编译后就40多K的一个很小的Dll. 官方资料:点击这里

Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的数据库,当然如果你知道原理也可以让它支持Mongo db

Dapper的r支持多表并联的对象。支持一对多 多对多的关系。并且没侵入性,想用就用,不想用就不用。无XML无属性。代码以前怎么写现在还怎么写。

Dapper原理通过Emit反射IDataReader的序列队列,来快速的得到和产生对象。性能提升了很多;(比采用常规的反射)

Dapper支持net2.0,3.0,3.5,4.0。不过就是要配置下。如果不知道如何配置查看我博客里的在2.0下使用3.5就可以了。

语法十分简单。并且无须迁就数据库的设计。

Dapper的使用:

使用Nuget安装:

a38602ce6dea5a0344cdaa3497da7f69.png

在解决方案管理器中点击项目,查看引用,如果有Dapper,说明安装成功。

1affb27223e8416ad604643afad13a33.png

Dapper的基本用法

首先配置连接字符串,这里以Sql Server为例:

7ba3d80010056715e150ce63ce6553b9.png

配置数据库连接字符串如下

使用dapper进行插入操作

private static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ToString();

///

/// 使用Dapper添加单条数据操作

///

///

///

public static int Insert(Student student)

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

return connection.Execute("insert into Student(UserName,Name) values(@UserName,@Name)", student);

}

}

///

/// 批量插入Student数据,返回影响行数

///

///

/// 影响行数

public static int Insert(List student)

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

return connection.Execute("insert into Student(UserName,Name) values(@UserName,@Name)", student);

}

}

使用dapper进行删除操作

///

/// 使用Dapper删除单条学生记录

///

///

///

public static int Delete(Student student)

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

return connection.Execute("DELETE FROM Student WHERE Id=@Id", student);

}

}

///

/// 使用Dapper批量删除学生记录

///

///

///

public static int Delete(List student)

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

return connection.Execute("DELETE FROM Student WHERE Id=@Id", student);

}

}

使用dapper进行修改操作

///

/// 单条记录修改

///

///

///

public static int Update(Student student)

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

return connection.Execute("update Student set name=@name where Id=@Id", student);

}

}

///

/// 批量修改

///

///

///

public static int Update(List student)

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

return connection.Execute("update Student set name=@name where Id=@Id", student);

}

}

使用dApper进行查询操作

///

/// 无参查询所有数据

///

///

public static List Query()

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

return connection.Query("select * from Student").ToList();

}

}

///

/// 查询指定数据

///

///

///

public static Student Query(Student Student)

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

return connection.Query("select * from Student where Id=@Id", Student).SingleOrDefault();

}

}

Dapper下In查询

///

/// In查询

///

public static List QueryIn()

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

var sql = "select * from Student where Id in @Ids";

//参数类型是Array的时候,dappper会自动将其转化

return connection.Query(sql, new { ids = new int[2] { 1, 2 }, }).ToList();

}

}

///

/// In查询

///

///

///

public static List QueryIn(int[] ids)

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

var sql = "select * from Student where id in @ids";

//参数类型是Array的时候,dappper会自动将其转化

return connection.Query(sql, new { ids }).ToList();

}

}

dapper 下多语句查询

///

/// 多语句操作

///

public static void QueryMultiple()

{

using (IDbConnection connection = new SqlConnection(connectionString))

{

var sql = "select * from Student; select * from Socre";

var multiReader = connection.QueryMultiple(sql);

var StudentList = multiReader.Read();

var bookList = multiReader.Read();

multiReader.Dispose();

}

}

}

总结

dapper使用起来,轻便快捷,灵活。运行速度快。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值