zoukankan      html  css  js  c++  java
  • Dapper-开源小型ORM

     一些关于Dapper的介绍:

     1.Dapper是一个轻型的开源ORM类,代码就一个SqlMapper.cs文件,编译后就40多K的一个很小的Dll. 

     2.Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的数据库

     3.Dapper的r支持多表并联的对象。支持一对多 多对多的关系。并且没侵入性。

     4.Dapper原理通过Emit反射IDataReader的序列队列,来快速的得到和产生对象。性能提升了很多;(比采用常规的反射)并且无须迁就数据库的设计。

     Dapper源码下载链接:

     点击这里下载dapper源代码

     Demo:

               var connection = GetOpenConnection();
    
                var guid = Guid.NewGuid();
                string id = "6e2a106d-d838-48b9-ac74-ad604457bba2";
    
                //1泛型
                var dog = connection.Query<Dog>("select * from Dog where Id = @Id", new { Id = id });
                
                //2 动态解析
                var rows = connection.Query("select * from Dog where Id = @Id", new { Id = id }).ToList();
                foreach (dynamic item in rows)
                {
                     String 黑客 = item.Name;
                }
    Query
                //3.执行不返回结果
                int result=connection.Execute("insert into Dog values(@age,@id,@name,@weight,@ignoredproperty)", new { age = 49, id = Guid.NewGuid(), name = "YZR", weight = 117.5, ignoredproperty = 1 });
    Execute
                //4.批量Execute
                List<Dog> list = new List<Dog>();
                list.Add(new Dog() { Age = 9, Id = Guid.NewGuid(), Name = "ZXX", Weight = 120 });
                list.Add(new Dog() { Age = 9, Id = Guid.NewGuid(), Name = "WMJ", Weight = 120 });
                List<dynamic> paramsList = new List<dynamic>();
                foreach (Dog item in list)
                {
                    paramsList.Add(new { age = item.Age, id = item.Id, name = item.Name, weight = item.Weight, ignoredproperty = 1 });
                }
                int result = connection.Execute("insert into Dog values(@age,@id,@name,@weight,@ignoredproperty)", paramsList);
    批量Execute
                //5.In操作
                dog = connection.Query<Dog>("select * from Dog where id in @ids", new { ids = new String[] { "6e2a106d-d838-48b9-ac74-ad604457bba3", "6e2a106d-d838-48b9-ac74-ad604457bba2", "535dab3a-d3c1-4cb0-b8f7-63351f491056" } });
                //等价于
                dog = connection.Query<Dog>("select * from Dog where id in (@id1,@id2,@id3)", new { id1 = "6e2a106d-d838-48b9-ac74-ad604457bba3", id2 = "6e2a106d-d838-48b9-ac74-ad604457bba2", id3 = "535dab3a-d3c1-4cb0-b8f7-63351f491056" });
    In操作
                //数据库要建立主外键关系
                var sql =
                          @"select p.*,u.* from Dog p 
                                        left join Owner u on u.OwnerId = p.Id 
                                         where p.id=@id Order by p.Id";
                Dog d = null;
                var t = connection.Query(sql, new { id = "6e2a106d-d838-48b9-ac74-ad604457bba2" });
                var data = connection.Query<Dog, Owner, Dog>(sql, (post, user) =>
                {
                    if (d == null || d.Id != post.Id)
                    {
                        d = post;
                    }
                    if (user != null)
                    {
                        d.user.Add(user);
                    }
                    return post;
                }, new { id = "6e2a106d-d838-48b9-ac74-ad604457bba2" });
    一对多关系
                //多查询的结果集
                sql =
                     @"select * from Dog where Id = @id
                       select * from Owner";
    
                using (var multi = connection.QueryMultiple(sql, new { id = "6e2a106d-d838-48b9-ac74-ad604457bba3" }))
                {
    
                     var data = multi.Read<Dog>().ToList();
                     var owner = multi.Read<Owner>().ToList();
                 }
    QueryMultiple
                //事务
    
                using (connection)
                {
                    //开始事务
                    IDbTransaction transaction = connection.BeginTransaction();
                    try
                    {
                        string query = "update Dog set Age=Age+1 where Id=@Id";
                        string query2 = "update Dog set Weight=Weight+1.0 where Id=@Id";
                        connection.Execute(query, new { Id = "71fff309-c7c9-4f64-b588-0a03e27459ba" }, transaction, null, null);
                        connection.Execute(query2, new { Id = "71fff309-c7c9-4f64-b588-0a03e27459ba" }, transaction, null, null);
                        //提交事务
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        //出现异常,事务Rollback
                        transaction.Rollback();
                        throw new Exception(ex.Message);
                    }
                }
    IDbTransaction
                //存储过程
    
                var p = new DynamicParameters();
                //实例1
                //p.Add("@result", dbType: DbType.Int32, direction: ParameterDirection.Output);
                //var user = connection.Query("Test", p, commandType: CommandType.StoredProcedure);
                //int totalCount = p.Get<int>("@result");
    
                //实例2
                //注意点:调用如果使用query,那么存储过程需要有select结果集
                //p.Add("@result", dbType: DbType.Int32, direction: ParameterDirection.Output);
                //p.Add("@rowcount", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
                //var user = connection.Query("TestValue", p, commandType: CommandType.StoredProcedure);
                //int re = p.Get<int>("@result");
                //int ro = p.Get<int>("@rowcount");
    
                //实例3
                //存储过程分页需要row_number() over( order by id)这个排序的id需要指定
                p.Add("@PageIndex", 1, dbType: DbType.Int32, direction: ParameterDirection.Input);
                p.Add("@PageSize", 3, dbType: DbType.Int32, direction: ParameterDirection.Input);
                p.Add("@TableName", "Dog", dbType: DbType.String, direction: ParameterDirection.Input);
                p.Add("@Where", " 1=1 order by id asc ", dbType: DbType.String, direction: ParameterDirection.Input);
                //p.Add("@rowcount", dbType: DbType.Int32, direction: ParameterDirection.Output);
                p.Add("@rowcount", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
                var result = connection.Query("SelectBase", p, commandType: CommandType.StoredProcedure);
    
                int count = p.Get<int>("@rowcount");
    Procedure
                //切换数据库
                connection.ChangeDatabase("数据库名称");
    ChangeDatabase

    Demo源代码下载

    点击这里下载

     

  • 相关阅读:
    BZOJ.1468.Tree(点分治)
    BZOJ.1935.[SHOI2007]Tree园丁的烦恼(CDQ分治 三维偏序)
    BZOJ.4319.[cerc2008]Suffix reconstruction(后缀数组 构造 贪心)
    BZOJ.3262.陌上花开([模板]CDQ分治 三维偏序)
    洛谷.3374.[模板]树状数组1(CDQ分治)
    BZOJ.4566.[HAOI2016]找相同字符(后缀数组 单调栈)
    POJ.3145.Common Substrings(后缀数组 倍增 单调栈)
    POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀数组 倍增)
    POJ.1743.Musical Theme(后缀数组 倍增 二分 / 后缀自动机)
    UOJ.35.[模板]后缀排序(后缀数组 倍增)
  • 原文地址:https://www.cnblogs.com/Francis-YZR/p/5528299.html
Copyright © 2011-2022 走看看