Node.js学习11~基于Egg.js框架,对MySQL数据库实现增删改查操作

Posted 秋9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node.js学习11~基于Egg.js框架,对MySQL数据库实现增删改查操作相关的知识,希望对你有一定的参考价值。

目 录

1.安装egg-mysql

2.开启egg-mysql插件

3.创建表结构sql

4.egg配置MySQL

5.实现增删改查操作

5.1service层实现

5.2controller层实现

5.3router实现


1.安装egg-mysql

$ npm i --save egg-mysql

2.开启egg-mysql插件

config/plugin.js文件内容

'use strict';

/** @type Egg.EggPlugin */
module.exports =
 
// had enabled by egg
  // static:
  //   enable: true,
  //

;

 

修改为:

// config/plugin.js

'use strict';

module.exports =
   
//mysql
   
mysql:
       
enable:true,
       
package:'egg-mysql'
   

;

3.创建表结构sql

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) DEFAULT NULL,
  `pwd` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

4.egg配置MySQL

    config.mysql = 
      //database configuration
      client:
          //host
          host:'localhost',
          //port
          port:'3306',
          //username
          user:'root',
          //password
          password:'123456',
          //database
          database:'egg'
      ,
      //load into app,default is open //加载到应用程序,默认为打开
      app:true,
      //load into agent,default is close //加载到代理中,默认值为“关闭”
      agent:false,
  ;

5.实现增删改查操作

5.1service层实现

// app/service/user.js
'use strict';
const Service = require('egg').Service;
class UserService extends Service 
    async findAll() 
        const user = await this.app.mysql.query('select * from users', '');
        return  user ;
    
    async findById(id) 
        const user = await this.app.mysql.query('select * from users where id=?', id);
        return  user ;
    
    async insert(obj) 
        const user = await this.app.mysql.insert('users',obj)
        return  user ;
    
    async update(obj) 
        const user = await this.app.mysql.update('users',obj)
        return  user ;
    
    async deleteById(id) 
        const user = await this.app.mysql.delete('users',  id: id);
        return  user ;
    


module.exports = UserService;

5.2controller层实现

'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller 
  async index() 
        const  ctx  = this;
        const user = await ctx.service.user.find();
        ctx.body = user;
  
    async findAll() 
        const user = await this.ctx.service.user.findAll();
        this.ctx.body = user;
    
    async findOne() 
        const id = this.ctx.params
        const user = await this.ctx.service.user.findById(id);
        this.ctx.body = user;
    
    async insert() 
        const  name, pwd  = this.ctx.request.body
        const obj=
            "name":name,
            "pwd":pwd
        
        const user = await this.ctx.service.user.insert(obj);
        this.ctx.body = user;
    
    async update() 
        const  id,name, pwd  = this.ctx.request.body
        const obj=
            "id":id,
            "name":name,
            "pwd":pwd
        
        const user = await this.ctx.service.user.update(obj);
        this.ctx.body = user;
    
    async delete() 
        const id = this.ctx.params;
        const user = await this.ctx.service.user.deleteById(id);
        this.ctx.body = user;
    


module.exports = HomeController;

5.3router实现

'use strict';

/**
 * @param Egg.Application app - egg application
 */
module.exports = app => 
  const  router, controller  = app;
  router.get('/', controller.home.index);
    router.get('/findAll', controller.home.findAll);
    router.get('/findOne/:id', controller.home.findOne);
    router.post('/insert', controller.home.insert);
    router.post('/update', controller.home.update);
    router.get('/delete/:id', controller.home.delete);
;

以上是关于Node.js学习11~基于Egg.js框架,对MySQL数据库实现增删改查操作的主要内容,如果未能解决你的问题,请参考以下文章

基于 Egg.js 框架的 Node.js 服务构建之用户管理设计

Egg入门学习

egg.js 配置cors跨域

Egg.js 介绍以及环境搭建

企业级 Node.js 框架 - Egg.js 1.0.0 正式发布

egg.js整合socket.io