12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
建站问答 时间:2024-11-30 15:58:11
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
在egg.js中使用MySQL的步骤如下:首先安装MySQL的驱动包egg-mysql,可以通过npm安装:$ npm install egg-mysql --save在config/plugin.js配置文件中启用MySQL插件:exports.mysql =
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
在egg.js中使用MySQL的步骤如下:
egg-mysql
,可以通过npm安装:$ npm install egg-mysql --save
config/plugin.js
配置文件中启用MySQL插件:exports.mysql = {enable: true,package: 'egg-mysql',};
config/config.default.js
配置文件中配置MySQL连接信息:exports.mysql = {client: {host: 'localhost',port: '3306',user: 'root',password: 'password',database: 'test',},};
const Controller = require('egg').Controller;class UserController extends Controller {async index() {const { ctx } = this;const user = await this.app.mysql.get('user', { id: 1 });ctx.body = user;}async create() {const { ctx } = this;const result = await this.app.mysql.insert('user', { name: 'Alice', age: 18 });ctx.body = result;}async update() {const { ctx } = this;const result = await this.app.mysql.update('user', { id: 1, age: 20 });ctx.body = result;}async destroy() {const { ctx } = this;const result = await this.app.mysql.delete('user', { id: 1 });ctx.body = result;}}module.exports = UserController;
这样就可以在Egg.js中使用MySQL进行数据库操作了。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19