用yarn安装egg-mysql
时间: 2024-05-16 18:13:10 浏览: 5
可以使用以下命令在项目中使用 yarn 安装 egg-mysql:
```
yarn add egg-mysql
```
安装完成后,在 Egg.js 项目中,可以通过以下方式进行使用:
1. 在 `config/plugin.js` 文件中配置插件:
```
exports.mysql = {
enable: true,
package: 'egg-mysql',
};
```
2. 在 `config/config.default.js` 文件中配置数据库连接信息:
```
exports.mysql = {
// database configuration
client: {
// host
host: 'localhost',
// port
port: '3306',
// username
user: 'root',
// password
password: '123456',
// database
database: 'test',
},
// load into app, default is open
app: true,
// load into agent, default is close
agent: false,
};
```
3. 在 Controller 或 Service 中使用:
```
const result = await this.app.mysql.get('table_name', { id: 1 });
console.log(result);
```