.net數(shù)據(jù)庫操作框架SqlSugar的簡單入門
介紹
SqlSugar是一款 老牌 .NET數(shù)據(jù)庫操作框架,由果糖大數(shù)據(jù)科技團(tuán)隊(duì)維護(hù)和更新 ,Github star數(shù)僅次于EF 和 Dapper
優(yōu)點(diǎn): 簡單易用、功能齊全、高性能、輕量級、服務(wù)齊全、有專業(yè)技術(shù)支持一天18小時(shí)服務(wù)
支持?jǐn)?shù)據(jù)庫:MySql、SqlServer、Sqlite、Oracle 、 postgresql、達(dá)夢、人大金倉
框架新功能
最新穩(wěn)定版本5.0.2.8 ,發(fā)布后1個(gè)月時(shí)間NUGET下載量達(dá)到5000的版本,用戶使用也相當(dāng)滿意
而在穩(wěn)定版本的基礎(chǔ)上又布了5.0.2.9版本
加入3大新功能
1. 配置查詢
解決了大量字典表和簡單就為取一個(gè)name 就要寫聯(lián)表的問題,讓單表查詢解決一切
2.多租戶+倉儲+自動分配
3.行轉(zhuǎn)列
1、配置查詢
解決了大量字典表和簡單就為取一個(gè)name 就要寫聯(lián)表的問題,讓單表查詢解決一切
字典表我相信大家都全用到,他們可以方便的存儲性別、學(xué)歷、崗位等 一串?dāng)?shù)據(jù) 并進(jìn)行TypeId進(jìn)行區(qū)分
1.1 創(chuàng)建測試數(shù)據(jù)
創(chuàng)建一個(gè)字典實(shí)體
public class DataDictionary { public string Code { get; set; } public string Name { get; set; } public string Type { get; set; } }
創(chuàng)建字典表并向里面插入測試數(shù)據(jù)
var db = GetInstance(); List<DataDictionary> datas = new List<DataDictionary>(); datas.Add(new DataDictionary() { Code="1", Name="男",Type="sex" }); datas.Add(new DataDictionary() { Code = "2", Name = "女", Type = "sex" }); datas.Add(new DataDictionary() { Code = "1", Name = "南通市", Type = "city" }); datas.Add(new DataDictionary() { Code = "2", Name = "蘇州市", Type = "city" }); datas.Add(new DataDictionary() { Code = "1", Name = "江蘇省", Type = "province" }); datas.Add(new DataDictionary() { Code = "2", Name = "湖南省", Type = "province" }); db.CodeFirst.InitTables<DataDictionary>();//這樣就能根據(jù)實(shí)體建表了 db.Insertable(datas).ExecuteCommand();//這樣就能把數(shù)據(jù)插進(jìn)數(shù)據(jù)庫了<br>
在建一個(gè)Person表
public class Person { //數(shù)據(jù)庫字段 [SqlSugar.SugarColumn(IsPrimaryKey =true,IsIdentity =true)] public int Id { get; set; } public string Name { get; set; } public int SexId { get; set; } public int CityId { get; set; } public int ProvinceId { get; set; } //非數(shù)據(jù)庫字段 [SqlSugar.SugarColumn(IsIgnore =true)] public string SexName { get; set; } [SqlSugar.SugarColumn(IsIgnore = true)] public string CityName { get; set; } [SqlSugar.SugarColumn(IsIgnore = true)] public string ProviceName { get; set; } }
1.2 傳統(tǒng)字典聯(lián)表實(shí)現(xiàn)缺點(diǎn)
如果我們要將Person中的非數(shù)據(jù)字典查詢出來那么我們就需要寫有2種實(shí)現(xiàn)方式
1.連表或者子查詢 (缺點(diǎn) 寫起來很浪費(fèi)時(shí)間)
2.將字典存到內(nèi)存,通過內(nèi)存賦值 (缺點(diǎn) 字典表超過1000條以上性能很差 ,并且不能排序,或者LIKE)
下面介紹通過SqlSugar的配置查詢解決上2面?zhèn)€難題
1.3 配置表簡化字典聯(lián)表
配置字典表
if (!db.ConfigQuery.Any()) { var types= db.Queryable<DataDictionary>().Select(it => it.Type).Distinct().ToList(); foreach (var type in types) { db.ConfigQuery.SetTable<DataDictionary>(it => it.Code, it => it.Name, type, it => it.Type == type); } //如果其中Code都是唯一值可以按 1.4中的用法使用循環(huán)都不要 }
配置完我們查詢就會很方便了
var res=db.Queryable<Person>().Select(it => new Person() { Id=it.Id.SelectAll(), SexName=it.SexId.GetConfigValue<DataDictionary>("sex"), ProvinceName = it.ProvinceId.GetConfigValue<DataDictionary>("province"), CityName = it.CityId.GetConfigValue<DataDictionary>("city"), }).ToList(); //也支持支持寫在Where或者Orderby
1.4 簡單聯(lián)表查詢也可以配置
db.ConfigQuery.SetTable<Order>(it => it.Id, it => it.Name);//配置Order<br> var list3 = db.Queryable<OrderItem>().Select(it => new OrderItem { ItemId = it.ItemId.SelectAll(), OrderName = it.OrderId.GetConfigValue<Order>() //查詢的時(shí)候直接用 }).ToList();
總結(jié):配置表查詢的方式可以大大降低重復(fù)聯(lián)表問題,并且配置好后基本就不要寫JOIN了
2、多租戶+倉儲+自動分配
SqlSugar多租戶是通過ConfigId進(jìn)行識別連接哪個(gè)庫,新版本添加了實(shí)體配置ConfigId
[TenantAttribute("1")] public class C1Table { public string Id { get; set; } } [TenantAttribute("2")] public class C2Table { public string Id { get; set; } }
下面我們倉儲就可以通過實(shí)體配置自動識別是連接哪個(gè)庫
public class Repository<T> : SimpleClient<T> where T : class, new() { public Repository(ISqlSugarClient context = null) : base(context)//注意這里要有默認(rèn)值等于null { if (context == null) { var db = new SqlSugarClient(new List<ConnectionConfig> { new ConnectionConfig() { ConfigId="1", DbType = SqlSugar.DbType.SqlServer, IsAutoCloseConnection = true, ConnectionString = Config.ConnectionString }, new ConnectionConfig() { ConfigId="2", DbType = SqlSugar.DbType.SqlServer, IsAutoCloseConnection = true, ConnectionString = Config.ConnectionString2 } }); base.Context = db; var configId = typeof(T).GetCustomAttribute<TenantAttribute>().configId; db.ChangeDatabase(configId); } } /// <summary> /// 擴(kuò)展方法,自帶方法不能滿足的時(shí)候可以添加新方法 /// </summary> /// <returns></returns> public List<T> CommQuery(string sql) { //base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做復(fù)雜操作 return base.Context.Queryable<T>().Where(sql).ToList(); } }
新版本還添加了切換倉儲功能
public class C1Service : Repository<C1Table> { public void Test() { base.AsTenant().BeginTran(); base.GetList(); //調(diào)用內(nèi)部倉儲方法 base.ChangeRepository<Repository<C2Table>>().GetList();//調(diào)用外部倉儲 base.AsTenant().CommitTran(); } }
3、行列互轉(zhuǎn)功能
第一個(gè)參數(shù) 列名、第二個(gè)參數(shù) 頭行名、第三個(gè)參數(shù) 值
var test06 = db.Queryable<Order>() .ToPivotTable(it => it.Id, it => it.Name, it => it.Sum(x => x.Price));//返回Datatable var test07 = db.Queryable<Order>() .ToPivotList(it => it.Id, it => it.Name, it => it.Sum(x => x.Price));//返回List<dynamic><br> var test08 = db.Queryable<Order>() .ToPivotJson(it => it.Id, it => it.Name, it => it.Sum(x => x.Price));//返回Json
官網(wǎng)地址: https://www.donet5.com/Home/Doc
以上就是.net數(shù)據(jù)庫操作框架SqlSugar的簡單入門的詳細(xì)內(nèi)容,更多關(guān)于.net數(shù)據(jù)庫操作框架SqlSugar的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于EF的Code?First的使用以及踩坑記錄
這篇文章主要介紹了關(guān)于EF的Code?First的使用以及踩坑記錄,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11asp.net繼承IHttpHandler接口實(shí)現(xiàn)給網(wǎng)站圖片添加水印功能實(shí)例
這篇文章主要介紹了asp.net繼承IHttpHandler接口實(shí)現(xiàn)給網(wǎng)站圖片添加水印功能,實(shí)例分析了asp.net基于IHttpHandler接口實(shí)現(xiàn)網(wǎng)站圖片水印功能的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07Asp.net管理信息系統(tǒng)中數(shù)據(jù)統(tǒng)計(jì)功能的實(shí)現(xiàn)方法
這篇文章主要介紹了Asp.net管理信息系統(tǒng)中數(shù)據(jù)統(tǒng)計(jì)功能的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-07-07使用ASP.NET模板生成HTML靜態(tài)頁面的五種方案
使用ASP.NET模版生成HTML靜態(tài)頁面并不是難事,主要是使各個(gè)靜態(tài)頁面間的關(guān)聯(lián)和鏈接如何保持完整。本文介紹了使用ASP.NET模版生成HTML靜態(tài)頁面的五種方案2011-11-11.NET?8新預(yù)覽版使用?Blazor?組件進(jìn)行服務(wù)器端呈現(xiàn)(項(xiàng)目體驗(yàn))
這篇文章主要介紹了.NET?8新預(yù)覽版使用?Blazor?組件進(jìn)行服務(wù)器端呈現(xiàn)(項(xiàng)目體驗(yàn)),這是 Blazor 統(tǒng)一工作的開始,旨在使 Blazor 組件能夠滿足客戶端和服務(wù)器端的所有 Web UI 需求,需要的朋友可以參考下2023-04-04ASP.NET、SharePoint中另存文件的長文件名被截?cái)嗟脑蚣敖鉀Q辦法
這個(gè)問題起初發(fā)生在SharePoint的環(huán)境中,我以為是SharePoint限制了長度,后來我試驗(yàn)了一下,在ASP.NET的應(yīng)用中也同樣會發(fā)生。2009-11-11asp.net DataTable相關(guān)操作集錦(篩選,取前N條數(shù)據(jù),去重復(fù)行,獲取指定列數(shù)據(jù)等)
這篇文章主要介紹了asp.net DataTable相關(guān)操作,包括篩選,取前N條數(shù)據(jù),去重復(fù)行,獲取指定列數(shù)據(jù)等.基本涵蓋了DataTable的常見操作技巧,需要的朋友可以參考下2016-06-06在 ASP.Net Core 中使用 MiniProfiler的方法
這篇文章主要介紹了在 ASP.Net Core 中使用 MiniProfiler的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03