数据库连接池以及sequelize实现增删改查等操作指南


当前第2页 返回上一页

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

//导入mysql模块
const mysql = require('mysql2');
//导入sequelize模块
const Sequelize = require('sequelize');
//创建sequelize对象,参数分别为:数据库名称,数据库类型,密码,配置
var MySequelize = new Sequelize('spj','root','929TJ813',{
    host:'localhost',
    port:3306,
    dialect:'mysql',   //数据库类型
    pool:{  //数据库连接池
        max:20,  //最大连接对象的个数
        min:5,  //最小连接对象的个数
        idle:1000  //最长等待时间,单位为毫秒
    }
})
module.exports = MySequelize ;   //导出创建的sequelize对象

3、创建数据模型:数据模型是一个类,对应的是数据库中一张表;

const Sequelize =require('sequelize')
const MySequesize = require('../config/dbconfig');  //导入创建的sequelize对象
//创建StudentModel模型,该模型对应的表名是student
var StudentModel = MySequesize.define('users',{
    sid:{
        type:Sequelize.INTEGER,  //表示属性的数据类型
        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表示不带时间戳属性
})
//同步数据库,force的值为false,表若存在则先删除后创建,force的值为true表示表若存在则不创建
var users = StudentModel.sync({force:false});
 
module.exports = StudentModel;   //导出模型

4、使用sequelize实现增删改查 。

const StudentModel = require('../../db/model/StudentModel');
const Sequelize = require('sequelize')
//插入数据
StudentModel.create({
    sname:'关羽',
    sgender:'男',
    sbirthday:'1998-12-28',
    saddress:'陕西宝鸡'
}).then(result=>{
    console.log("插入成功!",result);
}).catch(err=>{
    console.log("插入失败!",err);
})
 
//查询数据
StudentModel.findAll({
    raw:true   //查询出来只有需要的数据,没有别的内容
}).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);
})
 
//聚合函数
StudentModel.findAll({
    attributes:[[Sequelize.fn('COUNT',Sequelize.col('s_id')),"记录总数"]],  //col里面必须放的是列名
    raw:true
}).then(result=>{
    console.log(result)
})

5、使用sequelize实现模糊查询等内容。

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']   //查询结果按照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,   //查询的记录数
    offset:1,  //从索引值为几的记录开始查询(查询的起始位置),所有数据的索引值从0开始
    raw:true
}).then(result=>{
    console.log(result);
})

总结

到此这篇关于数据库连接池以及sequelize实现增删改查等操作指南的文章就介绍到这了,更多相关数据库连接池及sequelize增删改查内容请搜索


标签:SQLite

返回前面的内容

相关阅读 >>

射手播放器字幕保存路径怎么修改

filezilla无法启动文件传输错误的原因是什么?

node.js中实现同步操作的3种实现方法

Sqlite3 使用总结

详解ios四种保存数据的方式

c#操作Sqlite数据库之读写数据库的方法

Sqlite 入门教程一 基本控制台(终端)命令

python Sqlite的row对象操作示例

c#中实现在32位、64位系统下自动切换不同的Sqlite dll文件

基于java实现一个简单的单词本android app的实践

更多相关阅读请进入《Sqlite》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...