egg-sequelize增删改查操作笔记

安装egg-sequelize

npm install egg-sequelize mysql2 --save // 下载mysql驱动插件
npm install sequelize-cli --save-dev // 下载sequelize管理工具

引入sequelize

config/plugin.js 中引入 egg-sequelize 插件

'use strict';

/** @type Egg.EggPlugin */
module.exports = {
  sequelize:{
    enable:true,
    package:"egg-sequelize"
  }
};

在 config/config.default.js 中编写 sequelize 配置

# 这里连接的是存服务数据的数据库
const config = exports = {
    sequelize: {
      dialect: "mysql",
      host: "localhost", // 数据库地址
      port: '3306',
      database: "test-db", // 数据库名称
      username: "root",
      password: "",
      timezone: "+08:00", // 东八时区
      dialectOptions: {
        useUTC: false // 输出为东八时区,但是不管用
      }
    }
  };

初始化sequelize

  • 创建相应数据库
  • 在 egg 项目中,我们希望将所有数据库 Migrations 相关的内容都放在 database 目录下,所以我们在项目根目录下新建一个 .sequelizerc 配置文件:
# 指定目录地址
'use strict';

const path = require('path');

module.exports = {
  config: path.join(__dirname, 'database/config.json'),
  'migrations-path': path.join(__dirname, 'database/migrations'),
  'seeders-path': path.join(__dirname, 'database/seeders'),
  'models-path': path.join(__dirname, 'app/model'),
};
  • 创建config.js、 migrations、seeders、models
# 创建sequelize配置数据库
npx sequelize init:config
# 创建migrations文件夹
npx sequelize init:migrations
# 创建种子数据文件夹
npx sequelize init:seeders
# 创建model文件夹 
npx sequelize init:models

sequelize增删改查操作

  • 以 学生分数表为案例进行增删改查
  • 创建表
-- 学生表
CREATE TABLE `student` (
  `stuId`      int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',
  `name` varchar(10) not NULL COMMENT '名字',
  `sex`  char(1) not NULL COMMENT '性别',
  `birthday` DATE not NULL COMMENT '出生年月日',
  PRIMARY KEY (`stuId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into student(stuId,name,birthday,sex) values('0001' , '猴子' , '1989-01-01' , '男');
insert into student(stuId,name,birthday,sex) values('0002' , '猴子' , '1990-12-21' , '女');
insert into student(stuId,name,birthday,sex) values('0003' , '马云' , '1991-12-21' , '男');
insert into student(stuId,name,birthday,sex) values('0004' , '王思聪' , '1990-05-20' , '男');

-- 课程表
CREATE TABLE `course` (
    `courseId`     int(11) not null AUTO_INCREMENT comment '课程号',
    `cname` varchar(10) not null comment '课程名',
    `teaId`   int(11) not null comment '教师号',
    PRIMARY KEY (`courseId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into course(courseId,cname,teaId)values('0001' , '语文' , '0002');
insert into course(courseId,cname,teaId)values('0002' , '数学' , '0001');
insert into course(courseId,cname,teaId)values('0003' , '英语' , '0003');

-- 教师表
CREATE TABLE `teacher` (
    `teaId` int(11) not null AUTO_INCREMENT comment '教师号',
    `tname` varchar(10) not null comment '教师名',
     PRIMARY KEY (`teaId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into teacher(teaId,tname) values('0001' , '孟扎扎');
insert into teacher(teaId,tname) values('0002' , '马化腾');
insert into teacher(teaId,tname) values('0003' , null);
insert into teacher(teaId,tname) values('0004' , '');

-- 成绩表
CREATE TABLE `score` (
    `scoreId` int(11) not null AUTO_INCREMENT comment '课程号',
    `stuId` int(11) not null comment '学号',
    `score` float(3) not null comment '成绩',
    PRIMARY KEY (`scoreId`,`stuId`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into score(stuId,scoreId,score) values('0001' , '0001' , 80);
insert into score(stuId,scoreId,score) values('0001' , '0002' , 90);
insert into score(stuId,scoreId,score) values('0001' , '0003' , 99);
insert into score(stuId,scoreId,score) values('0002' , '0002' , 60);
insert into score(stuId,scoreId,score) values('0002' , '0003' , 80);
insert into score(stuId,scoreId,score) values('0003' , '0001' , 80);
insert into score(stuId,scoreId,score) values('0003' , '0002' , 80);
insert into score(stuId,scoreId,score) values('0003' , '0003' , 80);

创建

  1. 创建
const data = await ctx.model.Student.create({
        name: "student-1",
        sex: "男",
        birthday: "1989-01-01"
      });

查询

  1. 查询所有
const data = await ctx.model.Student.findAll();
      // 等同于
      // SELECT * FROM student
  1. 查询一条
const data = await ctx.model.Student.findAll();
      // 等同于
      // SELECT * FROM student
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一人创客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值