apijson簡單使用

apijson簡單使用

介紹

APIJSON 是一種專爲 API 而生的 JSON 網絡傳輸協議 以及 基於這套協議實現的 ORM 庫。爲簡單的增刪改查、複雜的查詢、簡單的事務操做 提供了徹底自動化的萬能 API。能大幅下降開發和溝通成本,簡化開發流程,縮短開發週期。適合中小型先後端分離的項目,尤爲是 BaaS、Serverless、互聯網創業項目和企業自用項目。html

Gitee:https://gitee.com/Tencent/APIJSONjava

示例

Java端:https://gitee.com/greyzeng/apijson-samplegit

運行

準備數據庫

DemoSQLConfig.java這個文件中提供了數據庫的配置信息github

須要配置:sql

  • 數據庫的Schema
  • 數據庫Version
  • 數據庫鏈接的URI
  • 數據庫的用戶名密碼

將/sql目錄下的腳本文件導入數據庫中。數據庫

增長依賴

將/libs目錄下的jar包增長到項目的classpath中json

啓動項目

運行DemoApplication後端

測試

在Postman中新增一個POST請求,請求的URL是:api

http://localhost:8080/get網絡

請求的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://127.0.0.1:8080/login,
發送請求

{
  "phone": "13000038710",
  "password": "apijson"
}

帳號和密碼,能夠到apijson_user表裏面查詢

生成接口文檔

APIAuto

參考資料

apijson-doc

APIJSON-boot

相關文章
相關標籤/搜索