LnskyDB
LnskyDB是基于Dapper的Lambda扩展,支持按时间分库分表,也可以自定义分库分表方法
开源地址 https://github.com/liningit/LnskyDB
在此非常感谢SkyChenSky其中lambda表达式的解析参考了他的开源项目
下面是用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 根据主键查询
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条件查询
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