MySQL + express脚手架

数据库

(记得打开MySQL)
可视化工具

  1. 新建连接

    #点击新建连接 选择MySQL
    
  2. 新建数据库

    #打开新建的连接 右键新建数据库
    #字符集选择utf8
    #排序规则选择utf8_general_ci
    
  3. 新建数据表

    #id 是int 值为非空键为自增 (注意不能有空行)
    
  4. 修改字段属性

    #右键设计表 可以编辑字段
    
  5. 设置主外键约束

    #右键设计表有主外键约束
    
  6. SQL文件可以这样写

    -- 设置客户端连接服务器端编码
    set names utf8;
    -- 丢弃数据库如果存在
    drop database if exists checkup;
    -- 创建数据库,设置存储字符的编码为UTF-8
    create database checkup charset=utf8;
    -- 进入数据库
    use checkup;
    
    -- 入职登记信息表
    CREATE TABLE `entry` (
      `id` int AUTO_INCREMENT PRIMARY KEY, -- 员工编号
      `part` varchar(16) NOT NULL, -- 部门
      `marry_state` char(2), -- 婚姻状态
      `enumber` varchar(18) NOT NULL, -- 身份证号
      `ephone` char(11), -- 电话
    ); 
    
  7. SQL的增删改查

    -- 向数据表中插入数据
    INSERT INTO student VALUES (99, 9999, '猪八戒', NULL);
    -- 批量插入
    INSERT INTO student VALUES
    	(101, 10001, "孙悟空", '11111'),
        (101, 10002, '沙悟净', 'hello');
    -- 指定字段插入
    INSERT INTO student (sn, id, name, qq_mail) VALUES 
    	(102, 20001, '曹孟德', null)
    -- 查询表里所有数据
    select * from exam_result;
    -- 查询表中某个或某些字段
    select id, name, english from exam_result;
    -- 带表达式查询
    SELECT id, name, id * 10 FROM exam_result;
    select avg(math) from exam_result;
    -- 分页查询
    select * from exam_result order by id limit 2 offset 0;
    
    -- 改
    update 表明 set 字段名 = '修改的值' where 条件;
    
    -- 删
    delete from 表名 where 条件;
    delete from 表名 where 条件;
    

导出SQL表

开启Apache (stat)

选择MySQL的admin

打开数据库 全选 导出SQL文件

使用express脚手架建立项目

https://www.expressjs.com.cn/

  1. 运行 Express 应用程序生成器。

    npx express-generator
    #如果版本较老
    npm install -g express-generator
    express
    
  2. 安装所以依赖的包

    cd myapp
    npm install
    
  3. 启动

    npm start
    # 使用pm2 但是不会自动更新
    pm2 start 文件名
    pm2 start bin/www
    
  4. 目录结构

    .
    ├── app.js
    ├── bin
    │   └── www
    ├── package.json
    ├── public
    │   ├── images
    │   ├── javascripts
    │   └── stylesheets
    │       └── style.css
    ├── routes
    │   ├── index.js
    │   └── users.js
    └── views
        ├── error.pug
        ├── index.pug
        └── layout.pug
    

app.js

var express = require('express')
var path = require('path')
var logger = require('morgan')
// 引入跨域模块
const cors = require('cors')
//引入路由器
var indexRouter = require('./routes/index')
// 创建WEB服务器
var app = express()

// 解决跨域
app.use(cors())
// 记录日志
app.use(logger('dev'))
// 将post传参过程中的json转为对象
app.use(express.json())
// 将post传参转为对象
app.use(express.urlencoded({ extended: false }))
// 托管静态资源,使用了绝对路径
app.use(express.static(path.join(__dirname, 'public')))

// 挂载路由器
app.use('/', indexRouter)

// 错误处理
app.use((err, req, res, next) => {
  // 打印错误信息
  console.log(err)
  res.send({ code: 500, msg: '服务器端错误' })
})

module.exports = app

connection.js

// 引入mysql模块
const mysql =require('mysql')
// 创建连接模块
const connection = mysql.createConnection({
	host:'127.0.0.1',
	port:'3306',
	user:'root',
	password:'',
	database:'checkup',
	multipleStatements:true // 是否可以连接多个
})
// 暴露连接对象
module.exports=connection

package.json

{
  "name": "node.js",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "cors": "^2.8.5",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "jade": "~1.11.0",
    "joi": "^17.9.2",
    "jsonwebtoken": "^9.0.0",
    "morgan": "~1.9.1",
    "mysql": "^2.18.1",
    "pm2": "^5.3.0"
  }
}

子路由

// 引入express模块
const express = require('express')
// 引入数据连接模块
const connection = require('../../connection')
const router = express.Router()

// 查询科室信息
// http://localhost:3000/hospital/department/search
router.get('/search', (req, res, next) => {
  var obj = req.query
  console.log(obj)
  connection.query('select * from department', (err, r) => {
    if (err) {
      return next(err)
    }
    res.send({ code: 200, msg: '查询科室信息', data: r })
  })
})
// 查询科室名称
// http://localhost:3000/hospital/department/search_name
router.get('/search_name', (req, res, next) => {
  var obj = req.query
  console.log(obj)
  connection.query('select name from department', [obj.id], (err, r) => {
    if (err) {
      return next(err)
    }
    res.send({ code: 200, msg: '查询科室名称', data: r })
  })
})
// 查询科室名称
// http://localhost:3000/hospital/department/search_top
router.get('/search_top', (req, res, next) => {
  var obj = req.query
  console.log(obj)
  connection.query('select * from superior_department', [obj.id], (err, r) => {
    if (err) {
      return next(err)
    }
    res.send({ code: 200, msg: '查询科室名称', data: r })
  })
})

// 新增科室信息
// http://localhost:3000/hospital/department/add
router.post('/add', (req, res, next) => {
  var obj = req.body
  console.log(obj)
  connection.query('insert into department set ?', [obj], (err, r) => {
    if (err) {
      return next(err)
    }
    res.send({ code: 200, msg: '新增科室信息', data: obj })
  })
})

// 根据ID删除科室
// http://localhost:3000/hospital/department/delete
router.delete('/delete', (req, res, next) => {
  var obj = req.query
  console.log(obj)
  connection.query('DELETE FROM department WHERE id=?', [obj.id], (err, r) => {
    if (err) {
      return next(err)
    }
    res.send({ code: 200, msg: '根据ID删除科室', data: obj })
  })
})

// 根据ID修改科室内容
// http://localhost:3000/hospital/department/update
router.put('/update', (req, res, next) => {
  var obj = req.body
  console.log(obj)
  connection.query(
    'UPDATE department  SET ? WHERE id = ?',
    [obj, obj.id],
    (err, r) => {
      if (err) {
        return next(err)
      }
      res.send({ code: 200, msg: '根据ID修改科室内容', data: obj })
    }
  )
})

// 暴露路由器
module.exports = router
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值