• MyDAL


    索引:

    目录索引

    一.安装

      在 VS 中执行一下 package 命令:  

    1 PM> Install-Package MyDAL

    二.API-快速使用

      1.命名空间,只需:

    1 using MyDAL;

      2.准备好一个 XConnection 对象,目前支持 MySQL / SQL Server ,后续会支持  Oracle / Postgre SQL ... 等等:

        以 MySQL 为例,如下:

     1 //
     2 // Nuget : Package : MySql.Data
     3 //
     4 // 不同版本 mysql 连接字符串一般如下:
     5 // "Server=localhost; Database=MyDAL_TestDB; Uid=SkyUser; Pwd=Sky@4321;"
     6 // "Server=localhost; Database=MyDAL_TestDB; Uid=SkyUser; Pwd=Sky@4321;SslMode=none;"
     7 // "Server=localhost; Database=MyDAL_TestDB; Uid=SkyUser; Pwd=Sky@4321;SslMode=none;allowPublicKeyRetrieval=true;"
     8 //
     9 var Conn = 
    10   new XConnection
    11    (
    12      new MySqlConnection
    13        ("Server=localhost; Database=MyDAL_TestDB; Uid=SkyUser; Pwd=Sky@4321;SslMode=none;allowPublicKeyRetrieval=true;")
    14     );

      3.新增一条数据:

     1             var pk = Guid.Parse("8f2cbb64-8356-4482-88ee-016558c05b2d");
     2             var m15 = new AlipayPaymentRecord
     3             {
     4                 Id = pk,
     5                 CreatedOn = DateTime.Parse("2018-08-20 19:12:05.933786"),
     6                 PaymentRecordId = Guid.Parse("e94f747e-1a6d-4be6-af51-016558c05b29"),
     7                 OrderId = Guid.Parse("f60f08e7-9678-41a8-b4aa-016558c05afc"),
     8                 TotalAmount = 0.010000000000000000000000000000M,
     9                 Description = null,
    10                 PaymentSN = "2018082021001004180510465833",
    11                 PayedOn = DateTime.Parse("2018-08-20 20:36:35.720525"),
    12                 CanceledOn = null,
    13                 PaymentUrl = "https://openapi.xxx?charset=UTF-8&app_id=zzz&version=1.0"
    14             };
    15 
    16             // 新增一条数据: AlipayPaymentRecord
    17             var res15 = await Conn.CreateAsync(m15);

        以 MySQL 为例,生成 SQL 如下:

    1 insert into `AlipayPaymentRecord` 
    2 (`Id`,`CreatedOn`,`PaymentRecordId`,`OrderId`,`TotalAmount`,`Description`,`PaymentSN`,`PayedOn`,`CanceledOn`,`PaymentUrl`)
    3 values 
    4 (?Id_2,?CreatedOn_3,?PaymentRecordId_4,?OrderId_5,?TotalAmount_6,
    5 ?Description_7,?PaymentSN_8,?PayedOn_9,?CanceledOn_10,?PaymentUrl_11);

       4.删除一条数据:

    1             var pk = Guid.Parse("8f2cbb64-8356-4482-88ee-016558c05b2d");
    2 
    3             // 删除一条数据: AlipayPaymentRecord
    4             await Conn.DeleteAsync<AlipayPaymentRecord>(it=>it.Id==pk);

        以 MySQL 为例,生成 SQL 如下:

    1 delete
    2 from `AlipayPaymentRecord`
    3 where  `Id`=?Id_1;

      5.修改一条数据:

    1             var pk1 = Guid.Parse("8f2cbb64-8356-4482-88ee-016558c05b2d");
    2 
    3             // 修改一条数据: AlipayPaymentRecord
    4             var res1 = await Conn.UpdateAsync<AlipayPaymentRecord>(it=>it.Id==pk1, new    //  where 条件: it=>it.Id==pk1
    5             {
    6                 Description = "new desc",    // 修改 AlipayPaymentRecord 字段 Description 的值为: "new desc"
    7                 PaymentUrl = "new url"    //  修改 AlipayPaymentRecord 字段 PaymentUrl 的值为: "new url"
    8             });

        以 MySQL 为例,生成 SQL 如下:

    1 update `AlipayPaymentRecord`
    2 set `Description`=?Description_1,
    3     `PaymentUrl`=?PaymentUrl_2
    4 where  `Id`=?Id_3;

      6.单表 查询一条数据:

    1             var pk1 = Guid.Parse("8f2cbb64-8356-4482-88ee-016558c05b2d");
    2 
    3             // 查询一条数据: AlipayPaymentRecord
    4             var res11 = await Conn.QueryOneAsync<AlipayPaymentRecord>(it=>it.Id==pk1);

        以 MySQL 为例,生成 SQL 如下:

    1 select *
    2 from `AlipayPaymentRecord`
    3 where  `Id`=?Id_1
    4 limit 0,1;

      7.多表连接 查询一组数据:  

     1             var res4 = await Conn
     2                 .Queryer(out AspnetUsers user4, out AspnetUserRoles userRole4, out AspnetRoles role4)
     3                 .From(() => user4)
     4                     .InnerJoin(() => userRole4)
     5                         .On(() => user4.Id == userRole4.UserId)
     6                     .InnerJoin(() => role4)
     7                         .On(() => userRole4.RoleId == role4.Id)
     8                 .Where(() => user4.NickName.StartsWith(""))
     9                 .OrderBy(() => user4.UserName)
    10                     .ThenOrderBy(() => user4.AgentLevel, OrderByEnum.Asc)
    11                 .QueryListAsync<AspnetUsers>();

        以 MySQL 为例,生成 SQL 如下:

    1 select user4.`*`
    2 from `AspNetUsers` as user4 
    3     inner join AspNetUserRoles as userRole4
    4         on user4.`Id`=userRole4.`UserId`
    5     inner join AspNetRoles as role4
    6         on userRole4.`RoleId`=role4.`Id`
    7 where  user4.`NickName` like  ?NickName_6
    8 order by user4.`UserName`  desc ,user4.`AgentLevel`  asc ;

      8.与表对应的 Model 如下:  

     1     /*
     2      * CREATE TABLE `alipaypaymentrecord` (
     3      *  `Id` char(36) NOT NULL,
     4      *  `CreatedOn` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
     5      *  `PaymentRecordId` char(36) NOT NULL,
     6      *  `OrderId` char(36) NOT NULL,
     7      *  `TotalAmount` decimal(65,30) NOT NULL,
     8      *  `Description` longtext,
     9      *  `PaymentSN` longtext,
    10      *  `PayedOn` datetime(6) DEFAULT NULL,
    11      *  `CanceledOn` datetime(6) DEFAULT NULL,
    12      *  `PaymentUrl` longtext,
    13      *  PRIMARY KEY (`Id`)
    14      *) ENGINE=InnoDB DEFAULT CHARSET=utf8
    15      */
    16     [XTable(Name = "AlipayPaymentRecord")]     // XTableAttribute 指明 该 Model 对应 DB 中的 table 名.
    17     public class AlipayPaymentRecord
    18     {
    19         public Guid Id { get; set; }
    20         public DateTime CreatedOn { get; set; }
    21         public Guid PaymentRecordId { get; set; }
    22         public Guid OrderId { get; set; }
    23         public decimal TotalAmount { get; set; }
    24         public string Description { get; set; }
    25         public string PaymentSN { get; set; }
    26         public DateTime? PayedOn { get; set; }
    27         public DateTime? CanceledOn { get; set; }
    28         public string PaymentUrl { get; set; }
    29     }

      9.快速查看 语句 对应生成的 SQL,如下:

        在 VS 输出 窗口 可以看到 语句执行前 对应 生成 的 参数化 SQL 例如 -- 新增如下:

    如果你喜欢这个平台框架,别忘了在 Github 上给点个 Star(星) 啊~~

                                             蒙

                                        2018-10-22 18:30 周一

                                        2018-11-18 16:53 周日

                                        2018-12-27 21:30 周四

                                        2019-02-07 23:55 周四

                                        2019-02-24 17:38 周日

                                        2019-04-12 17:56 周五

                                        2019-05-05 15:38 周日

                                        2019-05-20 17:50 周一

  • 相关阅读:
    Python 之 编程中常见错误
    Python 列表(数组)初识
    Python 字符串处理
    QT学习笔记三 窗口类型
    C++ Primer第五版学习笔记十 引用与指针
    C++ Primer第五版学习笔记九 变量及初始化,声明和定义,作用域
    angularf封装echarts
    记录npm yarn安装遇到的问题
    网页中嵌入google地图
    og协议-有利于SNS网站分享
  • 原文地址:https://www.cnblogs.com/Meng-NET/p/9831746.html
Copyright © 2020-2023  润新知