[Koa] Briefly talk about the basic concepts of ORM and ORM applications

ORM basic concepts

-What is ORM

That is, Object-Relationl Mapping, its role is to make a mapping between the relational database and the object, so that we do not need to deal with complex SQL statements when we are specifically operating the database, as long as we operate the object as usual Just operate it.

-What is "persistence"

Persistence, that is, save data (such as objects in memory) to a storage device (such as a disk) that can be permanently saved. The main application of persistence is to store data in memory in a relational database. Of course, it can also be stored in disk files, XML data files, and so on.

-What is the "persistence layer"

Persistence Layer, that is, a logical layer that focuses on the realization of a particular system in the data persistence application field, associates data users with data entities.

Why do persistence and ORM design (important)

    在目前的企业应用系统设计中,MVC,即 Model(模型)- View(视图)- Control(控制)为主要的系统架构模式。MVC 中的 Model 包含了复杂的业务逻辑和数据逻辑,以及数据存取机制(如 JDBC的连接、SQL生成和Statement创建、还有ResultSet结果集的读取等)等。
    将这些复杂的业务逻辑和数据逻辑分离,以将系统的紧耦 合关系转化为松耦合关系(即解耦合),是降低系统耦合度迫切要做的,也是持久化要做的工作。MVC 模式实现了架构上将表现层(即View)和数据处理层(即Model)分离的解耦合,而持久化的设计则实现了数据处理层内部的业务逻辑和数据逻辑分离的解耦合。 而 ORM 作为持久化设计中的最重要也最复杂的技术,也是目前业界热点技术。
  • ORM is used to map the objects represented by the object model to the SQL-based relational model database structure. In this way, we do not need to deal with complex SQL statements when we are specifically operating the entity database. We only need to simply manipulate the attributes and methods of the entity object to achieve the effect of operating the database.
  • ORM technology provides a bridge between the object and the database. The object data in the foreground and the relational data in the database are transformed into each other through this bridge. Different programming languages ​​have different ORM frameworks. For example, Java, its ORM framework has: Hibernate, Ibatis/Mybatis and so on. In Node Web development, Sequelize is a popular ORM framework.

ORM application

ORM is a technology that completes the operation of relational databases through the grammar of instance objects. It is the abbreviation of "Object/Relational Mapping". ORM maps databases into objects.

	1、数据库的表(table) --> 类(class)
	2、记录(record,行数据)--> 对象(object)
	3、字段(field)--> 对象的属性(attribute)

Look at the following is a line of SQL statement:

SELECT id, first_name, last_name, phone, birth_date, sex
 FROM persons 
 WHERE id = 10

The program runs SQL directly, and the way to operate the database is as follows:

res = db.execSql(sql);
name = res[0]["FIRST_NAME"];

The wording changed to ORM is as follows:

p = Person.get(10);
name = p.first_name;

A comparison shows that ORM uses objects to encapsulate database operations, so you don't need to touch the SQL language. Developers only use object-oriented programming, interact directly with data objects, and do not care about the underlying database

In summary, ORM has the following advantages:

1、数据模型都在一个地方定义,更容易更新和维护,也利于重用代码
2、ORM 有现成的工具,很多功能都可以自动完成,比如数据消毒、预处理、事务等等
3、它迫使你使用 MVC 架构,ORM 就是天然的 Model,最终使代码更清晰
4、基于 ORM 的业务代码比较简单,代码量少,语义性好,容易理解
5、你不必编写性能不佳的 SQL

ORM also has outstanding shortcomings:

1、ORM 库不是轻量级工具,需要花很多精力学习和设置
2、对于复杂的查询,ORM 要么是无法表达,要么是性能不如原生的 SQL
3、ORM 抽象掉了数据库层,开发者无法了解底层的数据库操作,也无法定制一些特殊的 SQL

Steps for usage:

1.使用 ORM 的第一步,就是你必须告诉它,怎么连接数据库
2.创建model:连接数据库以后,下一步就要把数据库的表,转成数据模型(Model)
3.创建数据库操作类,通过Model 里面数据库表的定义,创建操作方法
4.在业务流程中调用相关方法

CRUD method:

There are four basic operations of the database: create (new), read (read), update (update) and delete (delete), referred to as CRUD

ORM turns these four types of operations into object methods.

1、find()方法用于根据主键,获取单条记录或多条记录
2、where()方法用于指定查询条件
3、create()方法用于新建记录
4、Update()方法用于更新记录
5、destroy()方法用于删除记录

Relationship-The relationship between the table and the table (relation), divided into three types:

1、一对一(one-to-one):一种对象与另一种对象是一一对应关系,比如一个学生只能在一个班级
2、一对多(one-to-many): 一种对象可以属于另一种对象的多个实例,比如一张唱片包含多首歌
3、多对多(many-to-many):两种对象彼此都是"一对多"关系,比如一张唱片包含多首歌,同时一首歌可以属于多张唱片

The setting method of model association relationship for establishing relationship is as follows:

1、hasOne - 与目标模型建立1:1关联关系,关联关系(外键)存在于目标模型中
2、belongsTo - 与目标模型建立1:1关联关系,关联关系(外键)存在于源模型中
3、hasMany - 与目标模型建立1:N关联关系,关联关系(外键)存在于目标模型中
4、belongsToMany - 与目标模型建立N:M关联关系,会通过sourceId和targetId创建交叉表

Guess you like

Origin blog.csdn.net/weixin_43352901/article/details/108395043