欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue整合Node.js直連Mysql數(shù)據(jù)庫進(jìn)行CURD操作過程詳解

 更新時間:2023年07月18日 10:54:34   作者:豆苗學(xué)前端  
這篇文章主要給大家分享Vue整合Node.js,直連Mysql數(shù)據(jù)庫進(jìn)行CURD操作的詳細(xì)過程,文中有詳細(xì)的代碼講解,具有一定的參考價值,需要的朋友可以參考下

1. vue創(chuàng)建項(xiàng)目

vue create xxxx

2. 安裝相應(yīng)的依賴

引入需要的模塊(mysql、express、body-parser、cors、vue-resource)

cnpm install mysql --save
cnpm install express --save
cnpm install body-parser --save
cnpm install cors --save
cnpm install vue-resource --save

3.在項(xiàng)目根目錄下創(chuàng)建server文件夾:

3.1 在src目錄下,main.js中使用vue-resource模塊:

// 將vue-resource模塊引入進(jìn)來,否則this.$http將無法使用
import VueResource from 'vue-resource'
Vue.use(VueResource);

3.2 在server目錄下,編寫database.js:

// 配置數(shù)據(jù)庫相關(guān)信息

// 配置數(shù)據(jù)庫相關(guān)信息
module.exports = {
  mysql: {
    host: "localhost",
    user: "root",
    password: "root",
    database: "test",
    port: 3306
  }
};

3.3 在server目錄下,編寫sql.js:

// 編寫相關(guān)sql語句
let sqlMap = {
  user: {
    // 查詢數(shù)據(jù)
    select: "select * from user where username = ?;",
    // 插入數(shù)據(jù)
    insert: "insert into user values(?,?);",
    // 修改數(shù)據(jù)
    update: "update user set password = ? where username = ?;",
    // 刪除數(shù)據(jù)
    delete: "delete from user where username = ? and password = ?;"
  }
};
// 暴露sqlMap
module.exports = sqlMap;

3.4 在server目錄下,編寫api.js

// 引入database.js文件(配置數(shù)據(jù)庫相關(guān)信息)
let models = require('./database');
// 引入sql.js文件(編寫相關(guān)sql語句)
let $sql = require('./sql');
// 引入mysql和express模塊
let mysql = require('mysql');
let express = require('express');
let router = express.Router();
// 開始連接數(shù)據(jù)庫
let conn = mysql.createConnection(models.mysql);
conn.connect();
// 對JSON字符串進(jìn)行處理
let jsonWrite = function (response, result) {
  if (typeof result === 'undefined') {
    response.json({
      code: '500',
      msg: '操作失敗'
    });
  } else {
    response.json(result);
  }
};
// 查詢數(shù)據(jù)('/select')
router.post('/select', (request, response) => {
  // 獲取編寫相關(guān)sql語句
  let sql = $sql.user.select;
  console.log("相關(guān)sql語句:", sql);
  // 獲取請求參數(shù)
  let params = request.body;
  console.log("相關(guān)參數(shù)", params);
  // [params.username]傳參類似mybatis
  conn.query(sql, [params.username], function (error, result) {
    if (error) {
      console.log('網(wǎng)絡(luò)連接異常:' + error);
    }
    if (result) {
      jsonWrite(response, result);
      for (let i = 0; i < result.length; i++) {
        if (result[i].password == params.password) {
          return response.end();
        } else {
          return response.end();
        }
      }
    }
  })
});
// 插入數(shù)據(jù)('/insert')
router.post('/insert', (request, response) => {
  // 獲取編寫相關(guān)sql語句
  let sql = $sql.user.insert;
  console.log("相關(guān)sql語句:", sql);
  // 獲取請求參數(shù)
  let params = request.body;
  console.log("相關(guān)參數(shù)", params);
  // [params.username]傳參類似mybatis
  conn.query(sql, [params.username, params.password], function (error, result) {
    if (error) {
      console.log('網(wǎng)絡(luò)連接異常:' + error);
    }
    if (result) {
      jsonWrite(response, result);
    }
  })
});
// 修改數(shù)據(jù)('/update')
router.post('/update', (request, response) => {
  // 獲取編寫相關(guān)sql語句
  let sql = $sql.user.update;
  console.log("相關(guān)sql語句:", sql);
  // 獲取請求參數(shù)
  let params = request.body;
  console.log("相關(guān)參數(shù)", params);
  // [params.username]傳參類似mybatis
  conn.query(sql, [params.password, params.username], function (error, result) {
    if (error) {
      console.log('網(wǎng)絡(luò)連接異常:' + error);
    }
    if (result) {
      jsonWrite(response, result);
    }
  })
});
// 刪除數(shù)據(jù)('/delete')
router.post('/delete', (request, response) => {
  // 獲取編寫相關(guān)sql語句
  let sql = $sql.user.delete;
  console.log("相關(guān)sql語句:", sql);
  // 獲取請求參數(shù)
  let params = request.body;
  console.log("相關(guān)參數(shù)", params);
  // [params.username]傳參類似mybatis
  conn.query(sql, [params.username, params.password], function (error, result) {
    if (error) {
      console.log('網(wǎng)絡(luò)連接異常:' + error);
    }
    if (result) {
      jsonWrite(response, result);
    }
  })
});
// 暴露router
module.exports = router;

