文章目录
一、数据库(Sequelize)
安装
npm install --save egg-sequelize mysql2
配置
plugin.js中引入
sequelize:{
enable:true,
package:'egg-sequelize'
},
配置config.default.js
config.sequelize = {
dialect:'mysql',
database:"school_system",
host:'localhost',
port:3306,
username:'root',
password:'123456'
};
Mysql和sequelize数据类型对应关系
STRING => varchar(255)
INTEGER => int
DOUBLE => double
DATE => datetime
TEXT => text
创建表
在app/model目录下创建clazz.js,对应clazz表
module.exports = app =>{
const {STRING} = app.Sequelize;
const Clazz = app.model.define('clazz',{
//自动生成id
name:STRING,
})
return Clazz;
}
在根目录下创建app.js
module.exports = app =>{
app.beforeStart(async function() {
//sync方法会根据模型创建表
await app.model.sync({});
});
};
增删查改
增
async create(){
let name = this.ctx.request.body.name;
await this.app.model.Clazz.create({
name:name
})
this.ctx.body = "添加成功"
}
删
async destroy(){
let id = this.ctx.params.id;
await this.app.model.Clazz.destroy({
where:{
id:id
}
})
this.ctx.body = "删除班级数据"
}
查
async index(){
let id = this.ctx.request.query.id;
let clazzList = await this.app.model.Clazz.findAll({
where:{
id:id
}
});
this.ctx.body = clazzList;
}
改
async update(){
let id = this.ctx.params.id;
let name = this.ctx.request.body.name
await this.app.model.Clazz.update({name:name},{
where:{
id:id
}
})
this.ctx.body = "修改成功"
}
一对多关系
例如学生和班级
先定义学生模型,在model下新建student.js
module.exports = app =>{
const {STRING} = app.Sequelize;
const Student = app.model.define('Student',{
//自动生成id
name:STRING,
})
//所属哪个班级,指向班级主键
Student.associate = function() {
app.model.Student.belongsTo(app.model.Clazz,{
foreignKey:'clazz_id',
as:'clazz'
})
}
return Student;
}
增
async create(){
let name = this.ctx.request.body.name;
let clazz_id = this.ctx.request.body.clazz_id;
await this.app.model.Student.create({
name:name,
clazz_id:clazz_id
})
this.ctx.body = "添加成功"
}
二、Service(服务层)
在app/service目录下创建js文件
//增加了异常处理
const {Service} = require("egg");
class StudentService extends Service{
async getStudentList(){
try{
let studentList = await this.app.model.Student.findAll();
return studentList;
}catch(e){
return null;
}
}
}
module.exports = StudentService;
/controller/student.js
async index(){
let list = await this.ctx.service.student.getStudentList();
if(list){
this.ctx.body = {
code:1,
data:list
};
}else{
this.ctx.body = {
code:0,
msg:"服务器异常,请与管理员联系"
}
}
}
总结
controller层获取用户输入,使用service层操作数据库
三、实际开发中的问题
请求的url
在vue项目根目录下创建 .env.development 文件
VUE_APP_BASE_API = "http://127.0.0.1:7001"
在vue项目的src目录下新建utils文件夹,新建request.js
import axios from "axios";
const request = axios.create({
baseURL:process.env.VUE_APP_BASE_API
})
export default request;
vue文件不在引入axios,而引入request.js文件
import request from "../utils/request"
//axios.get('http://127.0.0.1:7001/login')
request.get(`/login`)
token
将token放在封装的request.js中
import axios from "axios";
const request = axios.create({
baseURL:process.env.VUE_APP_BASE_API
})
//拦截器
request.interceptors.request.use((req)=>{
let token = localStorage.getItem("token");
if(token){
req.headers.token = token;
}
return req
})
export default request;