最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1. vue創(chuà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.在項目根目錄下創(chuàng)建server文件夾:

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

// 將vue-resource模塊引入進來,否則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字符串進行處理
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進行交互界面簡單編寫

// 在路由中引入新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ù)庫進行CURD操作的詳細(xì)內(nèi)容,更多關(guān)于Vue Node直連Mysql進行CURD操作的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Vue項目請求超時處理方式

    Vue項目請求超時處理方式

    這篇文章主要介紹了Vue項目請求超時處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • vue.js加載新的內(nèi)容(實例代碼)

    vue.js加載新的內(nèi)容(實例代碼)

    vue是一種輕巧便捷的框架,那么如何進行對于數(shù)據(jù)加載的刷新呢?以下就是我對于vue.js數(shù)據(jù)加載的一點想法
    2017-06-06
  • Vue3中使用reactive時,后端有返回數(shù)據(jù)但dom沒有更新的解決

    Vue3中使用reactive時,后端有返回數(shù)據(jù)但dom沒有更新的解決

    這篇文章主要介紹了Vue3中使用reactive時,后端有返回數(shù)據(jù)但dom沒有更新的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • vue3使用watch監(jiān)聽props的值的注意事項及說明

    vue3使用watch監(jiān)聽props的值的注意事項及說明

    這篇文章主要介紹了vue3使用watch監(jiān)聽props的值的注意事項及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • Vue3 elementUI 中 date-picker 使用過程遇到坑

    Vue3 elementUI 中 date-picker 使用過程遇到坑

    這篇文章主要介紹了Vue3 elementUI 中 date-picker 使用過程遇到坑,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-10-10
  • vue各種事件監(jiān)聽實例(小結(jié))

    vue各種事件監(jiān)聽實例(小結(jié))

    這篇文章主要介紹了vue各種事件監(jiān)聽實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • vue實現(xiàn)圖片上傳到后臺

    vue實現(xiàn)圖片上傳到后臺

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)圖片上傳到后臺,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Vue 組件注冊全解析

    Vue 組件注冊全解析

    這篇文章主要介紹了Vue 組件注冊全解析的相關(guān)資料,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • Vuex之module使用方法及場景說明

    Vuex之module使用方法及場景說明

    這篇文章主要介紹了Vuex之module使用方法及場景說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 用Vue.js方法創(chuàng)建模板并使用多個模板合成

    用Vue.js方法創(chuàng)建模板并使用多個模板合成

    在本篇文章中小編給大家分享了關(guān)于如何使用Vue.js方法創(chuàng)建模板并使用多個模板合成的相關(guān)知識點內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-06-06

最新評論

海伦市| 三原县| 北宁市| 黔江区| 赞皇县| 阿克陶县| 马龙县| 台东市| 荣昌县| 石门县| 长子县| 邵东县| 南汇区| 增城市| 台中市| 巴中市| 温宿县| 巩义市| 咸丰县| 汉阴县| 宝清县| 孝昌县| 增城市| 泽普县| 玛多县| 祁门县| 镇赉县| 荣成市| 阜城县| 苗栗县| 永靖县| 天等县| 大港区| 维西| 赤水市| 腾冲县| 嵊泗县| 徐水县| 金湖县| 长治市| 城口县|