3.5 在serve文件夾下,寫入server.js

// 引入api.js
const userApi = require('./api');
// 引入express模塊
const express = require('express');
const app = express();
// 引入cors模塊,處理跨域問題
const cors = require('cors');
app.use(cors());
// 引入body-parser模塊,解析請求過來的參數(shù)
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
// 訪問Node服務(wù)端映射地址
app.use('/api', userApi);
// 開始服務(wù)器端口
app.listen(8088);
console.log("服務(wù)器端口開啟成功!");

4. 編寫demo2.vue進(jìn)行交互界面簡單編寫

// 在路由中引入新demo2.vue頁面
import demo2 from "@/components/demo2";
{
   path: '/demo2',
   name: 'demo2',
   component: demo2,
}
<template>
  <div style="margin-left: 100px">
    <h3>數(shù)據(jù)庫增刪改查(CURD操作)</h3>
    <label>用戶名:<input type="text" v-model="username"/></label><br/><br/>
    <label>密碼:<input type="password" v-model="password"/></label><br/><br/>
    <el-button type="primary" @click="selectData">查詢</el-button>
    <el-button type="primary" @click="insertData">增加</el-button>
    <el-button type="primary" @click="updateData">修改</el-button>
    <el-button type="primary" @click="deleteData">刪除</el-button>
  </div>
</template>
<script>
  export default {
    name: "demo2",
    data() {
      return {
        username: '',
        password: '',
      }
    },
    methods: {
      selectData() {
        // 訪問node.js服務(wù)端(參數(shù)名稱:要和api.js中[params.屬性名]一致,請求方式也應(yīng)該保持一致)
        this.$http.post('http://localhost:8088/api/select', {
          username: this.username
        }).then(data => {
          if (data.data[0].username == this.username && data.data[0].password == this.password) {
            this.$message({type: 'success', message: "查詢成功"});
          } else {
            this.$message({type: 'warning', message: "查詢失敗"});
          }
        }).catch(error => {
          this.$message({type: 'error', message: "網(wǎng)絡(luò)連接異常"});
        });
      },
      insertData() {
        // 訪問node.js服務(wù)端(參數(shù)名稱:要和api.js中[params.屬性名]一致,請求方式也應(yīng)該保持一致)
        this.$http.post('http://localhost:8088/api/insert', {
          username: this.username,
          password: this.password
        }).then(data => {
          if (data.data.affectedRows > 0) {
            this.$message({type: 'success', message: "新增成功"});
          } else {
            this.$message({type: 'warning', message: "新增失敗"});
          }
        }).catch(error => {
          this.$message({type: 'error', message: "網(wǎng)絡(luò)連接異常"});
        });
      },
      updateData() {
        // 訪問node.js服務(wù)端(參數(shù)名稱:要和api.js中[params.屬性名]一致,請求方式也應(yīng)該保持一致)
        this.$http.post('http://localhost:8088/api/update', {
          username: this.username,
          password: this.password
        }).then(data => {
          if (data.data.affectedRows > 0) {
            this.$message({type: 'success', message: "修改成功"});
          } else {
            this.$message({type: 'warning', message: "修改失敗"});
          }
        }).catch(error => {
          this.$message({type: 'error', message: "網(wǎng)絡(luò)連接異常"});
        });
      },
      deleteData() {
        // 訪問node.js服務(wù)端(參數(shù)名稱:要和api.js中[params.屬性名]一致,請求方式也應(yīng)該保持一致)
        this.$http.post('http://localhost:8088/api/delete', {
          username: this.username,
          password: this.password
        }).then(data => {
          if (data.data.affectedRows > 0) {
            this.$message({type: 'success', message: "刪除成功"});
          } else {
            this.$message({type: 'warning', message: "刪除失敗"});
          }
        }).catch(error => {
          this.$message({type: 'error', message: "網(wǎng)絡(luò)連接異常"});
        });
      }
    }
  }
