美文网首页
Mysql+Nodejs+Koa2+Vue+Quasar零起点教

Mysql+Nodejs+Koa2+Vue+Quasar零起点教

作者: 工程师54 | 来源:发表于2021-07-17 05:04 被阅读0次

一、概述

本示例项目的基本流程是:数据接口(mysql)-->服务接口(koa2)-->应用接口(quasar)。

本章节主要就是介绍服务接口的开发,是在koa2刚创建的项目里实现。

示例中koa2仅是实现服务接口,具体的业务逻辑都在数据接口的存储过程中实现了,其实在服务接口中也可以扩展业务逻辑,从而合理分配应用接口、服务接口、数据接口之间的计算资源。

二、接口清单

以下是示例中需要实现的服务接口清单,以及服务接口对应的访问地址:

1、登录校验接口

http://127.0.0.1:3000/interf/logincheck

2、登录账号分页查询接口

http://127.0.0.1:3000/interf/userpagelist

3、学生信息分页查询接口

http://127.0.0.1:3000/interf/studentpagelist

4、登录账号增删改接口

http://127.0.0.1:3000/interf/updateuser

5、学生信息增删改接口

http://127.0.0.1:3000/interf/updatestudent

6、获取登录者姓名接口

http://127.0.0.1:3000/interf/getloginname

三、涉及的文件

在koa2创建的项目基础上,服务接口开发涉及到三个文件:新增了两个文件config.js、mysqlModel.js,修改了已有的index.js文件。

四、config.js

本文件是mysql访问账号设置:

const conn_mysql = {   host: 'localhost',         // 服务器地址  
port: 3306,                 // 数据库端口号  
user: 'testuser1', // 数据库用户名  
password: '123456',   // 数据库密码  
database: 'test_db1',     // 数据库名称  
multipleStatements: true //允许多条sql同时执行
 };
 module.exports = conn_mysql ;

五、mysqlModel.js

本文件是mysql访问驱动程序,支持执行sql、执行存储过程两种方式:

const conn_mysql = require('../config')
const mysql = require('mysql');  
let pool = {};
 if (!pool.hasOwnProperty('data')) {    
    pool['data'] = mysql.createPool(conn_mysql);
 }
 const query = (sql, values) => {    
    return new Promise((resolve, reject) => {        
        pool['data'].getConnection((err, connection) => {            
            if (err) {                
                reject(err)            
            } else {                
                connection.query(sql, values, (err, rows) => {                    
                    if (err) {                        
                        reject(err)                    
                    } else {                        
                        resolve(rows)                    
                    }                    
                 connection.release()                
               })            
           }        
        })    
}) };
 const exepro = (sql) => {  
       return new Promise((resolve, reject) => {      
              pool['data'].getConnection((err, connection) => {          
                    if (err) {              
                           reject(err)          
                     } else {              
                           connection.query(sql, (err, rows, fields) => {                  
                               if (err) {                      
                                   reject(err)                   
                                } else {                      
                                  resolve(rows)                  
                             }                  
                connection.release()              
            })          
          }      
      })  
}) };
 module.exports = {     query,     exepro }

六、index.js

   改写现有index.js,增加了六个接口的路由代码:

const {query,exepro} =require('../models/mysqlModel')

const router = require('koa-router')()

router.get('/', async (ctx, next) => {

  awaitctx.render('index', {

   title: 'Hello 2!'

  })

})

//1、登录校验接口

.get('/interf/logincheck',async ctx => {

  constres =ctx.query;

  const{ username = '',userpwd=''} = res;

 if(username && userpwd){

   const queryData = `call sp_logincheck('${username}','${userpwd}')` ;

   const data = await exepro(queryData);

    ctx.body= {

       data,

    };

  };

})

//2、登录账号分页查询接口

.get('/interf/userpagelist',async ctx => {

  constres =ctx.query;

  const{ username,pageIndex,pageSize,sortBy,descending} = res;

  constqueryData = `call sp_userpagelist('${username}',${pageIndex},${pageSize},'${sortBy}',${descending},@rowscount,@pagescount,@errmsg)`;

  constdata = await exepro(queryData);

 ctx.body = {

       data,

  };

})

//3、学生信息分页查询接口

.get('/interf/studentpagelist',async ctx=> {

  constres =ctx.query;

  const{ studentname,studentsex,pageIndex,pageSize,sortBy,descending} = res;

  constqueryData = `callsp_studentpagelist('${studentname}','${studentsex}',${pageIndex},${pageSize},'${sortBy}',${descending},@rowscount,@pagescount,@errmsg)`;

  constdata = await exepro(queryData);

 ctx.body = {

       data,

  };

})

//4、登录账号增删改接口

.post('/interf/updateuser',async ctx => {

  constres =ctx.request.body;

  const{ optype,username,userpwd,userfullname} = res;

  constqueryData = `call sp_updateuser(${optype},'${username}','${userpwd}','${userfullname}')`;

  constdata = await exepro(queryData);

 ctx.body = {

       data,

  };

})

//5、学生信息增删改接口

.post('/interf/updatestudent',async ctx =>{

  constres =ctx.request.body;

  const{ optype,studentnum,studentname,studentsex,studentbirth} = res;

  constqueryData = `callsp_updatestudent(${optype},${studentnum},'${studentname}','${studentsex}','${studentbirth}')`;

  constdata = await exepro(queryData);

 ctx.body = {

       data,

  };

})

//6、获取登录者姓名接口

.get('/interf/getloginname',async ctx => {

  constres =ctx.query;

  const{ username = ''} = res;

 if(username){

   const queryData = [username];

   const data = await query('select user_fullname from t_login whereuser_name= ?', queryData );

   ctx.body = {

       data,

    };

  };

})

module.exports = router

相关文章

网友评论

      本文标题:Mysql+Nodejs+Koa2+Vue+Quasar零起点教

      本文链接:https://www.haomeiwen.com/subject/dfmvpltx.html