DSL 纯粹是多此一举,失去了 SQL 的灵活,又增加了认知负担,得不偿失,SQL builder 或者 ORM 专注自己的领域就好了,do one thing and do it well ,Mybatis 真的就是一个 Mapper 而已,用起来还不错,就是业务逻辑复杂改起来就很啰嗦,Gorm ent 之流也能凑合用,但是都没有 EF ,Active Record ,Eloquent ,还有 node 世界的某些好用。真的感慨 Java 世界竟然没有一个顺手的 ORM ,JPA 太重,不灵活,也可能是我菜,所以干脆连笨重啰嗦的 Java 一并抛弃,拥抱 .NET ,EF 自不必说,甚至连国产的 SqlSugar 都可圈可点,用起来很舒服。
查询语法:
var exp = Expressionable.Create<Supplier, SysUser>();
exp.AndIF(dto.SupName.IsNotEmpty(), (t, u) => t.SupName.Contains(dto.SupName));
exp.AndIF(dto.CreditCode.IsNotEmpty(), (t, u) => t.CreditCode.Contains(dto.CreditCode));
exp.AndIF(dto.Address.IsNotEmpty(), (t, u) => t.Address.Contains(dto.Address));
exp.AndIF(dto.DetailAdmissionType.IsNotEmpty(), (t, u) => t.DetailAdmissionType.Contains(dto.DetailAdmissionType));
exp.AndIF(dto.Leader.IsNotEmpty(), (t, u) => t.Leader.Contains(dto.Leader));
exp.AndIF(dto.AdmissionTerm.HasValue, (t, u) => (int)t.AdmissionTerm == dto.AdmissionTerm);
exp.AndIF(dto.StartNumber.HasValue, (t, u) => t.RegCapitalSubscribed >= dto.StartNumber);
exp.AndIF(dto.EndNumber.HasValue, (t, u) => t.RegCapitalSubscribed <= dto.EndNumber);
exp.AndIF(dto.DeptId.HasValue, (t, u) => u.DeptId == dto.DeptId);
exp.AndIF(dto.AdmissionType.HasValue, (t, u) => t.AdmissionType == dto.AdmissionType);
exp.AndIF(dto.CompanyType.HasValue, (t, u) => t.CompanyType == dto.CompanyType);
exp.AndIF(dto.BeginTime.HasValue, (t, u) => t.CreateTime >= dto.BeginTime);
exp.AndIF(dto.EndTime.HasValue, (t, u) => t.CreateTime <= dto.EndTime);
var result = _supplierService.Queryable()
.InnerJoin<SysUser>((t, u) => t.CreateBy == u.UserId)
.InnerJoin<SysDept>((t, u, d) => u.DeptId == d.DeptId)
.LeftJoin<SupplierInvoice>((t, u, d, si) =>
t.Id == si.SupId)
.LeftJoin<SupplierChange>((t, u, d, si, ch) =>
t.Id == ch.SupId && ch.MostRecent)
.Where(exp.ToExpression())
.OrderByDescending(t =>
t.Id)
.Select((t, u, d, si, ch) => new SupplierVo
{
Id = t.Id.SelectAll(),
DeptName = d.DeptName,
NickName = u.NickName,
InvoiceStatus = si.Status,
ChangeStatus = ch.Status,
})
.ToPage(dto);
int count = DB.Queryable<T>().ClearFilter()
.InnerJoin<SysUser>((table, usr) => table.CreateBy == usr.UserId)
.InnerJoin<SysDept>((table, usr, dept) => usr.DeptId == dept.DeptId)
.Where(table =>
table.Id == Record.TableId)
.WhereIF(transition.Condition.IsNotEmpty(), transition.Condition, parameters)
.Count();
根据指定条件更新指定列:
Update(new SysUser(){ Status = 1 }, it => new { it.Status }, f => f.Userid == 1));
Update(w => w.NoticeId == model.NoticeId, it => new SysNotice(){ Update_time = DateTime.Now, Title = "通知标题" });
另外还有不计其数的 CRUD 重载方法,覆盖了全部使用场景,开发效率很高