Vue整合Node.js直連Mysql數(shù)據(jù)庫進(jìn)行CURD操作過程詳解
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)拖拽穿梭框功能四種方式,使用原生js實(shí)現(xiàn)拖拽,VUe使用js實(shí)現(xiàn)拖拽穿梭框,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09基于node+vue實(shí)現(xiàn)簡單的WebSocket聊天功能
最近學(xué)習(xí)了一下websocket的即時通信,感覺非常的強(qiáng)大,這里我用node啟動了一個服務(wù)進(jìn)行websocket鏈接,然后再vue的view里面進(jìn)行了鏈接,進(jìn)行通信,廢話不多說,直接上代碼吧2020-02-02vue.js組件vue-waterfall-easy實(shí)現(xiàn)瀑布流效果
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)瀑布流之vue-waterfall-easy的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Nuxt.js nuxt-link與router-link的區(qū)別說明
這篇文章主要介紹了Nuxt.js nuxt-link與router-link的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11vant-list上拉加載onload事件觸發(fā)多次問題及解決
這篇文章主要介紹了vant-list上拉加載onload事件觸發(fā)多次問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01vue子組件改變父組件傳遞的prop值通過sync實(shí)現(xiàn)數(shù)據(jù)雙向綁定(DEMO)
本文通過一個demo給大家介紹了vue子組件改變父組件傳遞的prop值通過sync實(shí)現(xiàn)數(shù)據(jù)雙向綁定,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02