node js 连接数据库 (sequelize mysql2)

sequelize的使用

1.安装sequelize:npm install sequelize 必须先安装mysql的驱动模块(npm install mysql2)

注:一定要安装 mysql2 否则后面连接数据库 会报错

2.连接数据库:创建sequelize的对象

const sequelize = require('sequelize')
// 数据库配置项
const config = {
    host: "127.0.0.1",//本机ip
    username: "root",//数据库用户名
    password: "root",//数据库密码
    prot:3306,//数据库端口
    database: "shooping",//数据库名称
    dialect: "mysql",//数据库类型
    pool:{  //数据库连接池
        max:20,  //最大连接对象的个数
        min:5,  //最小连接对象的个数
        idle:1000  //最长等待时间,单位为毫秒
    }
}
//创建sequelize对象
const Mysequelize = new sequelize(config)
module.exports = Mysequelize

3.创建数据模型

const Sequelize =require('sequelize')
const Mysequelize = require('../config/index')

//创建数据模型:数据模型是一个类,对应的是数据库中一张表
// Sequelize 支持的数据类型
// Sequelize.STRING // VARCHAR(255)
// Sequelize.STRING(1234) // VARCHAR(1234)
// Sequelize.INTEGER //INT
const shopModel = Mysequelize.define('testshooping',{
    shopName:{
        type:Sequelize.STRING,  //表示属性的数据类型
        // field:'s_id',   //属性对应的列名,若不定义field则表中的列名(sid)就是属性名
        primaryKey:false,  //表示主键
        autoIncrement:false  //表示主键自增
    },
    shopPrice:{
        type:Sequelize.STRING,  //表示属性的数据类型
        // field:'s_id',   //属性对应的列名,若不定义field则表中的列名(sid)就是属性名
        primaryKey:false,  //表示主键
        autoIncrement:false  //表示主键自增
    },
    id:{
        type:Sequelize.INTEGER,  //表示属性的数据类型
        // field:'s_id',   //属性对应的列名,若不定义field则表中的列名(sid)就是属性名
        primaryKey:true,  //表示主键
        autoIncrement:true  //表示主键自增
    },
},
{
    freezeTableName:true, //true表示使用给定的表名,false表示模型名后加s作为表名
    timestamps:false  //true表示给模型加上时间戳属性(createAt、updateAt),false表示不带时间戳属性
})

module.exports = shopModel  

3.进行增删改查

const shopModel = require('./model/shoppingModel')

// 使用sequelize实现增删改查
// 增加
shopModel.create({
    shopName:'HUAWEI HONOR',
    shopPrice:'6000'
}).then(res=>{
    console.log('插入成功',res);
}).catch(err=>{
    console.log('插入失败',err);
})

// 删除 
shopModel.destroy({
    where:{
        id:2
    }
}).then(result=>{
    console.log("删除成功!",result)
}).catch(err=>{
    console.log("删除失败!",err);
})

// 修改
shopModel.findOne({
    where:{
        id:1
    }
}).then(textShop=>{
    textShop.update({
        shopPrice:'12000'
    }).then(result=>{
        console.log("更新成功!",result)
    }).catch(err=>{
        console.log("更新失败!",err);
    })
}).catch(error=>{
    console.log("无数据!",error);
})
// 查询
shopModel.findAll({
    // attributes:['shopPrice'],
    raw:true
}).then(result=>{
    console.log(result);
}).catch(err=>{
    console.log(err);
})

// 分页查询
shopModel.findAll({
    limit:3,   //查询的记录数
    offset:1,  //从索引值为几的记录开始查询(查询的起始位置),所有数据的索引值从0开始
    raw:true
}).then(result=>{
    console.log(result);
})

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值