安装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);
创建
- 创建
const data = await ctx.model.Student.create({
name: "student-1",
sex: "男",
birthday: "1989-01-01"
});
查询
- 查询所有
const data = await ctx.model.Student.findAll();
// 等同于
// SELECT * FROM student
- 查询一条
const data = await ctx.model.Student.findAll();
// 等同于
// SELECT * FROM student
网友评论