全稱 :Json Object Relational Mapping ,它是通過JSON 物件 去實現資料庫的一個關係對映 ,我理想中完整的JORM包含功能有
·1、表權授權
2、欄位級別授權
3、查詢返回備註
4、可以設定化
5、支援豐富的SQL語法
6、資料驗證
7、JSON作為資料庫中間語言支援多種資料庫
我們都知道 ORM用起來非常舒服,都強型別,但是他的缺點很顯
1、對動態操作支援比較弱 , 很多功能都要自個封裝,很難支援到函數級別
2、不能支援列級別的過濾,比如A使用者顯示2列,B使用者顯示3列,像這種很難處理
3、無法動態構造,這就導致了無法很好的用在 工作流、低程式碼平臺或者人工智慧產品中,很多都是需要自個拼SQL,查詢用Datable等
SqlSugar是一款 老牌 .NET 開源ORM框架,由果糖巨量資料科技團隊維護和更新
為提高本公司內部產品的開發效率,準備打造一款低程式碼平臺,所以從2022年1月份已經開始設計和架構 支援JORM運算元據庫
JsonClient jsonToSqlClient = new JsonClient(); jsonToSqlClient.Context = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.MySql, IsAutoCloseConnection = true, ConnectionString = "server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=haosql" });
Json格式:
{ "Table":"order", Select:[ [{SqlFunc_AggregateMin:["id"]},"id"], [{SqlFunc_GetDate:[]},"Date"] ] }
程式碼:
jsonToSqlClient.Queryable(json).ToSql() //Sql //SELECT MIN(`id`) AS `id` , NOW() AS `Date` FROM `Order`
{ "Table":"order", Where:[ "name","=", "{string}:xxx" ], Select:[ [{SqlFunc_AggregateMin:["id"]},"id"], [{SqlFunc_GetDate:[]},"Date"] ] }
程式碼:
jsonToSqlClient.Queryable(json).ToSql() //Sql //SELECT MIN(`id`) AS `id` , NOW() AS `Date` FROM `Order` WHERE `name` = @p0
條件查詢有2種語法
語法1:可以完美支援 SqlSugar表格查詢語法
Where: [{ ""FieldName"":""id"",""ConditionalType"":""0"",""FieldValue"":""1""}]
語法2:逗號拼接方式
Where:["name","=","{string}:a" , "&&" , "id" ,">", "{int}:1"]
運運算元號: = 、>、<、>=、<=、&&、||、(、)
欄位名:字母數位下劃線
引數值: {int}:1 表式型別為int值為1的引數
函數:{SqlFunc_AggregateMin:["id"]} 表式 min(id)
Json格式:
{ "Table":"order", PageNumber:"1", PageSize:"100" }
程式碼:
var sqls=jsonToSqlClient.Queryable(json).ToSqlList() //SELECT COUNT(1) FROM `Order` //SELECT * FROM `Order` LIMIT 0,100
Json格式:
{ "Table": "order" , GroupBy:["name"], Having: [{SqlFunc_AggregateAvg:["id"]},">","{int}:1" ], Select:[ [{SqlFunc_AggregateAvg:["id"]},"id"],"name" ] }
程式碼:
var sql= jsonToSqlClient.Queryable(json).ToSql() //SELECT AVG(`id`) AS `id` , `name` AS `name` FROM `Order` GROUP BY `name` HAVING AVG(`id`) > @p0
Json格式:
{ "Table":[ "order","o"], "LeftJoin01": ["orderdetail", "d", [ "d.orderid",">","o.id" ]], "Select":["o.id" ,["d.itemid","newitemid"]] }
程式碼:
var sql= jsonToSqlClient.Queryable(json).ToSql(); //SELECT `o`.`id` AS `o_id` , `d`.`itemid` AS `newitemid` FROM `Order` o Inner JOIN `orderdetail` d ON `d`.`orderid` > `o`.`id`
該功能目前還不完善,暫時不細解
var tableNames = jsonToSqlClient.GetTableNameList(json);//通過JSON獲取JSON所有表 var configs = GetConfigByUser(tableNames);//通過表獲取行列過濾備註等資訊 var sqlList = jsonToSqlClient .Queryable(json) .UseAuthentication(configs)//查詢啟用行列過濾 .ShowDesciption()//查詢返回備註 .ToResult();
單條插入
{ "Table":"order", Columns:{name:"{string}:1",price:"{decimal}:1"} }
批次持入
{ "Table":"order", Columns:[ {name:"{string}:2",price:"{decimal}:2"} , {name:"{string}:1",price:"{decimal}:1"} ] }
帶自增列
{ "Table":"order", Identity:"id", Columns: {name:"{string}:2",price:"{decimal}:2"} }
單個物件更新
{ "Table":"order", Columns: { id:"{int}:1" ,name:"{string}:1" }, WhereColumns:["id"] }
多個物件更新
{ "Table":"order", Columns:[ {id:2,name:"{string}:2",price:"{decimal}:2"} , {id:1,name:"{string}:1",price:"{decimal}:1"} ], WhereColumns:["id"] }
Sql語句方式更新
{ "Table":"order", Columns: {name:"{string}:2",price:"{decimal}:2"} , Where:["id","=","{int}:11"] }
Where中的用法和查詢一樣的
Json格式
{ "Table":"order", Where:[ "id"," = ","{int}:1" ] }
程式碼:
jsonToSqlClient.Deleteable(json).ToSqlList() //DELETE FROM `order` WHERE `id` = @p0
SqlSugar未來將重點開發JORM ,喜歡低程式碼平臺或者工作流的朋友,可以關注一下, 目前還是開發階段 ,預計在未來3個月內投入生產
https://github.com/donet5/SqlSugar 群號:995692596
.很多人問有了EF CORE為什麼還有其它ORM ,這個問題就等於淘寶只有天貓一樣,只有豐富的產品才會形成良性競爭,讓使用者有更多選擇