Postman如何通过xmysql工具的Restful API 接口访问MySQL
- GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。
导语
有时候用 Postman 接口测试需要获取MySQL的查询结果做接口输出的校验,这里介绍下 Postman 通过 Restful API 接口访问 MySQL 的工具xmysql的使用方法。
步骤
一、使用nmp安装xmysql
注:npm的安装和配置自行百度,这里主要讲xmysql,就不赘述了
C:\Users\wmp>node -v
v12.16.3
C:\Users\wmp>
C:\Users\wmp> npm install -g xmysql
C:\Users\wmp\AppData\Roaming\npm\xmysql -> C:\Users\wmp\AppData\Roaming\npm\node_modules\xmysql\bin\index.js
> es5-ext@0.10.59 postinstall C:\Users\wmp\AppData\Roaming\npm\node_modules\xmysql\node_modules\es5-ext
> node -e "try{require('./_postinstall')}catch(e){}"
+ xmysql@0.5.1
added 131 packages from 68 contributors in 22.806s
安装完输入xmysql可以查看具体参数,同时也表示安装成功
C:\Users\wmp>xmysql
Usage: index [options]
Options:
-V, --version output the version number
-h, --host <n> hostname of database / localhost by default
-u, --user <n> username of database / root by default
-p, --password <n> password of database / empty by default
-d, --database <n> database schema name
-r, --ipAddress <n> IP interface of your server / localhost by default
-n, --portNumber <n> port number for app / 3000 by default
-o, --port <n> port number for mysql / 3306 by default
-S, --socketPath <n> unix socket path / not used by default
-s, --storageFolder <n> storage folder / current working dir by default / available only with local
-i, --ignoreTables <n> comma separated table names to ignore
-a, --apiPrefix <n> api url prefix / "/api/" by default
-y, --readOnly readonly apis / false by default
-c, --useCpuCores <n> use number of CPU cores (using cluster) / 1 by default
-h, --help output usage information
Examples:
$ xmysql -u username -p password -d databaseSchema
Error: password for database is missing
Error: database name is missing
C:\Users\wmp>
连接数据库:
xmysql -h host_name -o port -u user_name -p user_password -d database_name
C:\Users\wmp>xmysql -h 192.168.5.103 -u root -p 123456 -o 3301 -d test
Generating REST APIs at the speed of your thought..
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Database : test
Number of Tables : 3 【test库表的数量】
REST APIs Generated : 62 【生成的api数量】
Xmysql took : 0 seconds
API's base URL : localhost:3000 【api的访问方式,端口3000可以通过参数 -n进行修改为其他】
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
xmysql的默认端口是3000,下面用postman调用api的方式试下数据库的增删改查(注: 在postman中执行sql语句时,cmd窗口和msqll都不可以关闭)
API Overview
HTTP Type | API URL | Comments |
---|---|---|
GET | / | 获取所有 REST API |
GET | /api/tableName | 列出表的行 |
POST | /api/tableName | 创建一个新行 |
PUT | /api/tableName | 用新行替换现有行 |
POST | /api/tableName/bulk | 创建多行 - 在请求正文中发送对象数组 |
GET | /api/tableName/bulk | 列出多行 - /api/tableName/bulk?_ids=1,2,3 |
DELETE | /api/tableName/bulk | 删除多行 - /api/tableName/bulk?_ids=1,2,3 |
GET | /api/tableName/:id | 按主键检索一行 |
PATCH | /api/tableName/:id | 按主键更新行元素 |
DELETE | /api/tableName/:id | 按主键删除一行 |
GET | /api/tableName/findOne | 作为列表工作,但获得单个记录匹配条件 |
GET | /api/tableName/count | 计算表中的行数 |
GET | /api/tableName/distinct | 表中的不同行 - /api/tableName/distinct?_fields=col1 |
GET | /api/tableName/:id/exists | 是否存在行 |
GET | /api/parentTable/:id/childTable | 获取具有父表外键的子表行列表 |
GET | /api/tableName/aggregate | 汇总数字列的结果 |
GET | /api/tableName/groupby | 按列的结果分组 |
GET | /api/tableName/ugroupby | 使用一次调用按结果进行多个分组 |
GET | /api/tableName/chart | 基于 (min,max,step) 或 (step array) 或 (automagic) 的数值列分布 |
GET | /api/tableName/autochart | 与 Chart 相同,但会自动识别哪些是数字列 |
GET | /api/xjoin | 处理连接 |
GET | /dynamic | 使用参数执行动态 mysql 语句 |
GET | /upload | 上传单个文件 |
GET | /uploads | 上传多个文件 |
GET | /download | 下载文件 |
GET | /api/tableName/describe | 描述每个表的列 |
GET | /api/tables | 获取数据库中的所有表 |
GET | /_health | 获取进程和 mysql 的运行状况 |
GET | /_version | 获取 Xmysql、mysql、node 的版本 |
查看对象表下所有可用的接口,可以参数调用并查看结果
以下是t1表可用的接口:
用t1表做一个简单的查询测试:
1、查询t1的整表数据
GET /api/t1
SQL: select * from `t1` ;
2、带条件查询
GET /api/t1?_where=(ida,eq,1)
SQL: select * from `t1` where `ida`=1;
3、分页查询
4、in查询
GET /api/t1/bulk?_ids=1,2,3
SQL: select * from `t1` where `ida` in (1,2,3) limit 0,20;
其他的增删改参考上述的API Overview,下面演示下xmysql在postman接口测试中的应用:
应用一:接口测试数据校验
- 启动xmysql服务:(测试时保持服务启动状态不要关闭)
xmysql -h 192.168.5.103 -u root -p Great@123 -o 3306 -d SCOTT
- 创建postman测试collection,包含测试接口和xmysql获取mysql数据接口
#测试接口1:获取全国行政区划
http://127.0.0.1:8369/query?fid={{pregion_code}}
#接口2:xmysql获取mysql数据接口
http://localhost:3000/api/AREAS?_where=(parent_region_code,eq,{{pregion_code}})
接口参数化,可在postman collection中设置全局参数并赋值
//定义变量做接口的输入传参并设置为全局变量
var pregion_code=110100;
pm.environment.set("pregion_code", pregion_code);
接口输出参数化设置:
在接口1Tests中用js脚本对接口输出结果需要断言的部分转参数并设置环境变量,以便在接口2的数据库输出中进行比较
//返回body转json
var jsonData = JSON.parse(responseBody);
var list=jsonData.result;
var regionCode=[];
num=list.length;
for (let i=0;i<num;i++){
regionCode.push(list[i].id);
}
//设置为全局变量,用于数据库查询接口的数据校验
pm.environment.set("regionCode", regionCode);
//console.log(regionCode)
接口2Tests中用js脚本对数据库查询到的json脚本进行处理,获取需要的数据,参数化后与接口1的输出进行比较
//返回body转json
var jsonData = JSON.parse(responseBody);
var res_id=[];
num=jsonData.length;
//获取数据库查询结果的region_code列
for (let i=0;i<num;i++){
res_id.push(jsonData[i].region_code+'');
}
//获取接口1输出的结果参数regionCode
var regionCode=pm.environment.get("regionCode");
//断言 比较接口和数据库的输出结果
tests["接口断言成功!!"] = res_id.sort().toString === regionCode.sort().toString ;
//console.log(res_id);
- 运行测试collection
应用二:利用postman+xmysql实现MySQL快速生成大量测试数据
建表脚本
drop table if exists t1;
CREATE TABLE t1
(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY comment '用户ID',
person_name varchar(30) comment '用户名称'
)
这里演示插入10000条:
- 创建insert 接口
- 设置collection并执行
3、执行完毕,查看表数量
调用表行数查询接口查询结果:
声明:xmysql工具主要用于测试环境,结合接口测试等,主要用于内部测试,在生产还是不建议使用,因为这款工具会造成严重的数据安全问题。
参考文档:
https://gitee.com/thinkyoung/xmysql
Enjoy GreatSQL
文章推荐:
面向金融级应用的GreatSQL正式开源
https://mp.weixin.qq.com/s/cI_wPKQJuXItVWpOx_yNTg
Changes in GreatSQL 8.0.25 (2021-8-18)
https://mp.weixin.qq.com/s/qcn0lmsMoLtaGO9hbpnhVg
MGR及GreatSQL资源汇总
https://mp.weixin.qq.com/s/qXMct_pOVN5FGoLsXSD0MA
GreatSQL MGR FAQ
https://mp.weixin.qq.com/s/J6wkUpGXw3YkyEUJXiZ9xA
在Linux下源码编译安装GreatSQL/MySQL
https://mp.weixin.qq.com/s/WZZOWKqSaGSy-mpD2GdNcA
关于 GreatSQL
GreatSQL是由万里数据库维护的MySQL分支,专注于提升MGR可靠性及性能,支持InnoDB并行查询特性,是适用于金融级应用的MySQL分支版本。
Gitee:
https://gitee.com/GreatSQL/GreatSQL
GitHub:
https://github.com/GreatSQL/GreatSQL
Bilibili:
https://space.bilibili.com/1363850082/video
微信&QQ群:
可搜索添加GreatSQL社区助手微信好友,发送验证信息“加群”加入GreatSQL/MGR交流微信群
QQ群:533341697
微信小助手:wanlidbc
本文由博客一文多发平台 OpenWrite 发布!
Postman如何通过xmysql工具的Restful API 接口访问MySQL的更多相关文章
- Java 调用Restful API接口的几种方式--HTTPS
摘要:最近有一个需求,为客户提供一些Restful API 接口,QA使用postman进行测试,但是postman的测试接口与java调用的相似但并不相同,于是想自己写一个程序去测试Restful ...
- SpringMVC Restful api接口实现
[前言] 面向资源的 Restful 风格的 api 接口本着简洁,资源,便于扩展,便于理解等等各项优势,在如今的系统服务中越来越受欢迎. .net平台有WebAPi项目是专门用来实现Restful ...
- 整合swagger2生成Restful Api接口文档
整合swagger2生成Restful Api接口文档 swagger Restful文档生成工具 2017-9-30 官方地址:https://swagger.io/docs/specificati ...
- Spring Boot入门系列(二十)快速打造Restful API 接口
spring boot入门系列文章已经写到第二十篇,前面我们讲了spring boot的基础入门的内容,也介绍了spring boot 整合mybatis,整合redis.整合Thymeleaf 模板 ...
- 用 shell 脚本做 restful api 接口监控
问题的提出 基于历史原因,公司有一个"三无"采集服务--无人员.无运维.无监控--有能力做的部门不想接.接了的部门没能力.于是就一直这样裸奔,直到前几天一个依赖于这个采集服务的大数 ...
- 初识restful api接口
一.restful api接口举例 实现功能 传统方式 restful方式 url HTTP方法 url HTTP方法 查询 /user/query?name=knyel GET /user?name ...
- 使用Flask设计带认证token的RESTful API接口
大数据时代 Just a record. 使用Flask设计带认证token的RESTful API接口[翻译] 上一篇文章, 使用python的Flask实现一个RESTful API服务器端 简 ...
- Spring Boot入门系列(二十一)如何优雅的设计 Restful API 接口版本号,实现 API 版本控制!
前面介绍了Spring Boot 如何快速实现Restful api 接口,并以人员信息为例,设计了一套操作人员信息的接口.不清楚的可以看之前的文章:https://www.cnblogs.com/z ...
- Restful API 接口设计标准及规范
Restful API 接口设计标准以及规范 RESTful概念 理解和评估以网络为基础的应用软件的架构设计,得到一个功能强.性能好.适宜通信的架构.REST指的是一组架构约束条件和原则." ...
随机推荐
- Nginx基本配置与应用
一.准备 1.1 环境准备 CentOS7软件环境 1.2 tomcat多实例 把/etc/profile.d/tomcat.sh中的变量注释了 #export TOMCAT_HOME=/usr/lo ...
- Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查
今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...
- poj3784(对顶堆)
题意:多组数据,让你求出1~i(i为奇数&&i<=n)的中位数 思路:首先复杂度必为O(n)或O(nlogn)的(数据范围) 思索,如果题目要求1次中位数,好求!排个序,取a[( ...
- Nastran的应变方向
问题 近日使用Nastran做一个算例,在计算频响时发现:位移场是连续的,而应变场不连续.以某一频率处应变场为例,其上表面X.Y方向应变场分布如下图.此处关闭了云图的插值,所显示的为单元的应变,因此云 ...
- JavaScript Number -> String
六种将Number类型转化为String类型的方法: 方法一:通过+运算符加上一个空字符串: eg:'' + 5 -> '5' 5 + '' -> '5' 方法二:toStrin ...
- 2021.05.03【NOIP提高B组】模拟 总结
比较水的一场比赛,却不能 AK T1 有 \(n\) 次,每次给 \(A_i,B_i\) 问以 \(i\) 结尾的 \(A,B\) 的匹配中最大和的最小值 问最大和的最小值,却不用二分. 如果暴力排序 ...
- Linux文本搜索及截取操作
Linux文本搜索及截取操作 cat 查看 grep 搜索 awk 截取 查看dna-server.xml 文件的内容 [root@localhost servers]# cat cwag9002/w ...
- vue华视电子身份证阅读器的使用
ie还是谷歌都是可以用的 只需要直接启用华视电子身份证阅读器的服务来的,至于服务已经上传到了网上 华视阅读器服务,下载下来解压,找到对应的华视电子读卡服务.exe文件,路径是CVR-1 ...
- springboot2.7.x 集成log4j2配置写入日志到mysql自定义表格
在阅读之前请先查看[springboot集成log4j2] 本文暂不考虑抽象等实现方式,只限于展示如何自定义配置log4j2并写入mysql数据库(自定义结构) 先看下log4j2的配置 <?x ...
- Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关
在 Apache ShardingSphere 5.1.1 发布后,ShardingSphere 合并了来自全球的团队或个人的累计 1028 个 PR,为大家带来 5.1.2 新版本.该版本在功能.性 ...