sqlsugar是性能最好的ORM之一,具備超越Dapper的性能 ,走的是EMIT夠構中間語言動態編譯到程序集,完成高性能的實體綁定,達到原生水平。mysql
2.功能很是強大sql
除了EF之外能夠說的是功能最大的ORM框架數據庫
支持 DbFirst、CodeFirst、數據庫維護、鏈式查詢、鏈式更新、鏈式刪除、鏈式插入、實體屬性、複雜模型的查詢、ADO.NET。特別是批量等功能都是貨真價實的並不是循環操做。併發
SqlSugar 4.0版本 6月底支持SqlSever的Core版 ,預計7月份支持多庫,8月分開始分佈式ORM的開發。 (3.x版本已經支持了4種數據庫,相對穩定功能簡單)app
完美的語法,能夠秒殺現有全部ORM框架框架
詳細語法請看孫凱旋博客園 http://www.codeisbug.com/Doc/8分佈式
Newtonsoft.Json:要注意最好添加較高版本的,不然會有兼容性問題高併發
Sqlsugar:這個版本要根據你的.Net Framework的版本選擇你合適的版本,這裏我用的是.Net Framework4.5因此我安裝的是sqlsugar5.0.0.8工具
MySql.Data性能
先貼一段代碼,這個是我封裝的一個操做數據庫的一個類,我採用的是單例模式,不過有個弊端就是不能使用高併發的狀況
public class DBContext<T> where T : class, new() { public SqlSugarClient Db; private static DBContext<T> mSingle = null; public static DBContext<T> GetInstance() { if (mSingle == null) mSingle = new DBContext<T>(); return mSingle; } protected DBContext() { //經過這個能夠直接鏈接數據庫 Db = new SqlSugarClient(new ConnectionConfig() { //能夠在鏈接字符串中設置鏈接池pooling=true;表示開啓鏈接池 //eg:min pool size=2;max poll size=4;表示最小鏈接池爲2,最大鏈接池是4;默認是100 ConnectionString = "database='" + "BookShop" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "1234" + "';charset='utf8';pooling=true", DbType = SqlSugar.DbType.MySql,//我這裏使用的是Mysql數據庫 IsAutoCloseConnection = true,//自動關閉鏈接 InitKeyType = InitKeyType.Attribute }); //調式代碼 用來打印SQL //Db.Aop.OnLogExecuting = (sql, pars) => //{ // Console.WriteLine(sql + "\r\n" + // Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); // Console.WriteLine(); //}; } public void Dispose() { if (Db != null) { Db.Dispose(); } } public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } } /// <summary> /// 獲取全部 /// </summary> /// <returns></returns> public virtual List<T> GetList() { return CurrentDb.GetList(); } /// <summary> /// 根據表達式查詢 /// </summary> /// <returns></returns> public virtual List<T> GetList(Expression<Func<T, bool>> whereExpression) { return CurrentDb.GetList(whereExpression); } /// <summary> /// 根據表達式查詢分頁 /// </summary> /// <returns></returns> public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel pageModel) { return CurrentDb.GetPageList(whereExpression, pageModel); } /// <summary> /// 根據表達式查詢分頁並排序 /// </summary> /// <param name="whereExpression">it</param> /// <param name="pageModel"></param> /// <param name="orderByExpression">it=>it.id或者it=>new{it.id,it.name}</param> /// <param name="orderByType">OrderByType.Desc</param> /// <returns></returns> public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel pageModel, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) { return CurrentDb.GetPageList(whereExpression, pageModel, orderByExpression, orderByType); } /// <summary> /// 根據主鍵查詢 /// </summary> /// <returns></returns> public virtual List<T> GetById(dynamic id) { return CurrentDb.GetById(id); } /// <summary> /// 根據主鍵刪除 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Delete(dynamic id) { if (string.IsNullOrEmpty(id.ObjToString)) { Console.WriteLine(string.Format("要刪除的主鍵id不能爲空值!")); } return CurrentDb.Delete(id); } /// <summary> /// 根據實體刪除 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Delete(T data) { if (data == null) { Console.WriteLine(string.Format("要刪除的實體對象不能爲空值!")); } return CurrentDb.Delete(data); } /// <summary> /// 根據主鍵刪除 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Delete(dynamic[] ids) { if (ids.Count() <= 0) { Console.WriteLine(string.Format("要刪除的主鍵ids不能爲空值!")); } return CurrentDb.AsDeleteable().In(ids).ExecuteCommand() > 0; } /// <summary> /// 根據表達式刪除 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Delete(Expression<Func<T, bool>> whereExpression) { return CurrentDb.Delete(whereExpression); } /// <summary> /// 根據實體更新,實體須要有主鍵 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Update(T obj) { if (obj == null) { Console.WriteLine(string.Format("要更新的實體不能爲空,必須帶上主鍵!")); } return CurrentDb.Update(obj); } /// <summary> ///批量更新 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Update(List<T> objs) { if (objs.Count <= 0) { Console.WriteLine(string.Format("要批量更新的實體不能爲空,必須帶上主鍵!")); } return CurrentDb.UpdateRange(objs); } /// <summary> /// 插入 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Insert(T obj) { return CurrentDb.Insert(obj); } /// <summary> /// 批量 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Insert(List<T> objs) { return CurrentDb.InsertRange(objs); } //能夠擴展更多方法 }
5.還有就是須要有model類,就是跟數據庫中表對應的model類,好比我這裏是book和booktype,附加一段代碼作個參考
[SugarTable("Books")]//指定數據庫中的表名,要對應數據庫的表名,不然會出錯 public class Books { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//指定主鍵和自動增加 public int Id { get; set; } public int BId { get; set; } public string BName { get; set; } public int TypeId { get; set; } }
6.開始操做數據庫了
Books b = new Books() { BId = 2, BName = "西遊記", TypeId = 2 }; BookType bt = new BookType() { TId= 3, TName = "健康"}; if (DBContext<Books>.GetInstance().CurrentDb.Insert(b)) { Console.WriteLine("books_添加成功!"); } if (DBContext<BookType>.GetInstance().Db.Insertable(bt).ExecuteCommand() > 0) { Console.WriteLine("BookType_添加成功!"); }
其餘操做數據庫的例子參考孫凱旋的博客園吧,附連接 http://www.codeisbug.com/Doc/8/1123
例子到這裏就結束了,分享一下,我在作這個過程當中遇到的問題:
1.由於我本來項目中已經存在程序包Newtonsoft.Json,而它的版本較低,當時忽略了版本問題,致使版本不兼容問題。後面升級以後就能夠了。
2.猶豫項目須要高併發處理數據,因此我上邊寫的單例模式其實存在必定的問題,因此作了必定的修改,代碼貼一下
public class DBContext<T> where T : class, new() { public SqlSugarClient Db; /// <summary> /// 修改後的代碼 /// </summary> /// <returns></returns> public static DBContext<T> OpDB() { DBContext<T> dbcontext_t = new DBContext<T>(); dbcontext_t.Db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "database='" + "bookshop" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "1234" + "';charset='utf8';pooling=true", DbType = SqlSugar.DbType.MySql, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); return dbcontext_t; } protected DBContext() { Db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "database='" + "bookshop" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "1234" + "';charset='utf8';pooling=true", DbType = SqlSugar.DbType.MySql, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); //調式代碼 用來打印SQL Db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); Console.WriteLine(); }; } public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } } //能夠擴展更多方法 }
ok,該demo的分享就到這了,若是有什麼錯誤的地方歡迎指出,有不理解的也能夠留言。