0. 前言
繼上一篇,以及上上篇,我們對SqlSugar有了一個大概的認識,但是這并不完美,因為那些都是理論知識,無法描述我們工程開發中實際情況,而這一篇,將帶領小伙伴們一起試著寫一個能在工程中使用的模板類,
1. 創建一個Client
SqlSugar在操作的時候需要一個Client,用來管理資料庫連接,并操作資料庫,所以我們寫一個DbContext用來創建Client:
public class DefaultContext
{
public SqlSugarClient Client { get; }
public DefaultContext(string connectionString, DbType dbType)
{
Client = new SqlSugarClient(new ConnectionConfig
{
ConnectionString = connectionString,//"Data Source=./demo.db",
DbType = dbType,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
Client.CodeFirst.InitTables(typeof(Dept), typeof(Person), typeof(Employee));
Client.Aop.OnLogExecuting = (sql, paramters) =>
{
Console.WriteLine(sql);
};
}
public SimpleClient<T> CreateClient<T>() where T : class, new()
{
return Client.GetSimpleClient<T>();
}
}
SqlSugar 提供了一個SimpleClient,這里面有很多可以直接拿來用的方法,而且這個是一個泛型類,也就是說我們可以使用它對單個物體類進行操作,這在開發中很重要,
2. 插入資料
對于一個程式而言,資料就像是血液一樣重要,對于ORM框架,插入是一切來源的基礎,所以我們先來看看SqlSugar的插入是怎樣的吧:
2.1 簡單的插入模式
public bool Insert(T insertObj);
public bool InsertRange(T[] insertObjs);
public bool InsertRange(List<T> insertObjs);
這是SqlSugar在SimpleClient里提供的兩個默認插入方法,一個是插入單個物體物件,一個是插入一組物件,
默認情況下,SqlSugar插入并不會將主鍵回傳給資料,如果后續操作需要當前資料的主鍵,則可以呼叫另外一個方法:
public int InsertReturnIdentity(T insertObj);
通過這個方法可以獲取一個默認的int型別主鍵值,
2.2 高級玩法
SqlSugar還有一種插入模式,通過AsInsertable回傳一個 IInsertable泛型介面:
public IInsertable<T> AsInsertable(T insertObj);
public IInsertable<T> AsInsertable(T[] insertObjs);
public IInsertable<T> AsInsertable(List<T> insertObjs);
這種模式與SimpleClient的普通插入模式不同,它并不會直接執行插入動作,需要手動呼叫并執行插入動作:
int ExecuteCommand();
執行動作,然后回傳受影響的行數,
bool ExecuteCommandIdentityIntoEntity();
執行動作,然后將主鍵插入物體物件,回傳插入結果,執行完成后,主鍵資料保存到物體示例中,
long ExecuteReturnBigIdentity();
int ExecuteReturnIdentity();
執行動作,然后回傳主鍵值,不會更新物體,
有一點值得特別注意:
所有會回傳主鍵的插入都只針對單個資料,如果一次插入多個資料,并不會回傳主鍵資訊也無法將主鍵資訊更新入物體中,
以上都是全列插入,SqlSugar還提供了只插入部分列和忽略某些列兩種模式:
IInsertable<T> InsertColumns(Expression<Func<T, object>> columns);// 滿足條件的插入,其他列則不插入
IInsertable<T> InsertColumns(params string[] columns);//插入指定列名
IInsertable<T> IgnoreColumns(Expression<Func<T, object>> columns);// 忽略滿足條件的列,插入其他列
IInsertable<T> IgnoreColumns(params string[] columns);// 忽略這幾個列
IInsertable<T> IgnoreColumns(bool ignoreNullColumn, bool isOffIdentity = false);//指定是否忽略Null列,并是否強制插入主鍵
3. 更新或插入
介紹完插入,那么來介紹一下更新,正所謂,沒有更新資料就是一灘死水,有了更新資料才有了變化,所以,就讓我們來看看如何優雅的更新資料吧:
3.1 簡單模式
先來兩個最簡單的:
public bool Update(T updateObj);
public bool UpdateRange(T[] updateObjs);
public bool UpdateRange(List<T> updateObjs);
傳入物體,直接更新到資料庫中,需要注意的是這種更新模式只需要保證主鍵有值,且與之對應即可,
public bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression);
這是另一種條件更新,會更新滿足whereExpression的所有元素,更新示例:
personClient.Update(p=>new Person
{
Age = 1
}, p=>p.Id == 1);
columns需要回傳一個要更新的物件的屬性列,也就是在columns中設定需要更新的內容,
3.2 高級模式
同樣,通過AsUpdateable開啟高級模式:
public IUpdateable<T> AsUpdateable(T[] updateObjs);
public IUpdateable<T> AsUpdateable(T updateObj);
public IUpdateable<T> AsUpdateable(List<T> updateObjs);
然后可以針對這些今天更多的操作:
int ExecuteCommand();
回傳命令執行影響的行數
bool ExecuteCommandHasChange();
回傳是否有變化,也就是影響行數是否大于0,
- 只更新某些列:
IUpdateable<T> SetColumns(Expression<Func<T, bool>> columns);
更新示例:
personClient.AsUpdateable(d).SetColumns(t=>t.Age ==2).ExecuteCommand();
傳入一個lambda運算式,使資料滿足lambda運算式,要求lambda運算式只能用 == 來判斷列是否等于某個值,
IUpdateable<T> SetColumns(Expression<Func<T, T>> columns);
IUpdateable<T> UpdateColumns(params string[] columns);
IUpdateable<T> UpdateColumns(Expression<Func<T, object>> columns);
傳入要更新的實際列名,其中 object 用來接一個匿名物件,其中屬性名字就是要更新的值,
- 不更新某些列
IUpdateable<T> IgnoreColumns(params string[] columns);// 忽略傳入的列名
IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns);// 用匿名物件表示要忽略的列名
IUpdateable<T> IgnoreColumns(bool ignoreAllNullColumns, bool isOffIdentity = false, bool ignoreAllDefaultValue = https://www.cnblogs.com/c7jie/p/false);// 設定是否忽略Null列,是否強制更新主鍵,是否忽略所有默認值列
- 條件更新
IUpdateable<T> Where(Expression<Func<T, bool>> expression);
IUpdateable<T> Where(string fieldName, string conditionalType, object fieldValue);
IUpdateable<T> Where(string whereSql, object parameters = null);
IUpdateable<T> WhereColumns(Expression<Func<T, object>> columns);
IUpdateable<T> WhereColumns(string columnName);
IUpdateable<T> WhereColumns(string[] columnNames);
來,簡單猜一猜這幾個是什么意思呢?
可以說很簡單明了的幾種條件設定模式,lambda表示篩選更新資料,欄位值判斷條件更新,
其中 conditionType的值,推薦使用 ConditionalType列舉的值,
3.3 更新或插入
在實際開發中可能會遇到插入或更新是走的一個方法,所以我們就要尋找一個可以直接更新或插入的方法,SqlSugar為此提供了解決方案:
ISaveable<T> Saveable<T>(T saveObject) where T : class, new();
ISaveable<T> Saveable<T>(List<T> saveObjects) where T : class, new();
不過這個方法是在SugarClient里,我們可以通過:
public ISqlSugarClient AsSugarClient();
在SimpleClient中獲得 與之關聯的SugarClient物件,
關于更新或插入判斷標準是,主鍵是否有值,如果主鍵有值且在資料庫中存在該條記錄,則執行更新,否則執行插入,
4. 洗掉
洗掉在實際開發程序中是一個非常重要的功能點,所以如何快速有效的洗掉資料也是一件很重要的事,那么,就來看看如何執行洗掉吧:
public bool Delete(Expression<Func<T, bool>> whereExpression);
public bool Delete(T deleteObj);
public bool DeleteById([Dynamic] dynamic id);
public bool DeleteByIds([Dynamic(new[] { false, true })] dynamic[] ids);
洗掉沒有其他需要注意的地方,第一個是條件洗掉,所有滿足條件的都要洗掉,第二個洗掉單個物件,后面兩個根據主鍵洗掉物件,
悄悄吐槽一下,主鍵的地方用object會比較好一點,因為動態物件會增加一次裝箱拆箱的程序,
當然了,洗掉也有AsDeleteable方法,IDeleteable介面特別提供了根據sql陳述句洗掉的方法,除此之外沒有別的需要注意的地方了,
5. 查詢
一個好的ORM框架,至少五分功力在查詢上,如何更快更準的查詢成為了現在開發對ORM框架的要求,同時簡單易用更是程式員對ORM的期望,
那么我們來看看SqlSugar在查詢上的功力吧:
public bool IsAny(Expression<Func<T, bool>> whereExpression);// 查詢是否存在符合條件的資料
public int Count(Expression<Func<T, bool>> whereExpression);// 獲取滿足條件的數量
public T GetById([Dynamic] dynamic id);//根據主鍵獲取一個實體
public bool IsAny(Expression<Func<T, bool>> whereExpression);//回傳滿足條件的一個物件
public List<T> GetList();// 以List的形式回傳所有資料
public List<T> GetList(Expression<Func<T, bool>> whereExpression);//回傳符合條件的所有資料
分頁獲取資料:
public List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page);
public List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc);
public List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page);
public List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc);
其中IConditionModel是一個空的介面,用來定義規范查詢規范,實際上使用的是類:
public class ConditionalModel: IConditionalModel
{
public ConditionalModel()
{
this.ConditionalType = ConditionalType.Equal;
}
public string FieldName { get; set; }
public string FieldValue { get; set; }
public ConditionalType ConditionalType { get; set; }
public Func<string,object> FieldValueConvertFunc { get; set; }
}
那么,我們看一下 ConditionType,定義了各種判斷依據:
public enum ConditionalType
{
Equal=0,
Like=1,
GreaterThan =2,
GreaterThanOrEqual = 3,
LessThan=4,
LessThanOrEqual = 5,
In=6,
NotIn=7,
LikeLeft=8,
LikeRight=9,
NoEqual=10,
IsNullOrEmpty=11,
IsNot=12,
NoLike = 13,
}
那么我們簡單看一下 使用IConditionModel進行分頁是怎樣的效果:
var list = personClient.GetPageList(new List<IConditionalModel>
{
new ConditionalModel
{
FieldName = "Age",
FieldValue = https://www.cnblogs.com/c7jie/p/"3",
ConditionalType = ConditionalType.LessThan
}
}, pageModel);
生成如下SQL陳述句:
SELECT COUNT(1) FROM (SELECT `Id`,`Name`,`Age` FROM `Person` WHERE Age < @ConditionalAge0 ) CountTable
SELECT `Id`,`Name`,`Age` FROM `Person` WHERE Age < @ConditionalAge0 LIMIT 0,2
可以看出兩者并沒有區別,只不過是不同的查詢習慣,
6. 總結
按照之前的習慣,到目前應該可以結束了,但是SqlSugar還有一些很重要的地方沒有介紹,所以就加個下期預告
下一篇將為大家分析SqlSugar的一些更高級的內容,查詢的高級模式、事務以及批量操作
好,總結一下這一篇,我們在這一篇看到了SqlSugar在增刪改查上的亮點,可以說更貼合實際業務需求開發,嗯,悄悄給個贊,
再有三篇的內容《C# 資料操作系列》就要完結了,從下一系列開始,就要步入作業中最重要的技術堆疊了:Asp.net Core,這是可以寫入簡歷的,嗯,沒錯,下一系列計劃以實戰的形式介紹asp.net core的知識點和設定,
更多內容煩請關注我的博客《高先生小屋》

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/30358.html
標籤:C#
上一篇:C#語法糖——持續更新
