PostMan连接MySql数据库
nodejs 是前端的javascript库。在进行接口测试过程中,我们经常需要通过数据库对比检查来判断接口执行的结果是否正确。但是 Postman 并没有提供数据库的查询。就无法通过 Postman 去连接数据库验证数据的正确性。
好在,有一款基于 node.js 的工具Xmysql可以将数据库的表数据转换成 Restful API 接口,返回 JOSN 格式的数据结果。
一、安装nodejs
二、安装xmysql
三、连接数据库
四、获取数据库的相关信息的API
一、安装nodejs
[下载地址] https://nodejs.org/en/
想要postman连接mysql,需要安装xmysql,启动该服务,然后才可以调用。
预置条件:完成nodejs和npm的安装
安装时要注意版本的匹配问题,用法和注意事项可查看https://github.com/o1lab/xmysql#xmysql--one-command-to-generate-rest-apis-for-any-mysql-database
二、安装xmysql
命令行安装xmysql:npm install xmysql -g:
![](https://img.haomeiwen.com/i8379736/8499eb81474f3db1.png)
三、连接数据库
连接数据库的命令:xmysql -h localhost -u mysqlUser -p mysqlPassword -d databaseName
备注:-h,-u,-p,-o:要连接的数据库的信息, -r,-n想要启动xmysql服务机器的信息
![](https://img.haomeiwen.com/i8379736/254359fe912df6a1.png)
四、获取数据库的相关信息的API
启动xmysql后,即可通过http://localhost:3000/api/ 访问 xmysql 自动生成的 Rest API,详细的使用方法可通过https://github.com/o1lab/xmysql查看
备注:localhost是mysql服务要启动的机器的ip,3000是xmysql服务默认端口,可在启动时通过-r修改
case1:查询某表数据,get请求:http://localhost:3000/api/table_name:
![](https://img.haomeiwen.com/i8379736/dc7d81de61ef2148.png)
case2:按某种条件查询,get请求:http://localhost:3000/api/table_name?_where=(username,eq,root)
![](https://img.haomeiwen.com/i8379736/d7fcc58a0fca2155.png)
case3:查询几行:get请求:http://localhost:3000/api/table_name?_fields=username,password,特殊的,分页_p=x&_size=x
![](https://img.haomeiwen.com/i8379736/975702aef2b356a0.png)
case4:插入数据,post请求:http://localhost:3000/api/table_name:
![](https://img.haomeiwen.com/i8379736/562c16a406d3d145.png)
case5:新用patch、删除用delete
![](https://img.haomeiwen.com/i8379736/08ddeacec6012a0b.png)