这里注意,主要用到了sequlize 框架和mysql2 驱动sql语句。
其中的返回值是复杂对象,所以需要转换
所有的sequelize支持的表模型的增删改查方法,返回值均为promise ,这里的处理均为await关键字和async修饰符。
以下是案例:
const student = require("../model/student");
//分页查询
exports.getStudent =async (page=1,num=10)=>{
let result = await student.findAndCountAll({
offset:(page-1)*num,
limit:+num,
});
result = JSON.parse(JSON.stringify(result)) ;
return result;
}
//添加数据
exports.addStu=async (obj)=>{
try {
let result = await student.create(obj);
return result.toJSON();
} catch (error) {
console.log(error);
}
}
//删除数据
exports.deleteStu=async (id)=>{
try {
let result = await student.destroy({
where:{
stuId:id
}
});
console.log(result);
return result;
} catch (error) {
console.log(error);
}
}
//更改数据 根据某个字段
exports.updateStu = async (obj,id)=>{
let result = await student.update(obj,{
where: {
id
}
});
return result
}
更多方法可以查看官网: