ORM(Object Relational Mapping,对象关系映射)

目录

一、ORM(Object Relational Mapping,对象关系映射)

1、持久化:将内存中的数据永久的保存到存储设备中(文件、数据库)

2、对象与关系(表)的映射

3、特点

二、通过ORM实现CRUD(增、删、改、查)

1、Node中通过Sequelize模块来实现ORM映射

2、Sequelize模块的使用方法

(1)安装:mysql2、Sequelize

(2)创建数据库的配置文件

(3)创建模型:实现orm的映射,一张表对应一个模型文件

(4)进行CRUD操作


一、ORM(Object Relational Mapping,对象关系映射)

1、持久化:将内存中的数据永久的保存到存储设备中(文件、数据库)

2、对象与关系(表)的映射

(1)类 <---> 表

(2)属性 <---> 列

(3)对象 <---> 行

3、特点

(1)提高开发效率:开发者不用在面向对象和关系型数据库之间进行来回的切换

(2)不用在程序中编写sql语句

二、通过ORM实现CRUD(增、删、改、查)

1、Node中通过Sequelize模块来实现ORM映射

2、Sequelize模块的使用方法

(1)安装:mysql2、Sequelize

(2)创建数据库的配置文件

//导入sequelize模块
const Sequelize = require('sequelize');

//配置数据库连接对象(默认带有数据库连接池):数据库名、用户名、密码、配置对象
const sequelize = new Sequelize('dbms','root','8945632',{
    host: 'localhost',
    port: 3306,
    dialect: 'mysql', //数据库类型
    pool:{
        max: 5,
        min: 0,
        idle: 10000 //若某个线程10秒没有使用,就释放
    },
    debug: true  //显示调试信息
})

module.exports = sequelize;

(3)创建模型:实现orm的映射,一张表对应一个模型文件

const Sequelize = require('sequelize');
//导入配置文件
const db = require('../config/mydb');

/*
定义模型:
  (1)类名和表名的映射:可以同名
  (2)类的属性和表的列的映射:
     A、属性名和列名相同:不需要指定列名,自动将属性名映射为列名
     B、属性名和列名不相同:要在映射时通过fields来指定列名
*/
const Employee = db.define('db_employee',{
    id:{  //属性名可以和列名相同,也可以不同
        type: Sequelize.INTEGER, //属性的数据类型,对应的是列的数据类型
        primaryKey: true, //表示该属性对应的是表的主键列
        autoIncrement: true, //设置为自增
        field: 'empid' //映射列名
    },
    name: {
        type: Sequelize.STRING(30),
        allowNull: false, //表示不允许为空
        //unique: true //表示值必须唯一
        field: 'empname'
    },
    gender: {
        type: Sequelize.STRING(4),
        allowNull: false
    },
    birthday: {
        type: Sequelize.DATE,
        allowNull: false
    },
    phone: {
        type: Sequelize.STRING(13),
        allowNull: false
    },
    address: {
        type: Sequelize.STRING(100),
        allowNull: false
    }
},{
    freezeTableName: true, //映射表名的设置:true表示使用用户给定的表名,false表示MySQL自动生成表名(为类名后加s)
    timestamps: false //是否自动生成时间戳列(createAt列、updateAt列),false表示不生成
});

module.exports = Employee;

①类名和表名的映射:可以同名

②类的属性和表的列的映射

Ⅰ属性名和列名相同:不需要指定列名,自动将属性名映射为列名

Ⅱ属性名和列名不相同:要在映射时通过fields来指定列名

(4)进行CRUD操作

查询:findAll、findOne

删除:destroy

插入:create

更新:update

const express = require('express');
const EmployeeModel = require('../db/model/employeeModel');

const router = express.Router();

/*
  查询所有:http://localhost:3000/emp/all
*/
router.get('/all',(req, res) => {
    EmployeeModel.findAll({
        raw: false //不显示时间戳列
    }).then((results)=>{
        res.send(results);
    })
})

/*
  删除记录:http://localhost:3000/emp/remove
*/
router.delete('/remove',(req, res) => {
    let eid = req.body.empid  //empid是前端传输过来,可以随意设置
    EmployeeModel.destroy({
        where: { //相当于sql语句中的where子句
            id: eid    //这里的id指的是employeeModel中的id属性,而不是表里的id属性
        }
    }).then((result)=>{
        res.send({
            code: result,
            msg: '删除成功'
        })
    }).catch((err)=>{
        console.log(err)
        res.send({
            code: 1000,
            msg: '删除失败'
        })
    })
})
/*
   插入数据:http://localhost:3000/emp/insert
*/
router.post('/insert',(req, res) => {
    //1.获取参数
    let ename = req.body.emp_name;
    let egender = req.body.emp_gender;
    let ebirthday = req.body.emp_birthday;
    let ephone = req.body.emp_phone;
    let eaddress = req.body.emp_address

    //2.调用create方法插入数据
    EmployeeModel.create({
        name: ename,
        gender: egender,
        birthday: ebirthday,
        phone: ephone,
        address: eaddress
    }).then((result)=>{
        res.send({
            code: result,
            msg: '插入成功'
        })
    }).catch(err=>{
        console.log(err)
        res.send({
            code: 1000,
            msg: '插入失败'
        })
    })
})
/*
  更新记录:http://localhost:3000/emp/update
*/
router.put('/update',(req, res) => {
    let eid = req.body.emp_id;
    let ename = req.body.emp_name;
    let ebirthday  = req.body.emp_birthday;
    let eaddress = req.body.emp_address;

    EmployeeModel.findOne({
        where: {
            id: eid
        }
    }).then((emp)=>{
        emp.update({
            name: ename,
            birthday: ebirthday,
            address: eaddress
        }).then((result)=>{
            res.send({
                code: result,
                msg: '更新成功'
            })
        }).catch(err=>{
            console.log(err)
            res.send({
                code: 1000,
                msg: '更新失败'
            })
        })
    }).catch(err=>{
        console.log(err)
        res.send({
            code: 1000,
            msg: '查无此人'
        })
    })
})
module.exports = router;

注意:req.body.(属性)应该与前端传输过来的属性一致,可随意设置

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值