egg mysql 项目实战,egg.js创建项目,目录介绍,简单使用,sequelize mysql使用
1、egg項目的創建
創建項目 npm init egg --type=simple
安裝依賴 npm i
依賴安裝完成后運行命令 npm run dev 即可運行項目
參考官方文檔 https://eggjs.org/zh-cn/intro/quickstart.html
2、egg目錄介紹
egg-project
├── package.json
├── app.js (可選)
├── agent.js (可選)
├── app
| ├── router.js
│ ├── controller // C 控制器,邏輯都在這邊實現
│ | └── home.js
│ ├── service (可選) // M 數據庫獲取數據在這邊實現
│ | └── user.js
│ ├── middleware (可選)
│ | └── response_time.js
│ ├── model (可選) // 數據庫模型放著
│ ├── schedule (可選)
│ | └── my_task.js
│ ├── public (可選)
│ | └── reset.css
│ ├── view (可選) // V 頁面渲染
│ | └── home.tpl
│ └── extend (可選)
│ ├── helper.js (可選)
│ ├── request.js (可選)
│ ├── response.js (可選)
│ ├── context.js (可選)
│ ├── application.js (可選)
│ └── agent.js (可選)
├── config
| ├── plugin.js
| ├── config.default.js
│ ├── config.prod.js
| ├── config.test.js (可選)
| ├── config.local.js (可選)
| └── config.unittest.js (可選)
└── test
├── middleware
| └── response_time.test.js
└── controller
└── home.test.js
如上,由框架約定的目錄:
app/router.js 用于配置 URL 路由規則,具體參見 Router。
app/controller/** 用于解析用戶的輸入,處理后返回相應的結果,具體參見 Controller。
app/service/** 用于編寫業務邏輯層,可選,建議使用,具體參見 Service。
app/middleware/** 用于編寫中間件,可選,具體參見 Middleware。
app/public/** 用于放置靜態資源,可選,具體參見內置插件 egg-static。
app/extend/** 用于框架的擴展,可選,具體參見框架擴展。
config/config.{env}.js 用于編寫配置文件,具體參見配置。
config/plugin.js 用于配置需要加載的插件,具體參見插件。
test/** 用于單元測試,具體參見單元測試。
app.js 和 agent.js 用于自定義啟動時的初始化工作,可選,具體參見啟動自定義。關于agent.js的作用參見Agent機制。
由內置插件約定的目錄:
app/public/** 用于放置靜態資源,可選,具體參見內置插件 egg-static。
app/schedule/** 用于定時任務,可選,具體參見定時任務。
3、簡單的使用
Service 寫法
// app/service/user.js
const Service = require('egg').Service;
class UserService extends Service {
async find(uid) {
// 下面這句就是數據庫獲取數據
const user = await this.ctx.db.query('select * from user where uid = ?', uid);
return user;
}
}
module.exports = UserService;
Controller 寫法
// app/controller/user.js
const Controller = require('egg').Controller;
class UserController extends Controller {
async getUserList() {
const { ctx, service } = this;
// 調用 Service 進行業務處理
const res = await service.user.find(ctx.query.uid);
// 設置響應內容和響應狀態碼
ctx.body = { res };
ctx.status = 201;
}
async delUser() {
// ...............
}
}
module.exports = UserController;
Router 寫法
// app/router.js
module.exports = app => {
const { router, controller } = app;
router.get('/user', controller.user.getUserList);
router.post('/user/del', controller.user.delUser);
};
4、sequelize,mysql的使用
安裝 npm install --save egg-sequelize mysql2
然后啟用插件( 在 config/plugin.js 中引入 egg-sequelize 插件)
// config/plugin.js
exports.sequelize = {
enable: true,
package: 'egg-sequelize',
};
接著配置數據庫信息(在 config/config.default.js 中添加 sequelize 配置)
'use strict';
/**
* @param {Egg.EggAppInfo} appInfo app info
*/
module.exports = appInfo => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = exports = {};
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1595165791967_5765';
// add your middleware config here
config.middleware = [];
config.sequelize = {
dialect: 'mysql',
host: 'localhost',
port: 3306,
database: 'shop',
username: 'root',
password: '123456',
};
// add your user config here
const userConfig = {
// myAppName: 'egg',
};
return {
...config,
...userConfig,
};
};
然后配置數據庫模型,在 /app的目錄下 建立 model文件夾,然后在 model文件夾建立一個 model文件( 如 /app/model/user.js )
'use strict';
module.exports = app => {
const { STRING, INTEGER, TINYINT } = app.Sequelize;
const User = app.model.define('users',
{
id: {
type: INTEGER(10), // 數據類型
allowNull: false, // 是否為 null
primaryKey: true, // 是否為 主鍵
autoIncrement: true, // 是否 自動填值
},
username: {
type: STRING(100),
allowNull: false,
defaultValue: '', // 默認值
},
age: {
type: TINYINT,
allowNull: false,
defaultValue: 0, // 默認值
},
password: {
type: STRING(100),
allowNull: false,
defaultValue: '',
},
},
{
freezeTableName: true, // Model 對應的表名將與model名相同
timestamps: false,
}
);
return User;
};
最后就是使用(比如在service/user.js)
'use strict';
const Service = require('egg').Service;
class userService extends Service {
async getUserAllList() {
const { ctx } = this;
// 注意 ctx.model.User 中的 User 是 model 下 user.js 的文件的首字母大寫的名稱
return await ctx.model.User.findAll();
}
}
module.exports = userService
表關聯參考 https://blog.csdn.net/weixin_39066827/article/details/107106472
本文地址:https://blog.csdn.net/john_xiaoweige/article/details/107499974
如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!
總結
以上是生活随笔為你收集整理的egg mysql 项目实战,egg.js创建项目,目录介绍,简单使用,sequelize mysql使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab功能块的作用,STEP7中功
- 下一篇: php response body,数据