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安装:
在解决方案管理器中点击项目,查看引用,如果有Dapper,说明安装成功。
Dapper的基本用法
首先配置连接字符串,这里以Sql Server为例:
配置数据库连接字符串如下
<connectionStrings>
<add name="connectionString" connectionString="server=.;user id=sa;password=123456;database=Dapper"/>
</connectionStrings>
使用dapper进行插入操作
private static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
/// <summary>
/// 使用Dapper添加单条数据操作
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public static int Insert(Student student)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Execute("insert into Student(UserName,Name) values(@UserName,@Name)", student);
}
}
/// <summary>
/// 批量插入Student数据,返回影响行数
/// </summary>
/// <param name="student"></param>
/// <returns>影响行数</returns>
public static int Insert(List<Student> student)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Execute("insert into Student(UserName,Name) values(@UserName,@Name)", student);
}
}
使用dapper进行删除操作
/// <summary>
/// 使用Dapper删除单条学生记录
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public static int Delete(Student student)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Execute("DELETE FROM Student WHERE Id=@Id", student);
}
}
/// <summary>
/// 使用Dapper批量删除学生记录
/// </summary>
/// <param name="Students"></param>
/// <returns></returns>
public static int Delete(List<Student> student)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Execute("DELETE FROM Student WHERE Id=@Id", student);
}
}
使用dapper进行修改操作
/// <summary>
/// 单条记录修改
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public static int Update(Student student)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Execute("update Student set name=@name where Id=@Id", student);
}
}
/// <summary>
/// 批量修改
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public static int Update(List<Student> student)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Execute("update Student set name=@name where Id=@Id", student);
}
}
使用dApper进行查询操作
/// <summary>
/// 无参查询所有数据
/// </summary>
/// <returns></returns>
public static List<Student> Query()
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Query<Student>("select * from Student").ToList();
}
}
/// <summary>
/// 查询指定数据
/// </summary>
/// <param name="Student"></param>
/// <returns></returns>
public static Student Query(Student Student)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Query<Student>("select * from Student where Id=@Id", Student).SingleOrDefault();
}
}
Dapper下In查询
/// <summary>
/// In查询
/// </summary>
public static List<Student> QueryIn()
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
var sql = "select * from Student where Id in @Ids";
//参数类型是Array的时候,dappper会自动将其转化
return connection.Query<Student>(sql, new { ids = new int[2] { 1, 2 }, }).ToList();
}
}
/// <summary>
/// In查询
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
public static List<Student> QueryIn(int[] ids)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
var sql = "select * from Student where id in @ids";
//参数类型是Array的时候,dappper会自动将其转化
return connection.Query<Student>(sql, new { ids }).ToList();
}
}
dapper 下多语句查询
/// <summary>
/// 多语句操作
/// </summary>
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<Student>();
var bookList = multiReader.Read<Socre>();
multiReader.Dispose();
}
}
总结
dapper使用起来,轻便快捷,灵活。运行速度快。