數(shù)據(jù)庫連接池以及sequelize實現(xiàn)增刪改查等操作指南
數(shù)據(jù)庫連接池
介紹數(shù)據(jù)庫連接池
數(shù)據(jù)庫連接池負責分配、管理和釋放數(shù)據(jù)庫連接,它允許應用程序重復使用一個現(xiàn)有的數(shù)據(jù)庫連接,而不是重新建立一個;釋放空閑時間超過最大空閑時間的數(shù)據(jù)庫連接來避免因為沒有釋放數(shù)據(jù)庫連接而引起的數(shù)據(jù)庫連接遺漏。
通俗的理解就是: 數(shù)據(jù)庫連接池是程序啟動時建立足夠數(shù)量的數(shù)據(jù)庫連接對象,并將這些連接對象組成一個池,由程序動態(tài)的對池中的連接對象進行申請、使用和釋放。
優(yōu)點
(1)避免應用程序頻繁的連接、斷開數(shù)據(jù)庫
(2)提供數(shù)據(jù)庫連接對象的使用頻率。
使用方法
(1)創(chuàng)建數(shù)據(jù)庫連接池:
mysql.createPool(config)
host:數(shù)據(jù)庫服務器的地址
port: 端口號
user:連接數(shù)據(jù)庫的用戶名
password:連接數(shù)據(jù)庫的密碼
database:數(shù)據(jù)庫名
connectionLimit:用于指定連接池中最大的鏈接數(shù),默認屬性值為10.
multipleStatements :是否允許執(zhí)行多條sql語句,默認值為false
(2)從連接池中獲取一個連接
連接池名.getConnection(function(err,connection){
執(zhí)行的代碼
})
//參數(shù)err:錯誤對象。連接失敗后的錯誤信息
//參數(shù)connection:連接對象。若連接失敗,它就是undefined(3)釋放連接對象(將連接對象放回連接池): connection.release();
(4)從連接池中移除連接對象: connection.destory();
(5)關(guān)閉該連接池: 連接池名.end();
數(shù)據(jù)庫訪問中的ORM——sequelize模塊
ORM
對象關(guān)系映射,主要解決面向?qū)ο缶幊膛c關(guān)系型數(shù)據(jù)庫之間不匹配的問題。
ORM的特點
- 可以提高開發(fā)的效率
- 不用直接寫SQL語句
sequelize模塊——ORM的實現(xiàn)模塊
基于promise的關(guān)系型數(shù)據(jù)庫ORM框架,這個庫完全采用JavaScript開發(fā)并且能夠用在Node.JS環(huán)境中,易于使用,支持多SQL方言(dialect)。它當前支持MySQL、MariaDB、SQLite、PostgreSQL、Sql Server 數(shù)據(jù)庫。
sequelize的特色
- 強大的模型定義,支持虛擬類型。
- 支持完善的數(shù)據(jù)驗證,減輕前后端的驗證壓力。
- Sequelize的查詢非常全面和靈活。
sequelize的使用
數(shù)據(jù)庫內(nèi)容:數(shù)據(jù)庫名稱為spj,數(shù)據(jù)庫表為 users表;


