數(shù)據(jù)庫(kù)連接池以及sequelize實(shí)現(xiàn)增刪改查等操作指南
數(shù)據(jù)庫(kù)連接池
介紹數(shù)據(jù)庫(kù)連接池
數(shù)據(jù)庫(kù)連接池負(fù)責(zé)分配、管理和釋放數(shù)據(jù)庫(kù)連接,它允許應(yīng)用程序重復(fù)使用一個(gè)現(xiàn)有的數(shù)據(jù)庫(kù)連接,而不是重新建立一個(gè);釋放空閑時(shí)間超過最大空閑時(shí)間的數(shù)據(jù)庫(kù)連接來(lái)避免因?yàn)闆]有釋放數(shù)據(jù)庫(kù)連接而引起的數(shù)據(jù)庫(kù)連接遺漏。
通俗的理解就是: 數(shù)據(jù)庫(kù)連接池是程序啟動(dòng)時(shí)建立足夠數(shù)量的數(shù)據(jù)庫(kù)連接對(duì)象,并將這些連接對(duì)象組成一個(gè)池,由程序動(dòng)態(tài)的對(duì)池中的連接對(duì)象進(jìn)行申請(qǐng)、使用和釋放。
優(yōu)點(diǎn)
(1)避免應(yīng)用程序頻繁的連接、斷開數(shù)據(jù)庫(kù)
(2)提供數(shù)據(jù)庫(kù)連接對(duì)象的使用頻率。
使用方法
(1)創(chuàng)建數(shù)據(jù)庫(kù)連接池:
mysql.createPool(config)
host:數(shù)據(jù)庫(kù)服務(wù)器的地址
port: 端口號(hào)
user:連接數(shù)據(jù)庫(kù)的用戶名
password:連接數(shù)據(jù)庫(kù)的密碼
database:數(shù)據(jù)庫(kù)名
connectionLimit:用于指定連接池中最大的鏈接數(shù),默認(rèn)屬性值為10.
multipleStatements :是否允許執(zhí)行多條sql語(yǔ)句,默認(rèn)值為false
(2)從連接池中獲取一個(gè)連接
連接池名.getConnection(function(err,connection){ 執(zhí)行的代碼 }) //參數(shù)err:錯(cuò)誤對(duì)象。連接失敗后的錯(cuò)誤信息 //參數(shù)connection:連接對(duì)象。若連接失敗,它就是undefined
(3)釋放連接對(duì)象(將連接對(duì)象放回連接池): connection.release();
(4)從連接池中移除連接對(duì)象: connection.destory();
(5)關(guān)閉該連接池: 連接池名.end();
數(shù)據(jù)庫(kù)訪問中的ORM——sequelize模塊
ORM
對(duì)象關(guān)系映射,主要解決面向?qū)ο缶幊膛c關(guān)系型數(shù)據(jù)庫(kù)之間不匹配的問題。
ORM的特點(diǎn)
- 可以提高開發(fā)的效率
- 不用直接寫SQL語(yǔ)句
sequelize模塊——ORM的實(shí)現(xiàn)模塊
基于promise的關(guān)系型數(shù)據(jù)庫(kù)ORM框架,這個(gè)庫(kù)完全采用JavaScript開發(fā)并且能夠用在Node.JS環(huán)境中,易于使用,支持多SQL方言(dialect)。它當(dāng)前支持MySQL、MariaDB、SQLite、PostgreSQL、Sql Server 數(shù)據(jù)庫(kù)。
sequelize的特色
- 強(qiáng)大的模型定義,支持虛擬類型。
- 支持完善的數(shù)據(jù)驗(yàn)證,減輕前后端的驗(yàn)證壓力。
- Sequelize的查詢非常全面和靈活。
sequelize的使用
數(shù)據(jù)庫(kù)內(nèi)容:數(shù)據(jù)庫(kù)名稱為spj,數(shù)據(jù)庫(kù)表為 users表;
1、安裝sequelize:npm install sequelize --->必須先安裝mysql的驅(qū)動(dòng)模塊(npm install mysql);
2、連接數(shù)據(jù)庫(kù):創(chuàng)建sequelize的對(duì)象;
//導(dǎo)入mysql模塊 const mysql = require('mysql2'); //導(dǎo)入sequelize模塊 const Sequelize = require('sequelize'); //創(chuàng)建sequelize對(duì)象,參數(shù)分別為:數(shù)據(jù)庫(kù)名稱,數(shù)據(jù)庫(kù)類型,密碼,配置 var MySequelize = new Sequelize('spj','root','929TJ813',{ host:'localhost', port:3306, dialect:'mysql', //數(shù)據(jù)庫(kù)類型 pool:{ //數(shù)據(jù)庫(kù)連接池 max:20, //最大連接對(duì)象的個(gè)數(shù) min:5, //最小連接對(duì)象的個(gè)數(shù) idle:1000 //最長(zhǎng)等待時(shí)間,單位為毫秒 } }) module.exports = MySequelize ; //導(dǎo)出創(chuàng)建的sequelize對(duì)象
3、創(chuàng)建數(shù)據(jù)模型:數(shù)據(jù)模型是一個(gè)類,對(duì)應(yīng)的是數(shù)據(jù)庫(kù)中一張表;
const Sequelize =require('sequelize') const MySequesize = require('../config/dbconfig'); //導(dǎo)入創(chuàng)建的sequelize對(duì)象 //創(chuàng)建StudentModel模型,該模型對(duì)應(yīng)的表名是student var StudentModel = MySequesize.define('users',{ sid:{ type:Sequelize.INTEGER, //表示屬性的數(shù)據(jù)類型 field:'s_id', //屬性對(duì)應(yīng)的列名,若不定義field則表中的列名(sid)就是屬性名 primaryKey:true, //表示主鍵 autoIncrement:true //表示主鍵自增 }, sname:{ type:Sequelize.STRING(50), field: 's_name', allowNull:false, //表示當(dāng)前列是否允許為空,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表示給模型加上時(shí)間戳屬性(createAt、updateAt),false表示不帶時(shí)間戳屬性 }) //同步數(shù)據(jù)庫(kù),force的值為false,表若存在則先刪除后創(chuàng)建,force的值為true表示表若存在則不創(chuàng)建 var users = StudentModel.sync({force:false}); module.exports = StudentModel; //導(dǎo)出模型
4、使用sequelize實(shí)現(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 //查詢出來(lái)只有需要的數(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("查無(wú)此人!",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實(shí)現(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]:[ //把a(bǔ)nd改為or即為或時(shí)間 { sgender:{ [Op.eq]:'男' } }, { saddress:{ [Op.eq]:'陜西寶雞' } } ] }, raw:true }).then(result=>{ console.log(result); }) //limit和offset:分頁(yè)查詢 StudentModel.findAll({ limit:3, //查詢的記錄數(shù) offset:1, //從索引值為幾的記錄開始查詢(查詢的起始位置),所有數(shù)據(jù)的索引值從0開始 raw:true }).then(result=>{ console.log(result); })
總結(jié)
到此這篇關(guān)于數(shù)據(jù)庫(kù)連接池以及sequelize實(shí)現(xiàn)增刪改查等操作指南的文章就介紹到這了,更多相關(guān)數(shù)據(jù)庫(kù)連接池及sequelize增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql alter table 修改表命令詳細(xì)介紹
MYSQL ALTER TABLE命令用于修改表結(jié)構(gòu),例如添加/修改/刪除字段、索引、主鍵等等,本文章通過實(shí)例向大家介紹MYSQL ALTER TABLE語(yǔ)句的使用方法,需要的朋友可以參考一下。2016-10-10

MySQL 8.0 Online DDL快速加列的相關(guān)總結(jié)

MySQL中因一個(gè)雙引號(hào)錯(cuò)位引發(fā)的血案詳析