1.2postman连接数据库xmysql和omysql的连接
预置条件:完成nodejs和npm的安装,想要postman连接mysql,需要安装xmysql或者omysql,启动该服务,然后才可以调用。
xmysql的使用:
安装时要注意版本的匹配问题,用法和注意事项可查看https://github.com/o1lab/xmysql#xmysql--one-command-to-generate-rest-apis-for-any-mysql-database
1、先下载安装nodejs
下载地址:https://nodejs.org/en/
可查看版本node -v
2、安装xmysql
命令行安装xmysql:npm install xmysql -g:
![](https://img.haomeiwen.com/i16814855/b7400737c62d1e56.png)
3、连接数据库
连接数据库的命令:xmysql -h localhost -u mysqlUser -p mysqlPassword -d databaseName
备注:-h,-u,-p,-o:要连接的数据库的信息, -r,-n想要启动xmysql服务机器的信息
![](https://img.haomeiwen.com/i16814855/43d00156bcc6148c.png)
4、执行sql语句
启动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/i16814855/100e81948f6fa1c2.png)
case2:按某种条件查询,get请求:http://localhost:3000/api/table_name?_where=(id,eq,3)
![](https://img.haomeiwen.com/i16814855/773c8fb133d78526.png)
case3:查询几行:get请求:http://localhost:3000/api/table_name?_fields=id,name,特殊的,分页_p=x&_size=x
![](https://img.haomeiwen.com/i16814855/9e14a9b5db424158.png)
case4:插入数据,post请求:http://localhost:3000/api/table_name:
![](https://img.haomeiwen.com/i16814855/31d216056a533757.png)
case5:新用patch、删除用delete
![](https://img.haomeiwen.com/i16814855/709987f0060a9031.png)
case6:其他方法参考:
http://testingpai.com/article/1595507196849
https://www.cnblogs.com/eriwang/p/10287704.html
https://cloud.tencent.com/developer/article/1485697
omysql的使用
1、先下载安装nodejs
下载地址:https://nodejs.org/en/
可查看版本node -v
2、在nodejs安装目录下创建2个文件夹,分别是node_cache,node_global
![](https://img.haomeiwen.com/i16814855/ccfba41f5386aaee.png)
3、然后在cmd执行npm config set prefix"D:\nodejs\node_global"和npm config set cache"D:\nodejs\node_cache"
![](https://img.haomeiwen.com/i16814855/7248804f329b3aa6.png)
4、配置环境变量
用户变量path新增D:\nodejs\node_global
![](https://img.haomeiwen.com/i16814855/1139b5035448b47f.png)
系统变量新增变量名:NODE_PATH,变量值:D:\nodejs\node_global\node_modules
![](https://img.haomeiwen.com/i16814855/580af8b557007af7.png)
配置完成后,安装个module测试下,安装最常用的express模块,打开cmd输入如下命令进行模块的全局安装:
npm install express -g
![](https://img.haomeiwen.com/i16814855/6bb99b3483187d28.png)
5、安装omysql
github 地址:https://github.com/liyinchigithub/Omysql
下载解压到nodejs的安装目录,然后cmd切换目录到cd D:\nodejs\Omysql-master。
安装依赖包:npm install
![](https://img.haomeiwen.com/i16814855/7d5495e389c42ac2.png)
6、启动omysql服务
执行启动命令: node server.js
![](https://img.haomeiwen.com/i16814855/3dedf69ba4975ad7.png)
7、执行sql语句
case1、功能 查询表内所有数据
请求方法:POST
请求地址:http://127.0.0.1:8005/mysql_demo/find
请求 body:
{
"host":"127.0.0.1",
"user":"root",
"password":"12345678",
"port":"3306",
"database":"数据库名称",
"findSql":"SELECT * FROM table4;"
}
![](https://img.haomeiwen.com/i16814855/7958a0a414acd4d2.png)
case2、功能 查询所有数据库
请求方法:POST
请求地址:
http://127.0.0.1:8005/mysql_demo/show_databases
请求 body:
{
"host":"127.0.0.1",
"port":"3306",
"user":"root",
"password":"12345678",
"findSql":"show databases"
}
![](https://img.haomeiwen.com/i16814855/227ddecca9e81c03.png)
case3、功能 查询数据库所有表
请求方法:POST
请求地址:
http://127.0.0.1:8005/mysql_demo/show_databases
请求 body:
{
"host":"127.0.0.1",
"port":"3306",
"user":"root",
"password":"12345678",
"database":"数据库名称",
"findSql":"show tables from zentao"
}
![](https://img.haomeiwen.com/i16814855/2968a15ad65b71d4.png)
case4、功能 创建数据库
请求方法:POST
请求地址:
http://127.0.0.1:8005/mysql_demo/CreateDB/
请求 body:
{
"host":"127.0.0.1",
"user":"root",
"password":"12345678",
"port":"3306",
"query":"CREATE DATABASE test;"
}
响应 body:
{
"result": "success",
"status": 200
}
![](https://img.haomeiwen.com/i16814855/efe643339c6ddc40.png)
case5、功能 创建表
请求方法:POST
请求地址:
http://127.0.0.1:8005/mysql_demo/CreateTable/
请求 body:
{
"host":"127.0.0.1",
"user":"root",
"password":"12345678",
"port":"3306",
"database":"数据库名称",
"query":"CREATE TABLE Persons(id int,name varchar(255),address varchar(255),listaddress varchar(255),City varchar(255));"
}
响应 body:
{
"result": "success",
"status": 200
}
![](https://img.haomeiwen.com/i16814855/f37c6fd5d0ce96b2.png)
case6、功能 插入数据
请求方法:PUT
请求地址:
http://127.0.0.1:8005/mysql_demo/insert_data/
请求 body:
{
"host":"127.0.0.1",
"user":"root",
"password":"123456",
"port":"3306",
"database":"test",
"addSql":"INSERT INTO persons(id,name,address,listaddress,City) VALUES(1,'pengyun','深圳南山','湖北宜昌','shenzhen');"
}
![](https://img.haomeiwen.com/i16814855/df8e41138a3b4215.png)
case7、功能 更新数据
请求方法:PUT
请求地址:http://127.0.0.1:8005/mysql_demo/update/
请求 body:
{
"host":"127.0.0.1",
"user":"root",
"password":"123456",
"port":"3306",
"database":"test",
"UpdateSql":"UPDATE persons SET address='福建省厦门市' WHERE name='pengyun';"
}
![](https://img.haomeiwen.com/i16814855/ec6d9c96846bcf62.png)
case8、功能 删除表数据
请求方法:DELETE
请求地址:http://127.0.0.1:8005/mysql_demo/delete/
请求 body:
{
"host":"127.0.0.1",
"user":"root",
"password":"123456",
"port":"3306",
"database":"test",
"delSql":"DELETE FROM persons where name='pengyun';"
}
![](https://img.haomeiwen.com/i16814855/8850022b2c267b86.png)
case9、功能 删除表
请求方法:DELETE
请求地址:http://127.0.0.1:8005/mysql_demo/drop
请求 body:
{
"host":"127.0.0.1",
"user":"root",
"password":"12345678",
"port":"3306",
"database":"数据库名称",
"DropSql":"DROP TABLE table4;"
}
![](https://img.haomeiwen.com/i16814855/6203d5b8422680b9.png)
case10、功能 删除数据库
请求方法:DELETE
请求地址:http://127.0.0.1:8005/mysql_demo/drop
请求 body:
{
"host":"127.0.0.1",
"user":"root",
"password":"123456",
"port":"3306",
"database":"test",
"DropSql":"DROP DATABASE test;"
}
![](https://img.haomeiwen.com/i16814855/3fd1be135461471d.png)