时间:2021-07-01 10:21:17 帮助过:4人阅读
2.services
user.js
const Service = require(‘egg‘).Service; class UserService extends Service { async create(user) { let {app}=this; let result = await app.mysql.insert(‘user‘, user ); return result; } } module.exports = UserService;
3.Controller
user.js
const Controller = require(‘egg‘).Controller; class UserController extends Controller { async create() { const { ctx,service} = this; let user =ctx.request.body; let result = await service.user.create(user); console.log(result); this.ctx.body=result; if(result.affectedRows === 1) { this.ctx.body ={ code:0, data:result.insertId } }else { this.ctx.body ={ code:1, data:‘用户添加失败‘ } } } } module.exports = UserController;
/ 判断插入成功
const insertSuccess = result.affectedRows === 1;
result的打印解果
OkPacket { fieldCount: 0, affectedRows: 1, insertId: 0, serverStatus: 2, warningCount: 0, message: ‘‘, protocol41: true, changedRows: 0 }
数据库添加数据成功
egg 连接mysql 在mysql 插入数据
标签:edr pac 插入 sql war password info app log