apijson簡單使用
介紹
APIJSON 是一種專為 API 而生的 JSON 網路傳輸協議 以及 基於這套協議實現的 ORM 庫。為簡單的增刪改查、複雜的查詢、簡單的事務操作 提供了完全自動化的萬能 API。能大幅降低開發和溝通成本,簡化開發流程,縮短開發週期。適合中小型前後端分離的專案,尤其是 BaaS、Serverless、網際網路創業專案和企業自用專案。
Gitee:https://gitee.com/Tencent/APIJSON
示例
Java端:https://gitee.com/greyzeng/apijson-sample
執行
準備資料庫
DemoSQLConfig.java這個檔案中提供了資料庫的配置資訊
需要配置:
- 資料庫的Schema
- 資料庫Version
- 資料庫連線的URI
- 資料庫的使用者名稱密碼
將/sql目錄下的指令碼檔案匯入資料庫中。
增加依賴
將/libs目錄下的jar包增加到專案的classpath中
啟動專案
執行DemoApplication
測試
在Postman中新增一個POST請求,請求的URL是:
請求的Body是:
{
"Moment": {
"id": 12
}
}
返回的結果是:
{
"Moment": {
"id": 12,
"userId": 70793,
"date": "2017-02-08 16:06:11.0",
"content": "APIJSON,let interfaces and documents go to hell !",
"praiseUserIdList": [
70793,
93793,
82044,
82040,
82055,
90814,
38710,
82002,
82006,
1508072105320,
82001
],
"pictureList": [
"http://static.oschina.net/uploads/img/201604/22172508_eGDi.jpg",
"http://static.oschina.net/uploads/img/201604/22172507_rrZ5.jpg",
"https://camo.githubusercontent.com/788c0a7e11a4f5aadef3c886f028c79b4808613a/687474703a2f2f696d61676573323031352e636e626c6f67732e636f6d2f626c6f672f3636303036372f3230313630342f3636303036372d32303136303431343232343932353935372d313732303737333630382e6a7067",
"http://static.oschina.net/uploads/img/201604/22172507_Pz9Y.png",
"https://camo.githubusercontent.com/c98b1c86af136745cc4626c6ece830f76de9ee83/687474703a2f2f696d61676573323031352e636e626c6f67732e636f6d2f626c6f672f3636303036372f3230313630342f3636303036372d32303136303431343232343930383036362d313837323233393236352e6a7067",
"https://camo.githubusercontent.com/f513fa631bd780dc0ec3cf2663777e356dc3664f/687474703a2f2f696d61676573323031352e636e626c6f67732e636f6d2f626c6f672f3636303036372f3230313630342f3636303036372d32303136303431343232343733323232332d3337333933303233322e6a7067",
"https://camo.githubusercontent.com/c98b1c86af136745cc4626c6ece830f76de9ee83/687474703a2f2f696d61676573323031352e636e626c6f67732e636f6d2f626c6f672f3636303036372f3230313630342f3636303036372d32303136303431343232343930383036362d313837323233393236352e6a7067",
"https://camo.githubusercontent.com/f513fa631bd780dc0ec3cf2663777e356dc3664f/687474703a2f2f696d61676573323031352e636e626c6f67732e636f6d2f626c6f672f3636303036372f3230313630342f3636303036372d32303136303431343232343733323232332d3337333933303233322e6a7067"
]
},
"ok": true,
"code": 200,
"msg": "success",
"sql:generate|cache|execute|maxExecute": "1|0|1|200",
"depth:count|max": "1|5",
"time:start|duration|end": "1611279884442|12|1611279884454"
}
更多的介面功能和查詢語法見:
新增一個介面
需求:假設我們需要新增一張資料表,並把資料表的資料快速釋出出來
假設要增加的資料表如下:
-- 原石
CREATE TABLE `b_stone` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`cost` int(10) NULL COMMENT '成本',
`price` int(10) NULL COMMENT '賣價',
`length` int(10) NULL,
`width` int(10) NULL,
`height` int(10) NULL,
`weight` float(8,1) NULL,
`creationdate` datetime default CURRENT_TIMESTAMP COMMENT '建立時間',
`modifydate` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間',
`modifier` varchar(80) NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
我們需要在model包下增加一個類:
package apijson.demo.model;
import apijson.MethodAccess;
@MethodAccess(
POST = {UNKNOWN, ADMIN},
DELETE = {ADMIN}
)
public class Stone {
}
在DemoSQLConfig中增加:
TABLE_KEY_MAP.put(Stone.class.getSimpleName(),"b_store");
配置表和實體類的對映
還需要在這個類中註冊許可權:
AbstractVerifier.ACCESS_MAP.put(Stone.class.getSimpleName(),getAccessMap(Stone.class.getAnnotation(MethodAccess.class)));
為了防止登入錯誤,我們可以提前先增加如下程式碼,DemoParser中,在每個構造方法執行完super()後增加:
setNeedVerify(false);
重啟應用,POST請求:http://localhost:8080/get
body
{
"Stone": {
"id": 1
}
}
返回
{
"Stone": {
"id": 1,
"cost": 2,
"price": 3,
"length": 4,
"width": 5,
"height": 6,
"weight": 7.0,
"creationdate": "2021-01-22 10:00:56.0",
"modifydate": "2021-01-22 10:01:00.0",
"modifier": "8"
},
"ok": true,
"code": 200,
"msg": "success",
"sql:generate|cache|execute|maxExecute": "1|0|1|200",
"depth:count|max": "1|5",
"time:start|duration|end": "1611282106759|10|1611282106769"
}
高階查詢
資料修改
介面地址:http://localhost:8080/post
{
"Moment": {
"content": "今天天氣不錯,到處都是提拉米蘇雪",
"userId": 38710
},
"tag": "Moment"
}
因為可以修改資料,所以這裡涉及到和登入的問題
登入
介面的查詢可以可以配置是否需要登入,登入介面:http://127.0.0.1:8080/login,
傳送請求
{
"phone": "13000038710",
"password": "666666"
}
賬號和密碼,可以到apijson_user表裡面查詢