从零开始搭建⼀个node+koa2+mysql接⼝项⽬从零开始搭建⼀个node+koa2+mysql接⼝项⽬
初始化项⽬
1.初始化⼀个koa2项⽬
$ koa2 koa_mysql_demo
2.安装模块
$ npm install
3.启动服务
$ npm start
4.访问
创建成功
引⼊mysql
1.在package中引⼊mysql依赖
"md5": "^2.2.1",
"mysql": "^2.14.1",
2.新建db.js
内容
const database={
user:'root',
password:'',
port:3306,
host:'localhost',
database:'publishing_group_2019'
}
3.在app.js中require此模块
const mysql=require('./config/db.js')
4.新建⼀个utils⽂件夹,在⽂件夹中创建⼀个query.js⽤来执⾏sql语句
const mysql=require('mysql')
const mysql_connect=require('../config/db.js')
let pool = atePool(mysql_connect);
let query=(sql, values)=>{
return new Promise((resolve, reject)=>{
if(err){
reject(err)
}else{
connection.query(sql, values,(err, rows)=>{
if(err){
reject(err)
}else{
resolve(rows)
}
})
}
})
})
};
query
}
进⾏简单的sql查询操作
1.创建app⽬录下⾯创建四个⽂件夹
2.在⽂件夹下创建js⽂件
3.bookDao进⾏sql语句出查询
代码如下
const mysql=require('../../utils/query.js')
let getBooklist = name =>{
let _sql = `select * from fa_pg_book where deletetime is null`; return mysql.query(_sql);
};
getBooklist : getBooklist
};
4.bookimpl调⽤bookDao
代码如下
const bookdao=require('../dao/bookDao')
var getBooklist= async (userId)=>{
var booklist = Booklist();
return booklist;
}
getBooklist : getBooklist
};
代码如下
let router =require('koa-router')();
let apiModel =require('../service/bookimpl.js');
<('/book/list', async (ctx, next)=>{
await Promise.all([
])
.
then(res =>{
ctx.body ={
code:200,
data: res,
message:'获取图书列表成功',
};
})
.catch(err =>{
ctx.body ={
code:500,
message: err,
};
});
});
6.在app.js中添加路由
const books = require('./app/controller/bookController')
app.utes(), books.allowedMethods())
7.启动项⽬进⾏调⽤
localhost:3000/book/list
成功
8.⼀些查询的时候的错误
8.1 :字符串模糊查询出现的错误mysql下载之后是个文件夹
当模糊查询的时候
如果你在controller中传⼊参数
在dao中使⽤的时候
let getBooklist =value=> {
let _sql =`select*from fa_pg_book where deletetime is null and(title like'%?%')or title like'%?%')`;
return mysql.query(_sql,value);
};
会出现错误如下
正确的⽤法是使⽤字符串拼接concat
let getBooklist =value=> {
let _sql =`select*from fa_pg_book where deletetime is null and(title like CONCAT('%',?,'%')or title like CONCAT('%',?,'%'))`; return mysql.query(_sql,value);
};
8.2:时间格式转换
新建util.js
内容如下
let util ={
//时间戳转时间
getcurrenttimes(date){
var time = date;
// 也可以获取当前的毫秒级时间戳
var date =new Date(time *1000);
var dt = FullYear()+"-"+(Month()<10?'0'+(Month()+1):(Month()+1))+"-"+(Date()<10?'0'+ Date(): Date());
return dt;
},
}
在controller中调⽤
let timetran=require('../util/util')
<('/book/list',async(ctx, next)=>{
await Promise.all([
])
.then(res =>{
for(let i in res[0]){
//对时间数据进⾏处理
console.log(res[0][i]['createtime'])
res[0][i]['createtime']= urrenttimes(res[0][i]['createtime'])
}
if(res && res.length >0){
let val = res[0];
ctx.body ={
code:200,
data: val,
message:'获取图书列表成功',
}
}
})
.catch(err =>{
ctx.body ={
code:500,
message: err,
};
});
});
8.3 简单分页
在dao中
let getBooklistPage= value=>{
let _sql =`select title,createtime,author from fa_pg_book where deletetime is null limit ?,?`;
return mysql.query(_sql,value);
};
在service中
var getBooklistpage=async(value)=>{
var booklist = BooklistPage(value);
return booklist;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论