• Sequelize框架:


    1.安装mysql支持:

    npm install mysql --save-dev 

    2.安装sequelize

    npm install sequelize --save-dev

    3.创建ORM对象

    var Sequelize = require('sequelize');
    var sequelize = new Sequelize('kameng', 'root', 'root', {
    host: '127.0.0.1',
    dialect: 'mysql'
    });
    

      

    4.操作数据库

    var router = require('koa-router')();
    var Sequelize = require('sequelize');
    var sequelize = new Sequelize('kameng', 'root', 'root', {
    host: '127.0.0.1',
    dialect: 'mysql'
    });
    // 可以分开写,下面的
    var Users = sequelize.define('users', {
    //id: {type:Sequelize.BIGINT,primaryKey: true},
    //uid:{type:Sequelize.BIGINT,primaryKey: true},
    email: Sequelize.STRING,
    pwd: Sequelize.STRING,
    nicheng: Sequelize.STRING,
    createtime:Sequelize.DATE,
    updtime:Sequelize.DATE,
    role:Sequelize.INTEGER
    },{
    timestamps: false,
    //paranoid: true //获取不到id的返回值
    });

    5.在路由下添加如下代码

    router.get('/', function *(next) {
    let user = {
    email: 'bb',
    pwd: 'bb',
    nicheng: 'bb',
    updtime:new Date(),
    role:0
    };
    // Users.create(user).then(function(result){
    // console.log(result);
    // console.log(user);
    // }).catch(function(err){
    // console.log('例外');
    // console.log(err.message);
    // });
    try{
    let rs = yield Users.create(user);
    console.log('成功');
    console.log(rs);
    console.log(rs.dataValues.id);
    }catch(err){
    console.log(err.errors[0].message); //识别唯一键
    }
    this.body='koa1';
    // yield this.render('index', {
    // title: 'Hello World Koa!'
    // });
    });
    
    module.exports = router;
    

      

  • 相关阅读:
    安装node和npm
    安装git
    常用软件
    vscode常用插件
    git生成ssh key
    04.接口初始化规则与类加载器准备阶段和初始化阶段的意义
    03.编译期常量与运行期常量的区别与数组创建本质分析
    02.常量的本质含义与反编译及助记符
    01.类加载,连接与初始化过程
    HTTP 状态码大全
  • 原文地址:https://www.cnblogs.com/yu-hailong/p/8523444.html
Copyright © 2020-2023  润新知