【express+Mysql DML、DQL+数据建模】express基于Node.js平台开发的Web开发框架

学习目标:
express
mysql DML
数据建模

目录

1 express

1.1 helloworld

 1.2 路由定义与使用

 1.3 路由参数

1.4response方法

 1.5 中间件

2 脚手架

 3 sql-ddl

3.1 数据库定义

3.2 表定义

3.3 表修改

4 sql-dml

4.1 查询语句

4.2 插入语句

4.3 修改语句

4.4 删除语句


1 express

express是基于 Node.js 平台,快速、开放、极简的 Web 开发框架。


1.1 helloworld

手动创建工程

$ mkdir myapp
$ cd myapp
$ npm init
$ npm install express --save

核心代码

const express = require('express')
const app = express()
const port = 3000
app.use(express.static('public'))
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})

在浏览器中访问

 1.2 路由定义与使用

定义了针对客户端请求的处理函数,不同uri对应不同的处理函数。通过Express的
实例对象app上的请求方法定义不同的路由,例如:get、post、delete、put、all...
在当前项目内创建routes/users.js文件

 内容如下

var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function (req, res, next) {
res.send('respond with a resource');
});
module.exports = router;

在main.js中使用users路由

const express = require('express')
const usersRouter = require('./routes/users'); //注意这里
const app = express()
const port = 3000
app.use(express.static('public'))
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.use('/users', usersRouter); //注意这里
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})

 重新启动项目,访问/路由和/users路由。

 
1.3 路由参数

Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }
Route path: /users
Request URL: http://localhost:3000/users?id=1&name=terry
req.query: { "id": "1", "name": "terry" }

1.4response方法

 1.5 中间件

body-parser
cookie-parser
cors
serve-static

2 脚手架

通过应用生成器工具 express-generator 可以快速创建一个应用的⻣架。

$ mkdir cms
$ cd cms
$ npx express-generator

 3 sql-ddl

3.1 数据库定义

mysql> CREATE DATABASE poll;
mysql> USE poll
Database changed

3.2 表定义

create table <表名>( <列名><数据类型>[列级完整约束条件]
[, <列名><数据类型>[列级完整约束条件]]
... [,<表级完整性约束条件>]
);
DROP TABLE IF EXISTS `tbl_exam_paper`;
CREATE TABLE `tbl_exam_paper` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`description` varchar(255) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`department_id` bigint(20) DEFAULT NULL,
`user_id` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `FK92534C8DF2C32590` (`department_id`),
CONSTRAINT `FK92534C8DF2C32590` FOREIGN KEY (`department_id`)
REFERENCE
)

3.3 表修改

#添加列
mysql> alter table tableName add columnName dataType
#删除列
mysql> alter table tableName drop columnName dataType
#修改列
mysql> alter table tableName modify columnName dataType
#删除数据库表
mysql> drop table tableName
#清空数据库表内容
mysql> truncate table s_item

4 sql-dml

用于访问和处理数据库的标准的计算机语言。

4.1 查询语句

mysql> select [all|distinct] <目标列表达式>
-> from <表或视图>[, <表或视图>
-> [where <条件表达式>]
-> [group by <列名1>
-> [having<条件表达式>]]
-> [order by <列名2>[asc|desc]]

单表查询

elect name
from tbl_student
where gender = 'male';

多表连接查询

select s.name,c.name
from tbl_student s, tbl_student_course sc ,tbl_course c
where s.id = sc.student_id
and c.id = sc.course_id;

4.2 插入语句

insert into tbl_student(id,name,gender)
values (null,'terry','gender')

4.3 修改语句

update tbl_student
set name = '王五'
where id = 1;

4.4 删除语句

delete from tbl_user
where id = 1;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值