Dapper简介:Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的。也就是说实体类都要自己写。它没有复杂的配置文件,一个单文件就可以了。
GIT地址:https://github.com/StackExchange/dapper-dot-net
Dapper扩展:https://github.com/tmsmith/Dapper-Extensions
一、Dapper优点
1.轻量级,单文件。
2.支持多数据库。
3.支持多表关联。
二、Dapper使用
在NuGet中搜索Dapper添加安装。如图:
三、Daaper实战
1.数据库连接
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //引用 using Dapper; using System.Data.SqlClient; using System.Configuration; using System.Data; using MySql.Data.MySqlClient; namespace CA.Dapper.Base { public class SQLConnection { private static string sqlConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); public static IDbConnection conn = new MySqlConnection(sqlConnection); } }
2.Models
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CA.Dapper.Models { [Serializable] public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } public DateTime CreateTime { get; set; } } }
3.Dal
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //引用 using Dapper; using CA.Dapper.Base; using System.Data; namespace CA.Dapper.Dal { public class PersonDal { public void Query(int rows = 0) { string sql = string.Format("select id,name,age,address,createtime from person limit {0}", rows); var list = SQLConnection.conn.Query(sql); } } }
4.Dto
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //引用 using CA.Dapper.Dal; using CA.Dapper.Models; namespace CA.Dapper.Dto { public class PersonDto : PersonDal { public Person PersonInfo { get; set; } public List<Person> PersonList { get; set; } } }
5.BO
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CA.Dapper.Base { // 摘要: // 单例模式抽象基础类,要求派生类有一个无参公共构造函数 public class SignBo : Base<SignBo> // { public Singleton():base(){} } // // 类型参数: // T: public abstract class TBase<T> where T : new() { private static T instance; protected static readonly object syncRoot = new object(); public static T Instance { get { if (TBase<T>.instance == null) { lock (TBase<T>.syncRoot) { if (TBase<T>.instance == null) { TBase<T>.instance = ((default(T) == null) ? Activator.CreateInstance<T>() : default(T)); } } } return TBase<T>.instance; } } } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //引用 using CA.Dapper.Base; using CA.Dapper.Dto; using CA.Dapper.Models; namespace CA.Dapper.Bo { public sealed partial class PersonBO : TBase<PersonBO> { PersonDto dto = new PersonDto(); public PersonBO() : base() { } public void Query(int rows) { dto.Query(rows); } } }
6.调用
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; //引用 using CA.Dapper.Bo; namespace CA.Dapper { class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); PersonBO.Instance.Query(50*10000); sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); } } }
测试用的数据是百万级的,效果还是很ok的,跟Daaper一样高效率ORM还有一款 Chloe,Git地址:https://github.com/shuxinqin/Chloe
跟Dapper、Chloe、EF上着之间有一个性能比较的测试,http://blog.jobbole.com/104442/,各有特色,希望能对您有帮助。