Dapper.Net 应用

                                                     Dapper应用

1.Dapper是什么

    Dapper是一款轻量级ORM工具。如果你在小的项目中,使用Entity Framework、NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀。你又觉得ORM省时省力,这时Dapper 将是你不二的选择。

2.为什么使用

  1. 轻量,编译完成之后只有120k(好象是变胖了)
  2. 速度快。Dapper的速度接近与IDataReader,取列表的数据超过了DataTable。
  3. 支持多种数据库。Dapper可以在所有Ado.net Providers下工作,包括sqlite, sqlce, firebird, oracle, MySQL, PostgreSQL and SQL Server
  4. 可以映射一对一,一对多,多对多等多种关系。
  5. 性能高。通过Emit反射IDataReader的序列队列,来快速的得到和产生对象,性能不错。
  6. 支持FrameWork2.0,3.0,3.5,4.0,4.5

3.使用Dapper.Net并演示

1. 使用Sqlserver创建测试表

2.创建winform应用程序,引用Dapper封装基础应用和框架

3.创建简单页面实现CRUD

4.开始实现

4.1创建表

CREATE DATABASE test
USE test
GO
CREATE TABLE Test
(
 testId INT PRIMARY KEY IDENTITY,
 testName VARCHAR(50) NULL
)

INSERT INTO Test VALUES ('CallmeYhz')

INSERT INTO Test  VALUES('周公瑾')

4.2实体

  public class Test
    {
        public int testId { get; set; }
        public string testName { get; set; }
    }

4.3 Nuget包

Dapper.Net 应用_第1张图片

4.4 封装搭建

 该类实现接口

    public interface IDaoBase where T : class
    {
        int Count(IPredicate predicate, bool buffered = false);
        bool Delete(IPredicate predicate);
        bool DeleteByID(TKey key);
        bool DeleteByIDList(Expressionobject>> expression, IEnumerable idList);
        int Execute(string sql, dynamic param = null);
        bool Exists(string sql, dynamic param = null, bool buffered = false);
        IEnumerable Get(string sql, Func map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null);
        IList GetAll();
        T GetByID(TKey key);
        IEnumerable GetByIDList(Expressionobject>> expression, IEnumerable idList, bool buffered = false);
        T GetEntity(IPredicate predicate, bool buffered = false);
        T GetEntity(string sql, object param, bool buffered = false);
        PagedList GetEntityPageList(SearchBase search);
        IList GetList(IPredicate predicate = null, IList sort = null, bool buffered = false);
        IList GetList(string sql, object param = null, bool buffered = true);
        IList GetList(string sql, object param = null, bool buffered = true);
        SqlMapper.GridReader GetMultiple(string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
        TScale GetScale(string sql, dynamic param = null, bool buffered = false);
        dynamic Insert(T entity);
        void InsertBatch(IEnumerable entityList, IDbTransaction transaction = null);
        bool Update(T entity);
        void UpdateBatch(IEnumerable entityList);
    }

数据连接工厂

  public class ConnectionFactory
    {
        /// 
        /// 数据库连接
        /// 
        public static string ConnectionString { set; get; }

        /// 
        /// 数据库类型
        /// 
        public static DatabaseType DataBaseType { set; get; }


        /// 
        /// 初始化
        /// 
        /// 连接串
        /// 数据库类型
        public static void Init(string conectionString, DatabaseType dataBaseType = DatabaseType.SqlServer)
        {
            ConnectionFactory.ConnectionString = conectionString;
            ConnectionFactory.DataBaseType = dataBaseType;
        }


        /// 
        /// 创建数据库连接
        /// 
        /// 
        public static IDbConnection CreateConnection()
        {
            IDbConnection connection = null;

            switch (ConnectionFactory.DataBaseType)
            {
                case DatabaseType.SqlServer:
                    connection = new SqlConnection(ConnectionFactory.ConnectionString);
                    break;

            }

            return connection;
        }
    }
 public class TestDao : DaoBaseint>
    {
        /// 
        /// 获取所有数据
        /// 
        /// 
        public IList GetAllList()
        {
            string sql = @"
                SELECT*FROM Test";
            return this.GetList(sql);
        }

        
        /// 
        /// 根据名称获取实体
        /// 
        /// 
        /// 
        public Test GetByName(string name)
        {
            string sql = @"
                SELECT*FROM Test WHERE testName=@testName";
            return this.GetEntity(sql, new { testName = name });
        }
        
    }
View Code

构造一个简单Winform界面

Dapper.Net 应用_第2张图片

配置数据库连接字符串并且初始化

<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    startup>
  <connectionStrings>
    
    <add name="db" connectionString=" Data Source=.;Initial Catalog=test;Integrated Security=SSPI; " />

  connectionStrings>
configuration>
     //初始化数据库连接
            ConnectionFactory.Init(ConfigurationManager.ConnectionStrings["db"].ConnectionString);

4.5演示

Dapper.Net 应用_第3张图片

转载于:https://www.cnblogs.com/CallmeYhz/p/5584337.html

你可能感兴趣的:(Dapper.Net 应用)