LnskyDB " " " " LnskyDB是基於Dapper的Lambda擴展,支持按時間分庫分表,也可以自定義分庫分表方法.而且可以T4生成實體類免去手寫實體類的煩惱. 文檔地址: https://liningit.github.io/LnskyDB/ 開源地址: https://github ...
LnskyDB
LnskyDB是基於Dapper的Lambda擴展,支持按時間分庫分表,也可以自定義分庫分表方法.而且可以T4生成實體類免去手寫實體類的煩惱.
文檔地址: https://liningit.github.io/LnskyDB/
開源地址: https://github.com/liningit/LnskyDB
nuget地址: https://www.nuget.org/packages/LnskyDB/
功能特點
Lambda表達式查詢方便
基於Dapper的Lambda表達式擴展可以方便的進行查詢篩選操作支持分庫分表
預設支持按年分庫按月分表,也支持自定義分庫分表.從此大數據不用愁T4自動生成實體
有T4模板自動生成實體類,再也不用手寫那些煩人的實體類了.倉儲類及介面也支持自動生成使用門檻低,快速上手
使用非常簡單,可以快速上手
註意問題
本框架只支持單表的Lambda表達式查詢,如果多表需要手寫sql,框架支持根據sql查詢修改等.
另外不太建議連表查詢,推薦在邏輯層處理
開源協議
上一期我們寫了分庫分表的增刪改查,這一期我們寫一下不分庫分表的增刪改查:
不分庫分表
查詢
- 根據主鍵查詢
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
var entity = repository.Get(new ProductSaleByDayNSEntity
{
SysNo = sysNo
});
- 根據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);
- 分頁查詢
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);
更新
- 根據主鍵更新
var updateEntity = new ProductSaleByDayNSEntity()
{
SysNo = Guid.Parse("650BC09C-2B9C-467B-A457-8B4853CC1F0F"),
DataSource = "測試來源修改",
ShopName = "店鋪修改"
};
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
//根據主鍵更新其他欄位
return repository.Update(updateEntity);
- 根據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);
刪除
- 根據主鍵刪除
var deleteEntity = new ProductSaleByDayNSEntity()
{
SysNo = Guid.Parse("650BC09C-2B9C-467B-A457-8B4853CC1F0F")
};
var repository = RepositoryFactory.Create<ProductSaleByDayNSEntity>();
return repository.Delete(deleteEntity);
- 根據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);