liningit
Published on 2017-09-02 11:31 in 暂未分类 with liningit

基于Dapper的开源Lambda扩展,且支持分库分表自动生成实体二

LnskyDB GitHub stars GitHub forks

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查询修改等.
另外不太建议连表查询,推荐在逻辑层处理

开源协议

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);
posted @ 2019-06-18 18:02 liningit 阅读(...) 评论(...) 编辑 收藏