一、ORM框架概述
对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术。简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久化到关系数据库中。那么,到底如何实现持久化呢?一种简单的方案是采用硬编码方式,为每一种可能的数据库访问操作提供单独的方法。
这种方案存在以下不足:
- 持久化层缺乏弹性。一旦出现业务需求的变更,就必须修改持久化层的接口
- 持久化层同时与域模型与关系数据库模型绑定,不管域模型还是关系数据库模型发生变化,毒药修改持久化曾的相关程序代码,增加了软件的维护难度。
ORM提供了实现持久化层的另一种模式,它采用映射元数据来描述对象关系的映射,使得ORM中间件能在任何一个应用的业务逻辑层和数据库层之间充当桥梁。Java典型的ORM中间件有:Hibernate
,ibatis
,speedframework
。
ORM的方法论基于三个核心原则:
- 简单:以最基本的形式建模数据。
- 传达性:数据库结构被任何人都能理解的语言文档化。
- 精确性:基于数据模型创建正确标准化了的结构。
二、引入sequelize
安装egg-sequelize
1 # 下载依赖,安装egg-sequelize和mysql27 2 npm install --save egg-sequelize mysql2
在egg项目中配置egg-sequelize
1 // config/plugin.js 2 exports.sequelize = { 3 enable: true, 4 package: 'egg-sequelize' 5 }
1 // config/config.default.js 2 config.sequelize = { 3 dialect: 'mysql', 4 database: 'army', //数据库名 5 host: 'localhost', 6 port: '3306', 7 username: 'root', //用户名 8 password: '', //密码 9 operatorsAliases: false, 10 }
1 // 根目录 app.js,没有创建一个app.js 2 module.exports = app => { 3 app.beforeStart(async function () { 4 // await app.model.sync({ force: true }); // 开发环境使用,会删除数据表 5 await app.model.sync({});//会永久保存数据 6 }); 7 };
三、创建数据模型
数据模型
这种模式可以通过控制器和服务进行访问app.model.Clazz或者ctx.model.Clazz,比如我们写app/controller/Clazz.js:
// app/model/clazz.js module.exports = app => { const { STRING } = app.Sequelize;//数据库字段类型,一对多 const Clazz = app.model.define('clazz', { //sequelize会自动创建主键 name: STRING,//数据库字段名称与字段类型 }) return Clazz; //返回班级 }
1 // app/model/students.js 2 module.exports = app => { 3 const { 4 STRING 5 } = app.Sequelize; 6 7 const Students = app.model.define('students', { 8 name: STRING, 9 }) 10 11 Students.associate = function () { 12 app.model.Students.belongsTo(app.model.Clazz, { //设置外键 13 foreignKey: 'clazz_id',//关联的外键 14 as: 'clazz'//将关联的数据显示到该字段上 15 }) 16 } 17 18 return Students; 19 }
四、操作数据
Controller获取数据
1 // app/controller/clazz.js 2 'use strict'; 3 4 const Controller = require('egg').Controller; 5 6 class ClazzController extends Controller { 7 //查询班级列表 8 async index() { 9 const clazzList = await this.app.model.Clazz.findAll();//查询数据库中Clazz. 10 await this.ctx.render('clazz_list', { 11 clazzList: clazzList//将记录标题'Hello World'插入'posts'表 12 //查询条件和结果自定义 13 }) 14 } 15 16 //添加班级的页面 17 async insertClazz(){ 18 await this.ctx.render('create_clazz.html') 19 } 20 21 //在数据库中添加班级 22 async create() { 23 const body = this.ctx.request.body; 24 const clazz = { 25 name:body.name 26 } 27 await this.app.model.Clazz.create(clazz);//把添加得班级存入到数据库中。 28 this.ctx.redirect("/clazz")//重镜像,添加数据之后自动跳转/Clazz页面 29 } 30 31 //通过id在数据库中删除班级 32 async destroy() { 33 const id = this.ctx.request.body.clazz_id; 34 const student = await this.app.model.Clazz.findOne({ 35 where: { 36 id: id 37 } 38 }); 39 student.destroy(); 40 this.ctx.redirect("/clazz") 41 } 42 } 43 44 module.exports = ClazzController;
1 // app/controller/students.js 2 'use strict'; 3 4 const Controller = require('egg').Controller; 5 6 class StudentsController extends Controller { 7 // 联查学生信息 8 async index() { 9 const studentList = await this.app.model.Students.findAll({ 10 include:[{ //联查班级的数据 11 model:this.app.model.Clazz, 12 as:'clazz' 13 }] 14 }); 15 await this.ctx.render('student_list.html', { 16 studentList: studentList 17 }) 18 } 19 20 //添加学生的页面 21 async insertStudent() { 22 const clazzList = await this.app.model.Clazz.findAll(); 23 await this.ctx.render('create_student.html', { 24 clazzList 25 }) 26 } 27 28 // 在数据库中添加学生 29 async create(){ 30 const body = this.ctx.request.body; 31 const student = { 32 name:body.name, 33 clazz_id:body.clazz_id 34 } 35 await this.app.model.Students.create(student); 36 this.ctx.redirect("/students") 37 } 38 39 // 通过id删除学生信息 40 async destroy(){ 41 const id = this.ctx.request.body.student_id; 42 //findOne指的是查找指定表的单条数据,返回一个对象 43 const student = await this.app.model.Students.findOne({ 44 //where()条件查询 45 where:{ 46 id:id 47 } 48 }); 49 student.destroy(); 50 this.ctx.redirect("/students") 51 } 52 } 53 54 module.exports = StudentsController;