基於Dapper的開源LINQ擴展,且支持分庫分表自動生成實體二

LnskyDB GitHub stars GitHub forks

LnskyDB是基於Dapper的Lambda擴展,支持按時間分庫分表,也能夠自定義分庫分表方法.並且能夠T4生成實體類免去手寫實體類的煩惱.git

文檔地址: https://liningit.github.io/LnskyDB/github

開源地址: https://github.com/liningit/LnskyDBsql

nuget地址: https://www.nuget.org/packages/LnskyDB/app

功能特色

  • Lambda表達式查詢方便
    基於Dapper的Lambda表達式擴展能夠方便的進行查詢篩選操做框架

  • 支持分庫分表
    默認支持按年分庫按月分表,也支持自定義分庫分表.今後大數據不用愁測試

  • T4自動生成實體
    有T4模板自動生成實體類,不再用手寫那些煩人的實體類了.倉儲類及接口也支持自動生成大數據

  • 使用門檻低,快速上手
    使用很是簡單,能夠快速上手ui

注意問題

本框架只支持單表的Lambda表達式查詢,若是多表須要手寫sql,框架支持根據sql查詢修改等.
另外不太建議連表查詢,推薦在邏輯層處理code

開源協議

MIT license.接口

上一期咱們寫了分庫分表的增刪改查,這一期咱們寫一下不分庫分表的增刪改查:

不分庫分表

查詢

  1. 根據主鍵查詢
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
var entity = repository.Get(new ProductSaleByDayNSEntity
{
    SysNo = sysNo
});
  1. 根據where條件查詢
var stTime = new DateTime(2019, 1, 15);
var endTime = new DateTime(2019, 2, 11);
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
var query = QueryFactory.Create<ProductSaleByDayNSEntity>(m => m.ShopName.Contains("測試"));
query.And(m => m.StatisticalDate >= stTime && m.StatisticalDate < endTime.Date.AddDays(1));
query.OrderByDescing(m => m.StatisticalDate);
query.StarSize = 20; //能夠設置查詢行數及開始行數
query.Rows = 10;
var lst = repository.GetList(query);
  1. 分頁查詢
var stTime = new DateTime(2019, 1, 15);
var endTime = new DateTime(2019, 2, 11);
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
var query = QueryFactory.Create<ProductSaleByDayNSEntity>(m => m.ShopName.Contains("測試"));
query.And(m => m.StatisticalDate >= stTime);
query.And(m => m.StatisticalDate < endTime.Date.AddDays(1));
query.OrderByDescing(m => m.StatisticalDate);
query.StarSize = 20;
query.Rows = 10;
var paging = repository.GetPaging(query);
var count = paging.TotalCount;
var lst = paging.ToList();//或者paging.Items

添加

var addEntity = new ProductSaleByDayNSEntity()
{
    SysNo = Guid.NewGuid(),
    DataSource = "測試來源",
    ProductID = Guid.NewGuid(),               
    ShopID = Guid.NewGuid(),
    ShopName = "測試店鋪",
    ProductName = "測試商品",
    OutProductID = Guid.NewGuid().ToString(),
    ImportGroupId = Guid.NewGuid(),
    StatisticalDate = DateTime.Now
};
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
//若是新增主鍵是自增列會自動賦值自增列值到主鍵
repository.Add(addEntity);

更新

  1. 根據主鍵更新
var updateEntity = new ProductSaleByDayNSEntity()
{
    SysNo = Guid.Parse("650BC09C-2B9C-467B-A457-8B4853CC1F0F"),
    DataSource = "測試來源修改",
    ShopName = "店鋪修改"
};
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
//根據主鍵更新其餘字段
return repository.Update(updateEntity);
  1. 根據where條件更新
var updateEntity = new ProductSaleByDayNSEntity()
{
    DataSource = "測試來源修改",
    ShopName = "店鋪修改Where",
    DBModel_ShuffledTempDate = new DateTime(2019, 01, 05),
};
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
var where = QueryFactory.Create<ProductSaleByDayNSEntity>(m => m.ShopName == "測試店鋪1" && m.StatisticalDate > new DateTime(2019, 01, 03));//where是更新條件
return repository.Update(updateEntity, where);

刪除

  1. 根據主鍵刪除
var deleteEntity = new ProductSaleByDayNSEntity()
{
    SysNo = Guid.Parse("650BC09C-2B9C-467B-A457-8B4853CC1F0F")
};
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
return repository.Delete(deleteEntity);
  1. 根據where條件刪除
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
var where = QueryFactory.Create<ProductSaleByDayNSEntity>();
//QueryiSearch方法表示搜索裏面空格表示或+表示且
//如 導入+手工 自動+生成 表示字段必須同時擁有導入和手工或者自動和生成
//生成sql是 and ((DataSource like '%導入%' and DataSource like '%手工%') or DataSource like '%自動%' and DataSource like '%生成%')            
where.QueryiSearch(m => m.DataSource, "新+更");
where.QueryiSearch(m => m.ShopName, "批量修改");
//注意若是是更新用的是實體類的DBModel_ShuffledTempDate Query中的無效
return repository.Delete(where);
相關文章
相關標籤/搜索