sequelize连接mysql_node.js通过Sequelize 连接MySQL
node.js通过Sequelize 连接MySQL
一.通过koa2脚手架构建项目
1.1 安装koa-generator
在终端输入:
$ npm install -g koa-generator1
1.2 使用koa-generator生成koa2项目
$ koa2 HelloKoa21
成功创建项目后,进入项目目录,并执行npm install命令
$ cd HelloKoa2
$ npm install1
2
1.3 启动项目
在终端输入:
$ npm start1
项目启动后,默认端口号是3000,在浏览器中运行可以得到下图的效果说明运行成功。
二.创建连接
2.1刚刚创建的文件使用webstorm打开
新建一个db目录
2.2查看Sequelize文档
使用npm安装Sequelize
npm install --save sequelize1
你还必须手动为所选数据库安装驱动程序选择一个方法之一:
# 选择以下之一:
$ npm install --save pg pg-hstore # Postgres
$ npm install --save mysql2
$ npm install --save mariadb
$ npm install --save sqlite3
$ npm install --save tedious # Microsoft SQL Server1
2
3
4
5
6
我这里下载得是MySQL2
2.3连接数据库
再刚刚创建得db文件加里面添加**config.js**
添加连接代码:
module.exports = { dbsMysql: 'mysql://root:123456@localhost:3306/new' //root是数据库管理员账号,‘123546’是密码 3306是端口号(MySQL默认是3306) school_admin是数据库名称
}1
2
3
4
5
6
继续在db文件夹里面添加mysql.js
添加连接以及添加日记:
const Sequelize = require('sequelize');
const mysqlurl = require('./config').dbsMysql
const sequelize = new Sequelize(mysqlurl, { // 选择一种日志记录参数 logging: console.log // 默认值,显示日志函数调用的第一个参数
});
// //每次启动server刷新数据库
// (async ()=>{
// await sequelize.sync({ force: true });
// })() module.exports = sequelize1
2
3
4
5
6
7
8
9
10
11
12
13
14
三.创建模型
3.1模型定义
在db目录下添加models文件夹再添加一个new2.js
定义模型:
const { Sequelize, DataTypes, Model } = require('sequelize');
const sequelize = require('../mysql');
const new2 = sequelize.define('t_new2', { name: { type: DataTypes.STRING, allowNull: false }, }, { // 这是其他模型参数 freezeTableName: true });
// 定义的模型是类本身
module.exports= new21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
四.添加路由
4.1创建new2路由
在routes文件夹中添加new2.js
//引入kob得routes模块
const router = require('koa-router')()
//定义模型为刚刚创建得new2.js
let Model = require("../db/models/new2");
//正常来说启动端口为http://localhost:3000 添加/new2就可以进入new2路由
router.prefix('/new1')
// 进入new2路由以后可以打印this is a users response!
router.get('/', function (ctx, next) { ctx.body = 'this is a users response!'
})
//设置增加add接口
router.post('/add', async function (ctx, next) { console.log(ctx.request.body) const new2 = await Model.create(ctx.request.body); ctx.body = { code:200, data:new2 }
})
//设置查询find接口
router.post('/find', async function (ctx, next) { const new2 =await Model.findAll({include: []}) console.log(1111) ctx.body = { code: 200, data: new2 }
})
//设置通过id得到所需信息的get接口
router.post('/get', async function (ctx, next) { // let users = await User. // find({}) console.log(ctx.request.body) let new2 = await Model.findOne({ // attributes: ['name', 'where'] where: { id: ctx.request.body.id } }); ctx.body = { code:200, data:new2 }
})
//设置修改update接口
router.post('/update', async function (ctx, next) { console.log(ctx.request.body) // let pbj = await Model.update({ _id: ctx.request.body._id }, ctx.request.body); let new2 = await Model.update(ctx.request.body, { where: { id: ctx.request.body.id } }); ctx.body = new2
})
//设置删除delete接口
router.post('/delete', async function (ctx, next) { console.log(ctx.request.body) // 删除所有名为 "Jane" 的人 await Model.destroy({ where: { id: ctx.request.body.id } }); ctx.body = 'shibai '
})
// //每次启动server刷新数据库
// (async ()=>{
// await sequelize.sync({ force: true });
// })()
module.exports = router1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
4.2在app.js里面添加路由
//引入刚刚创建的new2路由
const new2 =require('./routes/new2')1
2
//使用我们的路由
app.use(new2.routes(),new2.allowedMethods())1
2
4.3启动项目
在数据库中查看
5.测试
5.1使用浏览器查看
输入url:http://localhost:3000/new2
5.2.使用postman测试接口
测试find接口(因为我们写的find方法使用的post方法所以记得将get换成post):
http://localhost:3000/new2/find1
测试get接口
展示一下最后的目录
文章来源: blog.csdn.net,作者:ky_xin,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_44858959/article/details/111691928
sequelize连接mysql_node.js通过Sequelize 连接MySQL相关推荐
- MySQL会回收使用中的连接吗_Node.js实现mysql连接池使用事务自动回收连接的方法示例...
本文实例讲述了Node.js实现mysql连接池使用事务自动回收连接的方法.分享给大家供大家参考,具体如下: var mysql = require('mysql'), Connection = re ...
- paip.最好的脚本语言node js 环境搭建连接mysql
paip.最好的脚本语言node js 环境搭建连接mysql #====下载node...走十一个exe..容易的.. 1 #0----Hello world .js 2 #---------模 ...
- sequelize模型关联_关于Sequelize连接查询时inlude中model和association的区别详解
前言 大家都知道在使用Sequelize进行关系模型(表)间连接查询时,我们会通过model/as来指定已存在关联关系的连接查询模型,或是通过association来直接指定连接查询模型关系.那么,两 ...
- egg连接oracle,egg插件sequelize:表自连接
地区表: area id pid name level 1 0 福建 1 2 1 福州 2 3 2 厦门 2 自连接查询数据: const area= ctx.model.Area.getTableN ...
- 连接并配置好无极网络VPS主机 | 配置Mysql数据库 | Centos 常用操作| vim操作 | 保持centos的任务 | 阿里云mysql | Node.js | 腾讯云mysql
在无极网络获得主机并用Xshell6连接上 配置主机的DNS和网络连接 # http://www.5jwl.com/style/info/shownews.asp?id=1383 Xshell 6 ( ...
- 服务器nodejs连接mysql_《使用nodejs连接mysql数据库》
上边我们熟悉了如何用docker拉取远程的mysql,以及进入这个数据库然后连接.并且在命令行操作数据库,比如查看,或者操作数据库中的表. 也可以用node js操作数据库.只不过node的原生模块里 ...
- MySQL 学习笔记(15)— 连接查询(内连接、左外连接、右外连接、全外连接、交叉连接、自然连接等)
本文参考:https://gitbook.cn/gitchat/column/undefined/topic/5db92b68a9c3a53bc3800eff SQL 支持的连接查询包括内连接.外连接 ...
- mysql 连接 优化_(一)MySQL 连接优化
1.查看连接参数(show variables) mysql> show variables like '%connect%'; +------------------------------- ...
- 突然远程连接不上mysql_navicat突然连接不上远程linux服务器上的mysql
我linux服务器上的mysql是docker安装的,突然有一天我的navicat连接不上服务器上的mysql,于是开始了下面一系列的修复 1.首先登录服务器上mysql,看是否能正常登录,我发现不能 ...
最新文章
- ubuntu java classpath_java – 如何为Ubuntu中的文件夹设置CLASSPATH变量
- 从编码、网络传输、架构设计揭秘腾讯云高质量、高可用实时音视频技术实践...
- 作者:梁永生(1971-),男,博士,深圳信息职业技术学院教授,广东省珠江学者特聘教授,《大数据》深圳市大数据发展动态调研组组长...
- eclipse不能自动编译工程的解决方法
- 设计FMEA步骤五:风险分析
- android 应用市场发布以及流程(非原创)
- 阿里 酷家乐:实习生面试
- 小狼毫0.14 五笔拼音混输 Rime14
- Hadoop HA_Yarn HA 集群的搭建
- 查看英伟达NVIDIA显卡型号
- [云原生专题-41]:K8S - 核心概念 - Service业务的统一网关接口Ingress详解、安装、常见操作命令
- Excel求和公式的几种用法
- [译]GitHub应对1.28宕机事故的前前后后
- 数据结构与算法3 树(上)树与树的表示
- python自动化办公之 第1章 安装部署国产化系统和wps软件并python安装
- 生物信息-related
- 新编好的大盘指数预测程序,欢迎关注
- 什么是快速连接器?如何选择合适的快速密封接头?
- 【Turtle表白系列】你为什么还单身 ?因为昨天才遇见你。(爱心升级版本来袭)
- 华为IP的考试费要好几千,想问一下这个证书的含金量怎么样?