日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

sequelize连接mysql_node.js通过Sequelize 连接MySQL

發(fā)布時(shí)間:2023/12/2 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sequelize连接mysql_node.js通过Sequelize 连接MySQL 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

node.js通過Sequelize 連接MySQL

一.通過koa2腳手架構(gòu)建項(xiàng)目

1.1 安裝koa-generator

在終端輸入:

$ npm install -g koa-generator1

1.2 使用koa-generator生成koa2項(xiàng)目

$ koa2 HelloKoa21

成功創(chuàng)建項(xiàng)目后,進(jìn)入項(xiàng)目目錄,并執(zhí)行npm install命令

$ cd HelloKoa2

$ npm install1

2

1.3 啟動(dòng)項(xiàng)目

在終端輸入:

$ npm start1

項(xiàng)目啟動(dòng)后,默認(rèn)端口號(hào)是3000,在瀏覽器中運(yùn)行可以得到下圖的效果說明運(yùn)行成功。

二.創(chuàng)建連接

2.1剛剛創(chuàng)建的文件使用webstorm打開

新建一個(gè)db目錄

2.2查看Sequelize文檔

使用npm安裝Sequelize

npm install --save sequelize1

你還必須手動(dòng)為所選數(shù)據(jù)庫(kù)安裝驅(qū)動(dòng)程序選擇一個(gè)方法之一:

# 選擇以下之一:

$ 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連接數(shù)據(jù)庫(kù)

再剛剛創(chuàng)建得db文件加里面添加**config.js**

添加連接代碼:

module.exports = { dbsMysql: 'mysql://root:123456@localhost:3306/new' //root是數(shù)據(jù)庫(kù)管理員賬號(hào),‘123546’是密碼 3306是端口號(hào)(MySQL默認(rèn)是3306) school_admin是數(shù)據(jù)庫(kù)名稱

}1

2

3

4

5

6

繼續(xù)在db文件夾里面添加mysql.js

添加連接以及添加日記:

const Sequelize = require('sequelize');

const mysqlurl = require('./config').dbsMysql

const sequelize = new Sequelize(mysqlurl, { // 選擇一種日志記錄參數(shù) logging: console.log // 默認(rèn)值,顯示日志函數(shù)調(diào)用的第一個(gè)參數(shù)

});

// //每次啟動(dòng)server刷新數(shù)據(jù)庫(kù)

// (async ()=>{

// await sequelize.sync({ force: true });

// })() module.exports = sequelize1

2

3

4

5

6

7

8

9

10

11

12

13

14

三.創(chuàng)建模型

3.1模型定義

在db目錄下添加models文件夾再添加一個(gè)new2.js

定義模型:

const { Sequelize, DataTypes, Model } = require('sequelize');

const sequelize = require('../mysql');

const new2 = sequelize.define('t_new2', { name: { type: DataTypes.STRING, allowNull: false }, }, { // 這是其他模型參數(shù) freezeTableName: true });

// 定義的模型是類本身

module.exports= new21

2

3

4

5

6

7

8

9

10

11

12

13

14

15

四.添加路由

4.1創(chuàng)建new2路由

在routes文件夾中添加new2.js

//引入kob得routes模塊

const router = require('koa-router')()

//定義模型為剛剛創(chuàng)建得new2.js

let Model = require("../db/models/new2");

//正常來說啟動(dòng)端口為http://localhost:3000 添加/new2就可以進(jìn)入new2路由

router.prefix('/new1')

// 進(jìn)入new2路由以后可以打印this is a users response!

router.get('/', function (ctx, next) { ctx.body = 'this is a users response!'

})

//設(shè)置增加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 }

})

//設(shè)置查詢find接口

router.post('/find', async function (ctx, next) { const new2 =await Model.findAll({include: []}) console.log(1111) ctx.body = { code: 200, data: new2 }

})

//設(shè)置通過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 }

})

//設(shè)置修改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

})

//設(shè)置刪除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 '

})

// //每次啟動(dòng)server刷新數(shù)據(jù)庫(kù)

// (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里面添加路由

//引入剛剛創(chuàng)建的new2路由

const new2 =require('./routes/new2')1

2

//使用我們的路由

app.use(new2.routes(),new2.allowedMethods())1

2

4.3啟動(dòng)項(xiàng)目

在數(shù)據(jù)庫(kù)中查看

5.測(cè)試

5.1使用瀏覽器查看

輸入url:http://localhost:3000/new2

5.2.使用postman測(cè)試接口

測(cè)試find接口(因?yàn)槲覀儗懙膄ind方法使用的post方法所以記得將get換成post):

http://localhost:3000/new2/find1

測(cè)試get接口

展示一下最后的目錄

文章來源: blog.csdn.net,作者:ky_xin,版權(quán)歸原作者所有,如需轉(zhuǎn)載,請(qǐng)聯(lián)系作者。

原文鏈接:blog.csdn.net/weixin_44858959/article/details/111691928

總結(jié)

以上是生活随笔為你收集整理的sequelize连接mysql_node.js通过Sequelize 连接MySQL的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。