1、安裝sequelize:npm install sequelize --->必須先安裝mysql的驅(qū)動模塊(npm install mysql);
2、連接數(shù)據(jù)庫:創(chuàng)建sequelize的對象;
//導入mysql模塊
const mysql = require('mysql2');
//導入sequelize模塊
const Sequelize = require('sequelize');
//創(chuàng)建sequelize對象,參數(shù)分別為:數(shù)據(jù)庫名稱,數(shù)據(jù)庫類型,密碼,配置
var MySequelize = new Sequelize('spj','root','929TJ813',{
host:'localhost',
port:3306,
dialect:'mysql', //數(shù)據(jù)庫類型
pool:{ //數(shù)據(jù)庫連接池
max:20, //最大連接對象的個數(shù)
min:5, //最小連接對象的個數(shù)
idle:1000 //最長等待時間,單位為毫秒
}
})
module.exports = MySequelize ; //導出創(chuàng)建的sequelize對象3、創(chuàng)建數(shù)據(jù)模型:數(shù)據(jù)模型是一個類,對應的是數(shù)據(jù)庫中一張表;
const Sequelize =require('sequelize')
const MySequesize = require('../config/dbconfig'); //導入創(chuàng)建的sequelize對象
//創(chuàng)建StudentModel模型,該模型對應的表名是student
var StudentModel = MySequesize.define('users',{
sid:{
type:Sequelize.INTEGER, //表示屬性的數(shù)據(jù)類型
field:'s_id', //屬性對應的列名,若不定義field則表中的列名(sid)就是屬性名
primaryKey:true, //表示主鍵
autoIncrement:true //表示主鍵自增
},
sname:{
type:Sequelize.STRING(50),
field: 's_name',
allowNull:false, //表示當前列是否允許為空,false表示該列不能為空
//unique:true //表示該列的值必須唯一
},
sgender:{
type:Sequelize.STRING(4),
field:'s_gender',
allowNull: false
},
sbirthday:{
type:Sequelize.DATE,
field:'s_birthday',
allowNull:false
},
saddress:{
type:Sequelize.STRING(100),
field:'s_address',
allowNull:false
},
sage:{
type:Sequelize.INTEGER,
field:'s_age',
allowNull:false
}
},{
freezeTableName:true, //true表示使用給定的表名,false表示模型名后加s作為表名
timestamps:false //true表示給模型加上時間戳屬性(createAt、updateAt),false表示不帶時間戳屬性
})
//同步數(shù)據(jù)庫,force的值為false,表若存在則先刪除后創(chuàng)建,force的值為true表示表若存在則不創(chuàng)建
var users = StudentModel.sync({force:false});
module.exports = StudentModel; //導出模型4、使用sequelize實現(xiàn)增刪改查 。
const StudentModel = require('../../db/model/StudentModel');
const Sequelize = require('sequelize')
//插入數(shù)據(jù)
StudentModel.create({
sname:'關(guān)羽',
sgender:'男',
sbirthday:'1998-12-28',
saddress:'陜西寶雞'
}).then(result=>{
console.log("插入成功!",result);
}).catch(err=>{
console.log("插入失敗!",err);
})
//查詢數(shù)據(jù)
StudentModel.findAll({
raw:true //查詢出來只有需要的數(shù)據(jù),沒有別的內(nèi)容
}).then(data=>{
console.log(data);
})
//刪除記錄
StudentModel.destroy({
where:{
sid:2
}
}).then(result=>{
console.log("刪除成功!",result)
}).catch(err=>{
console.log("刪除失??!",err);
})
//更新記錄
StudentModel.findOne({
where:{
sid:3
}
}).then(users=>{
users.update({
sname:'張飛',
sgender:'男'
}).then(result=>{
console.log("更新成功!",result)
}).catch(err=>{
console.log("更新失敗!",err);
})
}).catch(error=>{
console.log("查無此人!",error);
})
//查詢部分字段
StudentModel.findAll({
attributes:['sname','saddress'],
raw:true
}).then(result=>{
console.log(result);
}).catch(err=>{
console.log(err);
})
//聚合函數(shù)
StudentModel.findAll({
attributes:[[Sequelize.fn('COUNT',Sequelize.col('s_id')),"記錄總數(shù)"]], //col里面必須放的是列名
raw:true
}).then(result=>{
console.log(result)
})5、使用sequelize實現(xiàn)模糊查詢等內(nèi)容。
const StudentModel = require('../../db/model/StudentModel');
const Op = require('sequelize').Op; //引入sequelize模塊的Op操作符
//模糊查詢
StudentModel.findAll({
where:{
sname:{
[Op.like]:'張%'
}
},
raw:true
}).then(result=>{
console.log(result);
})
//between:查詢年齡在12到18之間的人的信息
StudentModel.findAll({
where:{
sage:{
[Op.between]:[12,18]
}
},
raw:true
}).then(result=>{
console.log(result);
})
//in:查詢地址是'陜西西安‘和'陜西商洛‘的記錄
StudentModel.findAll({
where:{
saddress:{
[Op.in]:['陜西西安','陜西商洛']
}
},
order:[
['sage','desc'] //查詢結(jié)果按照sage的降序排列
],
raw:true
}).then(result=>{
console.log(result);
})
//and和or:查詢性別為'男‘,并且地址是‘陜西寶雞'的記錄
StudentModel.findAll({
where:{
[Op.and]:[ //把and改為or即為或時間
{
sgender:{
[Op.eq]:'男'
}
},
{
saddress:{
[Op.eq]:'陜西寶雞'
}
}
]
},
raw:true
}).then(result=>{
console.log(result);
})
//limit和offset:分頁查詢
StudentModel.findAll({
limit:3, //查詢的記錄數(shù)
offset:1, //從索引值為幾的記錄開始查詢(查詢的起始位置),所有數(shù)據(jù)的索引值從0開始
raw:true
}).then(result=>{
console.log(result);
})總結(jié)
到此這篇關(guān)于數(shù)據(jù)庫連接池以及sequelize實現(xiàn)增刪改查等操作指南的文章就介紹到這了,更多相關(guān)數(shù)據(jù)庫連接池及sequelize增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL數(shù)據(jù)庫聚合查詢和聯(lián)合查詢詳解
聚合查詢就是在一個表里通過聚合函數(shù)進行查詢操作,通常是求和,求平均值等操作,這篇文章主要介紹了MySQL聚合查詢和聯(lián)合查詢的相關(guān)資料,需要的朋友可以參考下2024-03-03
MySQL 8.0 Online DDL快速加列的相關(guān)總結(jié)
在實際的MySQL運維過程中,我們經(jīng)常會遇到業(yè)務需要給某張表添加字段的情況,本文將介紹幾種加字段的方法,感興趣的朋友可以參考下2021-06-06

