開源地址 https://github.com/liningit/LnskyDB
LnskyDB
LnskyDB是基於Dapper的linq擴充套件支援按時間分庫分表,也可以自定義分庫分表方法
下面是用ProductSaleByDayEntity作為示例,其中StatisticalDate為分庫分表欄位,如果是對分庫分表物件進行資料庫操作則必須傳入StatisticalDate或者設定DBModel_ShuffledTempDate指定是那個庫和表
1. 查詢
1.1 根據主鍵查詢
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
var entity = repositoryFactory.Get(new ProductSaleByDayEntity
{
DBModel_ShuffledTempDate = new DateTime(2019, 01, 01),//這兒表示差19年1月的庫和表
SysNo = sysNo
});
1.2 根據where條件查詢
var stTime = new DateTime(2019, 1, 15);
var endTime = new DateTime(2019, 2, 11);
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
var query = QueryFactory.Create<ProductSaleByDayEntity>(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;
//分庫的傳入stTime,endTime會自動根據時間查詢符合條件的庫和表
var lst = repositoryFactory.GetList(query, stTime, endTime);
如果可以確定統計時間也可以查指定的庫表進行單表查詢
var stTime = new DateTime(2019, 1, 15);
var endTime = new DateTime(2019, 1, 18);
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
var query = QueryFactory.Create<ProductSaleByDayEntity>(m => m.ShopName.Contains("測試"));
query.And(m => m.StatisticalDate >= stTime);
query.And(m => m.StatisticalDate < endTime.Date.AddDays(1));
query.DBModel.DBModel_ShuffledTempDate = new DateTime(2019, 01, 01);//這兒表示查19年1月的庫和表
query.OrderByDescing(m => m.StatisticalDate);//單表查詢可以隨意排序
query.StarSize = 20;
query.Rows = 10;
var lst= repositoryFactory.GetList(query);
1.3 分頁查詢
var stTime = new DateTime(2019, 1, 15);
var endTime = new DateTime(2019, 2, 11);
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
var query = QueryFactory.Create<ProductSaleByDayEntity>(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;
//分庫的傳入stTime,endTime會自動根據時間查詢符合條件的庫和表
var paging = repositoryFactory.GetPaging(query, stTime, endTime);
var count = paging.TotalCount;
var lst = paging.ToList();//或者paging.Items
如果可以確定統計時間也可以查指定的庫表
var stTime = new DateTime(2019, 1, 15);
var endTime = new DateTime(2019, 1, 18);
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
var query = QueryFactory.Create<ProductSaleByDayEntity>(m => m.ShopName.Contains("測試"));
query.And(m => m.StatisticalDate >= stTime);
query.And(m => m.StatisticalDate < endTime.Date.AddDays(1));
query.DBModel.DBModel_ShuffledTempDate = new DateTime(2019, 01, 01);//這兒表示查19年1月的庫和表
query.OrderByDescing(m => m.StatisticalDate);//單表查詢可以隨意排序
query.StarSize = 20;
query.Rows = 10;
var paging= repositoryFactory.GetPaging(query);
var count = paging.TotalCount;
var lst = paging.ToList();//或者paging.Items
2. 新增
var addEntity = new ProductSaleByDayEntity()
{
SysNo = Guid.NewGuid(),
DataSource = "測試來源",
ProductID = Guid.NewGuid(),
ShopID = Guid.NewGuid(),
ShopName = "測試店鋪",
ProductName = "測試商品",
OutProductID = Guid.NewGuid().ToString(),
ImportGroupId = Guid.NewGuid(),
StatisticalDate = DateTime.Now//分庫分表欄位是必須的
};
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
//如果新增主鍵是自增列會自動賦值自增列值到主鍵
repositoryFactory.Add(addEntity);
3. 更新
3.1 根據主鍵更新
var updateEntity = new ProductSaleByDayEntity()
{
SysNo = Guid.Parse("650BC09C-2B9C-467B-A457-8B4853CC1F0F"),
DataSource = "測試來源修改",
ShopName = "店鋪修改",
StatisticalDate = new DateTime(2019,01,05),//如果StatisticalDate賦值了則根據StatisticalDate找庫表,然後根據主鍵更新,StatisticalDate也會被更新成所賦的值
//如果不想更新StatisticalDate可以用下面這句話
// DBModel_ShuffledTempDate=new DateTime(2019,01,05),//如果不想更新StatisticalDate欄位則用這句話來確定是那個庫及表
};
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
//根據主鍵更新其他欄位
return repositoryFactory.Update(updateEntity);
3.2 根據where條件更新
var updateEntity = new ProductSaleByDayEntity()
{
DataSource = "測試來源修改",
ShopName = "店鋪修改Where",
DBModel_ShuffledTempDate = new DateTime(2019, 01, 05),//如果用這句話來確定是那個庫表
// StatisticalDate = statisticalDate,//如果要更新StatisticalDate則可以用這句話替代上面那句話
};
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
var where = QueryFactory.Create<ProductSaleByDayEntity>(m => m.ShopName == "測試店鋪1" && m.StatisticalDate > new DateTime(2019, 01, 03));//where是更新條件
//注意如果是更新用的是實體類的DBModel_ShuffledTempDate Query中的無效
return repositoryFactory.Update(updateEntity, where);
4. 刪除
3.1 根據主鍵刪除
var deleteEntity = new ProductSaleByDayEntity()
{
SysNo = Guid.Parse("650BC09C-2B9C-467B-A457-8B4853CC1F0F"),
DBModel_ShuffledTempDate = new DateTime(2019, 01, 05),//對於分庫分表來說DBModel_ShuffledTempDate是必須的用來確認是那個庫表
};
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
return repositoryFactory.Delete(deleteEntity);
3.1 根據where條件刪除
var repositoryFactory = RepositoryFactory.Create<ProductSaleByDayEntity>();
var where = QueryFactory.Create<ProductSaleByDayEntity>();
where.DBModel.DBModel_ShuffledTempDate = new DateTime(2019, 01, 01);
//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 repositoryFactory.Delete(where);
5. 多執行緒處理
對於mvc每次請求都會在請求結束時將資料庫連線關閉,如果是新建執行緒則需要線上程開始呼叫DBTool.BeginThread();
並且線上程結束為止呼叫DBTool.CloseConnections();
關閉連線
public class ThreadTool
{
public static void QueueUserWorkItem(Action action)
{
ThreadPool.QueueUserWorkItem(delegate
{
DBTool.BeginThread();
try
{
action();
}
finally
{
DBTool.CloseConnections();
}
});
}
}
ThreadTool.QueueUserWorkItem(ThreadDo);//呼叫
6. 實體類T4自動生成
在LnskyDB.Demo\T4中有可以自動生成實體類的T4模版.
其中DbHelper.ttinclude中的Config是配置資料庫的
Entity.tt是生成實體的T4模版.大家可以根據自己的情況修改