zoukankan      html  css  js  c++  java
  • 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包

    4.4 封装搭建

     该类实现接口

        public interface IDaoBase<T, TKey> where T : class
        {
            int Count(IPredicate predicate, bool buffered = false);
            bool Delete(IPredicate predicate);
            bool DeleteByID(TKey key);
            bool DeleteByIDList(Expression<Func<T, object>> expression, IEnumerable<TKey> idList);
            int Execute(string sql, dynamic param = null);
            bool Exists(string sql, dynamic param = null, bool buffered = false);
            IEnumerable<TReturn> Get<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null);
            IList<T> GetAll();
            T GetByID(TKey key);
            IEnumerable<T> GetByIDList(Expression<Func<T, object>> expression, IEnumerable<TKey> idList, bool buffered = false);
            T GetEntity(IPredicate predicate, bool buffered = false);
            T GetEntity(string sql, object param, bool buffered = false);
            PagedList<T> GetEntityPageList(SearchBase search);
            IList<T> GetList(IPredicate predicate = null, IList<ISort> sort = null, bool buffered = false);
            IList<T> GetList(string sql, object param = null, bool buffered = true);
            IList<TEntity> GetList<TEntity>(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<TScale>(string sql, dynamic param = null, bool buffered = false);
            dynamic Insert(T entity);
            void InsertBatch(IEnumerable<T> entityList, IDbTransaction transaction = null);
            bool Update(T entity);
            void UpdateBatch(IEnumerable<T> entityList);
        }

    数据连接工厂

      public class ConnectionFactory
        {
            /// <summary>
            /// 数据库连接
            /// </summary>
            public static string ConnectionString { set; get; }
    
            /// <summary>
            /// 数据库类型
            /// </summary>
            public static DatabaseType DataBaseType { set; get; }
    
    
            /// <summary>
            /// 初始化
            /// </summary>
            /// <param name="conectionString">连接串</param>
            /// <param name="dataBaseType">数据库类型</param>
            public static void Init(string conectionString, DatabaseType dataBaseType = DatabaseType.SqlServer)
            {
                ConnectionFactory.ConnectionString = conectionString;
                ConnectionFactory.DataBaseType = dataBaseType;
            }
    
    
            /// <summary>
            /// 创建数据库连接
            /// </summary>
            /// <returns></returns>
            public static IDbConnection CreateConnection()
            {
                IDbConnection connection = null;
    
                switch (ConnectionFactory.DataBaseType)
                {
                    case DatabaseType.SqlServer:
                        connection = new SqlConnection(ConnectionFactory.ConnectionString);
                        break;
    
                }
    
                return connection;
            }
        }
     public class TestDao : DaoBase<Test,int>
        {
            /// <summary>
            /// 获取所有数据
            /// </summary>
            /// <returns></returns>
            public IList<Test> GetAllList()
            {
                string sql = @"
                    SELECT*FROM Test";
                return this.GetList<Test>(sql);
            }
    
            
            /// <summary>
            /// 根据名称获取实体
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public Test GetByName(string name)
            {
                string sql = @"
                    SELECT*FROM Test WHERE testName=@testName";
                return this.GetEntity(sql, new { testName = name });
            }
            
        }
    View Code

    构造一个简单Winform界面

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

    <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演示

  • 相关阅读:
    【原】相煎何太急——input的blur事件与button的click事件
    【原】jQuery与CSS自动生成验证码
    【转】HTML转义字符大全
    【原】来自于一位前端“布道者”的建议
    【原】如何在jQuery中实现闭包
    【转】Web前端研发工程师编程能力飞升之路
    【原】git如何删除本地和远程的仓库
    H5不同样式新闻垂直滚动效果
    mui防止软键盘弹起方法
    js显示对象所有属性和方法的函数
  • 原文地址:https://www.cnblogs.com/CallmeYhz/p/5584337.html
Copyright © 2011-2022 走看看