# powerful-api **Repository Path**: danielliu1123223/powerful-api ## Basic Information - **Project Name**: powerful-api - **Description**: 基于mybatis-plus,通过Json数据格式只需要一个接口即可完成单表基本的增、删、改、查操作。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-03-03 - **Last Updated**: 2021-03-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # powerful-api #### Description ​ **基于mybatis-plus,通过`Json`数据格式只需要一个接口即可完成单表基本的增、删、改、查操作。** 提供接口: ```java @PostMapping("/api") public PageResult api(@RequestBody Execution execution) { return ApiUtil.execute(windowService, execution); } ``` 执行条件: ```json { "query": { "select": [], // 查询的column "where": { "eq": {}, // = e.g. "name"="daniel" "like": {}, // like e.g. "name" LIKE "%dan%" 注意: 这里的like表示or关系 "gt": {}, // > "lt": {}, // < "ge": {}, // >= "le": {}, // <= "in": {} // in }, "page": { "cur": 0, // 当前页数 默认1 "size": 0, // 每页显示条数 默认10 "searchCount": true, // 在分页查询中是否需要查询记录数 默认false "findAll": true // true表示直接查询所有 cur和size变无效 }, "order": {} // order by (asc表示升序, des表示降序) }, "update": { "set": { "update": {}, // 直接设值 e.g. set "name"="daniel" "offset": {} // 设置偏移量 e.g. set `follower`=`follower`+1 }, "where": { "eq": {}, "like": {}, "gt": {}, "lt": {}, "ge": {}, "le": {}, "in": {} } }, "delete": { "where": { "eq": {}, "like": {}, "gt": {}, "lt": {}, "ge": {}, "le": {}, "in": {} } }, "insert": { "entity": {} // 将map封装为bean, 使用驼峰或下划线均可 } } ``` ## e.g. #### 查询操作 ​ 分页模糊查询 `window_id`='1' AND (`name` LIKE '%菜%' OR `description` LIKE '%菜%') AND `follower` > 2 根据 `add_time`降序, `follower`升序 排序, 当前页数为1, 每页显示条数5, 显示总的记录数 请求体为: ```json { "query": { "select": ["name"], "where": { "eq": { "window_id": "1" }, "like": { "name": "%菜%", "description": ["%菜%"] // 可以传数组 }, "gt": { "follower": 2 } }, "page": { "cur": 1, "size": 5, "searchCount": true, "findAll": false }, "order": { "add_time": "des", "follower": "ASC" } } } ``` 得到的sql语句: ```mysql SELECT name FROM mh_window WHERE (window_id = ? AND (name LIKE ? OR description LIKE ?) AND follower > ?) ORDER BY add_time DESC,follower ASC ``` (因为查询到的结果集为0, 所以不会有 LIMIT 1,5) #### 更新操作 ​ 给`window_id`为'1'数据设置 `name`='北京烤牛', `follower`=`follower`+1 请求体为: ```json { "update": { "set": { "update": { "name": "北京烤牛" }, "offset": { "follower": 1 } }, "where": { "eq": { "window_id": "1" } } } } ``` 得到sql: ```mysql UPDATE mh_window SET name=?,follower=follower+(1) WHERE (window_id = ?) ``` #### 其他操作 ​ 其他操作同理...... #### restful风格 ​ 如果想要体现出restful的风格, 可使用下面的方式 ```java /** * query */ @PostMapping("/query") public PageResult query(@RequestBody Query query) { return ApiUtil.execute(windowService, query); } /** * update */ @PutMapping("/update") public PageResult update(@RequestBody Update update) { return ApiUtil.execute(windowService, update); } /** * delete */ @DeleteMapping("/delete") public PageResult delete(@RequestBody Delete delete) { return ApiUtil.execute(windowService, delete); } /** * insert */ @PostMapping("/insert") public PageResult insert(@RequestBody Insert insert) { return ApiUtil.execute(windowService, insert); } ```