推荐 APIJSON, 后端接口和文档自动化,前端(客户端) 定制返回 JSON 的数据和结构。
### 1.单表查询
```js
{
"User": {
"sex": 1
}
}
```
自动转换为
```sql
SELECT * FROM User WHERE sex = '1' LIMIT 0, 1
```
### 2.数组查询
```js
{
"[]": {
"count": 10,
"page": 1,
"User": {
"sex": 1
}
}
}
```
自动转换为
```sql
SELECT * FROM User WHERE sex = '1' LIMIT 10, 20
```
### 3.应用场景示例:
#### 获取用户
请求:
<pre><code class="language-json">{
"User":{
}
}
</code></pre>
[点击这里测试](
http://apijson.cn:8080/get/{"User":{}})
返回:
<pre><code class="language-json">{
"User":{
"id":38710,
"sex":0,
"name":"TommyLemon",
"tag":"Android&Java",
"head":"
http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
"date":1485948110000,
"pictureList":[
"
http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
"
http://common.cnblogs.com/images/icon_weibo_24.png"
]
},
"code":200,
"msg":"success"
}
</code></pre>
<br />
#### 获取用户列表
请求:
<pre><code class="language-json">{
"[]":{
"count":3, //只要 3 个
"User":{
"@column":"id,name" //只要 id,name 这两个字段
}
}
}
</code></pre>
[点击这里测试](
http://apijson.cn:8080/get/{"[]":{"count":3,"User":{"@column":"id,name"}}})
返回:
<pre><code class="language-json">{
"[]":[
{
"User":{
"id":38710,
"name":"TommyLemon"
}
},
{
"User":{
"id":70793,
"name":"Strong"
}
},
{
"User":{
"id":82001,
"name":"Android"
}
}
],
"code":200,
"msg":"success"
}
</code></pre>
<br />
#### 获取动态及发布者用户
请求:
<pre><code class="language-json">{
"Moment":{
},
"User":{
"id@":"Moment/userId" //
User.id = Moment.userId
}
}
</code></pre>
[点击这里测试](
http://apijson.cn:8080/get/{"Moment":{},"User":{"id@":"Moment%252FuserId"}})
返回:
<pre><code class="language-json">{
"Moment":{
"id":12,
"userId":70793,
"date":"2017-02-08 16:06:11.0",
"content":"1111534034"
},
"User":{
"id":70793,
"sex":0,
"name":"Strong",
"tag":"djdj",
"head":"
http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000",
"contactIdList":[
38710,
82002
],
"date":"2017-02-01 19:21:50.0"
},
"code":200,
"msg":"success"
}
</code></pre>
<br />
#### 获取类似微信朋友圈的动态列表
请求:
<pre><code class="language-json">{
"[]":{ //请求一个数组
"page":0, //数组条件
"count":2,
"Moment":{ //请求一个名为 Moment 的对象
"content$":"%a%" //对象条件,搜索 content 中包含 a 的动态
},
"User":{
"id@":"/Moment/userId", //
User.id = Moment.userId 缺省引用赋值路径,从所处容器的父容器路径开始
"@column":"id,name,head" //指定返回字段
},
"Comment[]":{ //请求一个名为 Comment 的数组,并去除 Comment 包装
"count":2,
"Comment":{
"momentId@":"[]/Moment/id" //Comment.momentId =
Moment.id 完整引用赋值路径
}
}
}
}
</code></pre>
[点击这里测试](
http://apijson.cn:8080/get/{"[]":{"page":0,"count":2,"Moment":{"content$":"%2525a%2525"},"User":{"id@":"%252FMoment%252FuserId","@column":"id,name,head"},"Comment[]":{"count":2,"Comment":{"momentId@":"[]%252FMoment%252Fid"}}}})
返回:
<pre><code class="language-json">{
"[]":[
{
"Moment":{
"id":15,
"userId":70793,
"date":1486541171000,
"content":"APIJSON is a JSON Transmission Structure Protocol …",
"praiseUserIdList":[
82055,
82002,
82001
],
"pictureList":[
"
http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
"
http://common.cnblogs.com/images/icon_weibo_24.png"
]
},
"User":{
"id":70793,
"name":"Strong",
"head":"
http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000"
},
"Comment[]":[
{
"id":176,
"toId":166,
"userId":38710,
"momentId":15,
"date":1490444883000,
"content":"thank you"
},
{
"id":1490863469638,
"toId":0,
"userId":82002,
"momentId":15,
"date":1490863469000,
"content":"Just do it"
}
]
},
{
"Moment":{
"id":58,
"userId":90814,
"date":1485947671000,
"content":"This is a Content...-435",
"praiseUserIdList":[
38710,
82003,
82005,
93793,
82006,
82044,
82001
],
"pictureList":[
"
http://static.oschina.net/uploads/img/201604/22172507_aMmH.jpg"
]
},
"User":{
"id":90814,
"name":7,
"head":"
http://static.oschina.net/uploads/user/51/102723_50.jpg?t=1449212504000"
},
"Comment[]":[
{
"id":13,
"toId":0,
"userId":82005,
"momentId":58,
"date":1485948050000,
"content":"This is a Content...-13"
},
{
"id":77,
"toId":13,
"userId":93793,
"momentId":58,
"date":1485948050000,
"content":"This is a Content...-77"
}
]
}
],
"code":200,
"msg":"success"
}
</code></pre>
<br />
[在线测试](
http://apijson.org)
创作不易,您的支持是对作者的最大鼓励,右上角点 Star 支持下吧,谢谢 ^_^