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

LnskyDB

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

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

在此很是感謝SkyChenSky其中lambda表達式的解析參考了他的開源項目sql

下面是用ProductSaleByDayEntity做爲示例,其中StatisticalDate爲分庫分表字段,若是是對分庫分表對象進行數據庫操做則必須傳入StatisticalDate或者設置DBModel_ShuffledTempDate指定是那個庫和表數據庫


1. 倉儲的建立

倉儲的建立有兩種方式一種是經過RepositoryFactory.Create<ProductSaleByDayEntity>()建立IRepository<ProductSaleByDayEntity> 還有一種是建立一個倉儲類繼承Repository<ProductSaleByDayEntity>多線程

public interface IProductSaleByDayRepository : IRepository<ProductSaleByDayEntity>
{
}
public class ProductSaleByDayRepository : Repository<ProductSaleByDayEntity>
{
}
//調用的地方能夠
IProductSaleByDayRepository repository=new ProductSaleByDayRepository();

2. 查詢

2.1 根據主鍵查詢mvc

var repository = RepositoryFactory.Create<ProductSaleByDayEntity>();
var entity = repository.Get(new ProductSaleByDayEntity
{
    DBModel_ShuffledTempDate = new DateTime(2019, 01, 01),//這兒表示差19年1月的庫和表
    SysNo = sysNo
});

2.2 根據where條件查詢app

var stTime = new DateTime(2019, 1, 15);
var endTime = new DateTime(2019, 2, 11);
var repository = 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 = repository.GetList(query, stTime, endTime);

若是能夠肯定統計時間也能夠查指定的庫表進行單表查詢測試

var stTime = new DateTime(2019, 1, 15);
var endTime = new DateTime(2019, 1, 18);
var repository = 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= repository.GetList(query);

2.3 分頁查詢ui

var stTime = new DateTime(2019, 1, 15);
var endTime = new DateTime(2019, 2, 11);
var repository = 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 = repository.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 repository = 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= repository.GetPaging(query);
var count = paging.TotalCount;
var lst = paging.ToList();//或者paging.Items

3. 添加

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 repository = RepositoryFactory.Create<ProductSaleByDayEntity>();
//若是新增主鍵是自增列會自動賦值自增列值到主鍵
repository.Add(addEntity);

4. 更新

4.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 repository = RepositoryFactory.Create<ProductSaleByDayEntity>();
//根據主鍵更新其餘字段
return repository.Update(updateEntity);

4.2 根據where條件更新

var updateEntity = new ProductSaleByDayEntity()
{
    DataSource = "測試來源修改",
    ShopName = "店鋪修改Where",
    DBModel_ShuffledTempDate = new DateTime(2019, 01, 05),//若是用這句話來肯定是那個庫表
    // StatisticalDate = statisticalDate,//若是要更新StatisticalDate則能夠用這句話替代上面那句話
};
var repository = RepositoryFactory.Create<ProductSaleByDayEntity>();
var where = QueryFactory.Create<ProductSaleByDayEntity>(m => m.ShopName == "測試店鋪1" && m.StatisticalDate > new DateTime(2019, 01, 03));//where是更新條件
//注意若是是更新用的是實體類的DBModel_ShuffledTempDate Query中的無效
return repository.Update(updateEntity, where);

5. 刪除

5.1 根據主鍵刪除

var deleteEntity = new ProductSaleByDayEntity()
{
    SysNo = Guid.Parse("650BC09C-2B9C-467B-A457-8B4853CC1F0F"),
    DBModel_ShuffledTempDate = new DateTime(2019, 01, 05),//對於分庫分表來講DBModel_ShuffledTempDate是必須的用來確認是那個庫表
};
var repository = RepositoryFactory.Create<ProductSaleByDayEntity>();
return repository.Delete(deleteEntity);

5.2 根據where條件刪除

var repository = 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 repository.Delete(where);

6. 多線程處理

對於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);//調用

7. 實體類T4自動生成

LnskyDB.Demo\T4中有能夠自動生成實體類的T4模版. 其中DbHelper.ttinclude中的Config是配置數據庫的 Entity.tt是生成實體的T4模版.你們能夠根據本身的狀況修改

相關文章
相關標籤/搜索