Past [Posts] Past and ORM framework ORM framework of life

Past and Present ORM framework

 
https://www.cnblogs.com/7tiny/p/9551754.html

 

table of Contents

A, ORM Introduction
Second, the principle of ORM
three, ORM advantages and disadvantages of
four common ORM framework

A, ORM Introduction

ORM (Object Relational Mapping) object-relational mapping, generally refers to persistent data and mapping of physical objects

 

 

Data storage is the vast majority of software systems have access to technology, a certain number of software products, in order to facilitate the storage and management of data, this tool will be introduced into the database, but the data from the database how to write a program it?

For the convenience of the programmer to write code data by the database, general language vendors will develop drivers for the various databases of the database connection adapter, such as ADO.Net, JDBC like.

However, driver duty database connection management information that is connected to a database, set the connection parameters, will return to their normally packaged data set type, the type of driver packages is often described as the core data, modern software design for features are easy to describe things and object-oriented thinking as the core, the conversion between the two, there are many way to go.

Apart from writing conversion section, Sql statement is a major problem, in general programming languages ​​do not have to define the type of sql statement, this is because each database sql statement style is not the same, it is difficult to give a unified solution. Second best, generally using the programming language string sql statement passed to the database drivers. Sql statement to abandon all kinds of learning outside, this way there is a big downside, and that is very easy to spell sql statement because the wrong hands and make mistakes.

In this scenario, ORM framework was born!

Two, ORM works

After completion of ORM explain past life, we talk about ORM framework for what we do.

In the first section we mentioned, and under no circumstances ORM, there are two slots points:

1. The return drive types and objects can not map well
to learn the cost of 2.SQL statement and error-prone rate (many database statements is difficult to grasp all)

Well, let me see how to improve our ORM two slots points:


1. The data-driven data returned is usually data-centric data sets, we need to get one by one by hand to match the class object and column data returned by the database, and then assigned to the object. Would like to thank the two generics and reflection grammar, by generics and reflection, we can get to any property of an entity class rather than specific to one type, one by one by traversing the property to get a collection of data and the entity class copy returns. This operation put the perfect package data collection data collection became the core of object-oriented classes and objects related data.

Spelling 2.sql statements, we can provide a common set of sql statement template, and then when the specific operating entity object attribute names and attribute values ​​entity object as a parameter into the mosaic, assembled into a complete sql statement (such as java system the Mybatis frame) or still using a package easy to understand Api, Api assembled into internal sql statement (e.g. .Net framework EntityFramework system) and by a corresponding method of physical objects

ORM is also what we've done?

After completion of the two most important issues to address, we can do some other things to help us in the frame. ORM framework to do most is "Cache."

 


As a programmer should master the basics, database operations and hard to deal with, and the program is running in memory, the operating memory faster than hard drives operate faster than a few times, you can see a large-scale high-traffic system Since the database is easy to operate too frequent and slow down the overall speed, thus affecting the use of the system. Therefore, ORM framework to help us reduce access the database, to speed up the system speed.

ORM framework caching systems are generally more complex, and each ORM framework to implement caching mechanism is different. The whole idea is the same, for frequently accessed data cache and data cache when editing to be updated in order to avoid inconsistent data problems. Details of the cache implementation strategies do not enumerate here, may be of interest to analyze for an ORM framework.

Three, ORM advantages and disadvantages

By describing two of the above, we can easily summarize the pros and cons of ORM framework. Let's say for advantage.

ORM framework reduces the learning curve, one pair are not familiar with the sql statement Developers can also easily operate the database through a simple ORM framework Api.

提高了开发效率,ORM使我们减少很多繁琐重复的工作量,让我们的注意力集中在实现业务上。

一定程度上提高了程序的响应速度。

ORM框架的弊端也很明显,框架会自动生成Sql语句,所有场景的sql语句都是同一套模板,难以自动针对场景对sql语句进行良好的优化,某种场景下很容易生成执行很慢的sql语句。如果让DBA看到这样的执行sql,必定引来抓狂崩溃。

ORM框架只是为了满足绝大多数的场景而生的,特殊需要优化sql的场景下,我们完全可以直接使用驱动手动执行sql或使用ORM框架内提供的sql语句api进行自定义sql语句。

四、市面常见的ORM框架

.Net体系:

EntityFramework,功能强大,lambda api,有些庞大臃肿,很多功能用不上
Dapper,轻量级,数据库种类支持丰富,sql写法灵活,运行速度快
CYQ.Data,自动化,日志,分布式缓存,弱类型,api简介

Java体系:

MyBatis,映射采用Xml配置sql,多种映射关系灵活配置,sql需要手动编写到配置,轻量级,半自动
Hibernate,Xml配置sql,支持HQL语句,移植性好,日志,重量级,功能全,全自动
Speedment,lambda api,依赖java8

目录

一、ORM简介
二、ORM的工作原理
三、ORM的优缺点
四、常见的ORM框架

一、ORM简介

