后端链接数据库

This commit is contained in:
aixianling
2022-12-30 11:53:48 +08:00
parent cfaef6713b
commit cf5f60a049
8 changed files with 97 additions and 1 deletions

View File

@@ -0,0 +1,9 @@
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
this.ctx.body = 'Hello world';
}
}
module.exports = HomeController;

4
server/app/router.js Normal file
View File

@@ -0,0 +1,4 @@
module.exports = (app) => {
const { router, controller } = app;
router.get('/', controller.home.index);
};

31
server/app/service/db.js Normal file
View File

@@ -0,0 +1,31 @@
const Service = require("egg").Service
class DbService extends Service {
async addOrUpdate(table, form) {
let result
if (!form.id) {//创建
result = await this.app.mysql.insert(table, form)
} else {//更新
result = await this.app.mysql.update(table, form)
}
return result === 1
}
async delete(table, id) {
await this.app.mysql.delete(table, {id})
}
async detail(table, id) {
return await this.app.mysql.get(table, {id})
}
async list(table, params = {}) {
return await this.app.mysql.select(table, {
where: params,
limit: params?.size || 10, // 返回数据量
offset: Math.max(params?.current - 1, 0), // 数据偏移量
})
}
}
module.exports = DbService