</script>
<style scoped>
</style>

啟動后端

5. 測試:

以上就是Vue整合Node.js直連Mysql數(shù)據(jù)庫進(jìn)行CURD操作的詳細(xì)內(nèi)容,更多關(guān)于Vue Node直連Mysql進(jìn)行CURD操作的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Vue實(shí)現(xiàn)拖拽穿梭框功能四種方式實(shí)例詳解

    Vue實(shí)現(xiàn)拖拽穿梭框功能四種方式實(shí)例詳解

    這篇文章主要介紹了Vue實(shí)現(xiàn)拖拽穿梭框功能四種方式,使用原生js實(shí)現(xiàn)拖拽,VUe使用js實(shí)現(xiàn)拖拽穿梭框,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • 詳解Vue computed計(jì)算屬性是什么

    詳解Vue computed計(jì)算屬性是什么

    在vue中,有時候你需要對data中的數(shù)據(jù)進(jìn)行處理,或者對抓取的數(shù)據(jù)進(jìn)行處理之后再掛載呈現(xiàn)到標(biāo)簽中,這時候你就需要計(jì)算屬性了,當(dāng)然看到這里你可能還是不了解那下面我舉幾個實(shí)例并附代碼解釋
    2023-03-03
  • 基于node+vue實(shí)現(xiàn)簡單的WebSocket聊天功能

    基于node+vue實(shí)現(xiàn)簡單的WebSocket聊天功能

    最近學(xué)習(xí)了一下websocket的即時通信,感覺非常的強(qiáng)大,這里我用node啟動了一個服務(wù)進(jìn)行websocket鏈接,然后再vue的view里面進(jìn)行了鏈接,進(jìn)行通信,廢話不多說,直接上代碼吧
    2020-02-02
  • vue.js組件vue-waterfall-easy實(shí)現(xiàn)瀑布流效果

    vue.js組件vue-waterfall-easy實(shí)現(xiàn)瀑布流效果

    這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)瀑布流之vue-waterfall-easy的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Nuxt.js nuxt-link與router-link的區(qū)別說明

    Nuxt.js nuxt-link與router-link的區(qū)別說明

    這篇文章主要介紹了Nuxt.js nuxt-link與router-link的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • vue開發(fā)自定義的全局公共組件詳解

    vue開發(fā)自定義的全局公共組件詳解

    本文介紹了如何開發(fā)自定義全局公共組件的兩種方法,第一種方法是通過在components文件夾中創(chuàng)建新的組件文件夾,例如Loading文件夾,并在其中創(chuàng)建index.js和index.vue文件,通過在vue的入口文件main.js中進(jìn)行引入,可以實(shí)現(xiàn)組件的全局調(diào)用
    2024-10-10
  • vue中使用protobuf的過程記錄

    vue中使用protobuf的過程記錄

    由于目前公司采用了ProtoBuf做前后端數(shù)據(jù)交互,進(jìn)公司以來一直用的是公司大神寫好的基礎(chǔ)庫,完全不了解底層是如何解析的。下面小編給大家分享vue中使用protobuf的過程記錄,需要的朋友參考下吧
    2018-10-10
  • vant-list上拉加載onload事件觸發(fā)多次問題及解決

    vant-list上拉加載onload事件觸發(fā)多次問題及解決

    這篇文章主要介紹了vant-list上拉加載onload事件觸發(fā)多次問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Vue3插槽(slot)使用方法詳解

    Vue3插槽(slot)使用方法詳解

    在VUE開發(fā)項(xiàng)目的過程中,插槽<slot>是重要的承載分發(fā)內(nèi)容的出口,下面這篇文章主要給大家介紹了關(guān)于Vue3插槽(slot)使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • vue子組件改變父組件傳遞的prop值通過sync實(shí)現(xiàn)數(shù)據(jù)雙向綁定(DEMO)

    vue子組件改變父組件傳遞的prop值通過sync實(shí)現(xiàn)數(shù)據(jù)雙向綁定(DEMO)

    本文通過一個demo給大家介紹了vue子組件改變父組件傳遞的prop值通過sync實(shí)現(xiàn)數(shù)據(jù)雙向綁定,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02

最新評論