ORM(Object Relational Mapping)对象关系映射,一般指持久化数据和实体对象的映射

 

 

数据存储是绝大多数软件系统都要接触到的技术,具有一定规模的软件产品,为了方便存储和管理数据,便引入了数据库这一工具,但是数据如何从程序写入数据库的呢?

为方便程序员通过代码将数据写入数据库,一般的语言开发的厂商都会为各种数据库适配数据库连接的驱动程序,比如ADO.Net,JDBC等。

但是数据库连接的驱动程序的职责在于管理连接数据库,设置连接参数等信息,通常会返回各自封装好的数据集类型,驱动程序封装的类型往往是以数据为核心进行描述的,现代化的软件设计为了简便描述事物的特征都而以面向对象思想为核心,两者之间的转换还有很多的路要走。

除却转换部分,Sql语句的编写也是一大学问,一般的编程语言都没有为sql语句定义类型,这是因为每种数据库的sql语句风格都是不一样的,难以给出一个统一的方案。退而求其次,一般的编程语言都采用字符串形式传递sql语句到数据库驱动程序。抛弃各种各样的sql语句的学习之外,这种方式有一个很大的弊端,那就是sql语句的拼写极容易由于手误而犯错。

在这种场景下,ORM框架诞生了!

二、ORM的工作原理

在讲解完ORM的前世之后,我们来聊聊ORM框架为我们做了什么。

在第一节我们提过了,没有ORM的情况下,主要有两个槽点:

1.驱动返回类型和对象不能良好映射
2.SQL语句的学习成本及易错率(多种数据库语句难以全部掌握)

那么,且看我们的ORM如何改善这两个槽点:


1.数据驱动返回的数据通常都是以数据为核心的数据集合,我们需要通过手动将类对象和数据库返回的列数据进行一一匹配获取,然后赋值到对象上。在这里要感谢泛型和反射两大语法,通过泛型和反射,我们可以获取到任何实体类的属性而不是具体到某一种类型,通过遍历实体类的属性去数据集合中一一获取并复制返回。这一操作便将数据集合的数据完美包装成了以面向对象为核心的和类相关的对象数据集合。

2.sql语句的拼写,我们可以提供一套公共sql语句模板,然后在具体实体对象操作的时候将实体对象的属性名称和属性值当作参数拼接进去,组装成完整的sql语句(例如java体系中的Mybatis框架)或者依旧采用封装一套浅显易懂的Api,Api内部通过对应方法和实体对象的组装成sql语句(例如.Net体系中EntityFramework框架)

ORM还为我们做了什么?

最重要的两个问题解决完之后,我们可以在框架中做一些对我们有帮助的其他事情。ORM框架做的最多的便是“缓存”。

 


作为程序员应该掌握的基础知识,数据库操作是要和硬盘打交道的,而程序是在内存中运行的,操作内存的速度要比操作硬盘快数十倍以上,可见一个访问量较高的大型系统很容易由于数据库操作过于频繁而拖慢整体速度,从而影响系统的使用。因此,ORM框架要帮助我们减少数据库的访问,加快系统速度。

ORM框架的缓存系统一般是较为复杂的,而且每种ORM框架对缓存的实现机制都是不同的。整体的思路却是一致的,对访问频率较高的数据进行缓存,并在对数据编辑的时候要对缓存进行更新,以免出现数据不一致的问题。详细的缓存实现策略这里不一一赘述,感兴趣可以针对某个ORM框架进行剖析。

三、ORM的优缺点

通过上面两节的描述,我们很容易总结出ORM框架的优缺点。我们先来说说优点。

ORM框架降低了学习门槛,一个对sql语句并不熟悉的开发人员也可以很容易通过简易的ORM框架Api进行数据库的操作。

提高了开发效率,ORM使我们减少很多繁琐重复的工作量,让我们的注意力集中在实现业务上。

一定程度上提高了程序的响应速度。

ORM框架的弊端也很明显,框架会自动生成Sql语句,所有场景的sql语句都是同一套模板,难以自动针对场景对sql语句进行良好的优化,某种场景下很容易生成执行很慢的sql语句。如果让DBA看到这样的执行sql,必定引来抓狂崩溃。

ORM框架只是为了满足绝大多数的场景而生的,特殊需要优化sql的场景下,我们完全可以直接使用驱动手动执行sql或使用ORM框架内提供的sql语句api进行自定义sql语句。

四、市面常见的ORM框架

.Net体系:

EntityFramework,功能强大,lambda api,有些庞大臃肿,很多功能用不上
Dapper,轻量级,数据库种类支持丰富,sql写法灵活,运行速度快
CYQ.Data,自动化,日志,分布式缓存,弱类型,api简介

Java体系:

MyBatis,映射采用Xml配置sql,多种映射关系灵活配置,sql需要手动编写到配置,轻量级,半自动
Hibernate,Xml配置sql,支持HQL语句,移植性好,日志,重量级,功能全,全自动
Speedment,lambda api,依赖java8

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11737838.html