database 什么是 ORM,它是如何工作的,我应该如何使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1279613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is an ORM, how does it work, and how should I use one?
提问by Ryan
Someone suggested I use an ORM for a project that I'm designing, but I'm having trouble finding information on what it is or how it works.
有人建议我将 ORM 用于我正在设计的项目,但我无法找到有关它是什么或如何工作的信息。
Can anyone give me a brief explanation of what an ORM is and how it works and how I should get started using one?
谁能给我一个关于 ORM 是什么、它是如何工作的以及我应该如何开始使用 ORM 的简要说明?
回答by e-satis
Introduction
介绍
Object-Relational Mapping(ORM) is a technique that lets you query and manipulate data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to a librarythat implements the Object-Relational Mapping technique, hence the phrase "an ORM".
对象关系映射(ORM) 是一种技术,可让您使用面向对象的范例从数据库中查询和操作数据。在谈到ORM,大多数人都指的是图书馆实现了对象关系映射技术,因此短语“ORM”。
An ORM library is a completely ordinary library written in your language of choice that encapsulates the code needed to manipulate the data, so you don't use SQL anymore; you interact directly with an object in the same language you're using.
ORM 库是用您选择的语言编写的完全普通的库,它封装了操作数据所需的代码,因此您不再使用 SQL;您直接与使用相同语言的对象进行交互。
For example, here is a completely imaginary case with a pseudo language:
例如,这是一个使用伪语言的完全虚构的案例:
You have a book class, you want to retrieve all the books of which the author is "Linus". Manually, you would do something like that:
你有一个书籍类,你想检索作者是“Linus”的所有书籍。手动,你会做这样的事情:
book_list = new List();
sql = "SELECT book FROM library WHERE author = 'Linus'";
data = query(sql); // I over simplify ...
while (row = data.next())
{
book = new Book();
book.setAuthor(row.get('author');
book_list.add(book);
}
With an ORM library, it would look like this:
使用 ORM 库,它看起来像这样:
book_list = BookTable.query(author="Linus");
The mechanical part is taken care of automatically via the ORM library.
机械部分通过 ORM 库自动处理。
Pros and Cons
利弊
Using ORM saves a lot of time because:
使用 ORM 可以节省大量时间,因为:
- DRY: You write your data model in only one place, and it's easier to update, maintain, and reuse the code.
- A lot of stuff is done automatically, from database handling to I18N.
- It forces you to write MVCcode, which, in the end, makes your code a little cleaner.
- You don't have to write poorly-formed SQL (most Web programmers really suck at it, because SQL is treated like a "sub" language, when in reality it's a very powerful and complex one).
- Sanitizing; using prepared statements or transactions are as easy as calling a method.
- DRY:您只在一处编写数据模型,并且更容易更新、维护和重用代码。
- 很多东西都是自动完成的,从数据库处理到I18N。
- 它迫使您编写MVC代码,这最终会使您的代码更简洁一些。
- 您不必编写格式不佳的 SQL(大多数 Web 程序员真的很讨厌它,因为 SQL 被视为一种“子”语言,而实际上它是一种非常强大和复杂的语言)。
- 消毒;使用准备好的语句或事务就像调用一个方法一样简单。
Using an ORM library is more flexible because:
使用 ORM 库更灵活,因为:
- It fits in your natural way of coding (it's your language!).
- It abstracts the DB system, so you can change it whenever you want.
- The model is weakly bound to the rest of the application, so you can change it or use it anywhere else.
- It lets you use OOP goodness like data inheritance without a headache.
- 它适合您的自然编码方式(这是您的语言!)。
- 它抽象了数据库系统,因此您可以随时更改它。
- 该模型与应用程序的其余部分的绑定很弱,因此您可以更改它或在其他任何地方使用它。
- 它让您可以毫无头疼地使用 OOP 的优点,例如数据继承。
But ORM can be a pain:
但是 ORM 可能很痛苦:
- You have to learn it, and ORM libraries are not lightweight tools;
- You have to set it up. Same problem.
- Performance is OK for usual queries, but a SQL master will always do better with his own SQL for big projects.
- It abstracts the DB. While it's OK if you know what's happening behind the scene, it's a trap for new programmers that can write very greedy statements, like a heavy hit in a
for
loop.
- 你必须学习它,ORM 库不是轻量级工具;
- 你必须设置它。同样的问题。
- 对于通常的查询,性能还可以,但是对于大型项目,SQL 大师使用自己的 SQL 总是会做得更好。
- 它抽象了数据库。虽然如果您知道幕后发生的事情是可以的,但对于可以编写非常贪婪的语句(例如
for
循环中的重击)的新程序员来说,这是一个陷阱。
How to learn about ORM?
如何学习ORM?
Well, use one. Whichever ORM library you choose, they all use the same principles. There are a lot of ORM libraries around here:
嗯,用一个。无论您选择哪个 ORM 库,它们都使用相同的原则。这里有很多 ORM 库:
- Java: Hibernate.
- PHP: Propelor Doctrine(I prefer the last one).
- Python: the Django ORM or SQLAlchemy(My favorite ORM library ever).
- C#: NHibernateor Entity Framework
- Java:休眠。
- PHP:Propel或Doctrine(我更喜欢最后一个)。
- Python:Django ORM 或SQLAlchemy(我最喜欢的 ORM 库)。
- C#:NHibernate或实体框架
If you want to try an ORM library in Web programming, you'd be better off using an entire framework stack like:
如果您想在 Web 编程中尝试使用 ORM 库,最好使用整个框架堆栈,例如:
Do not try to write your own ORM, unless you are trying to learn something. This is a gigantic piece of work, and the old ones took a lot of time and work before they became reliable.
不要尝试编写自己的 ORM,除非您想学习一些东西。这是一项巨大的工作,旧的工作需要花费大量的时间和工作才能变得可靠。
回答by OscarRyz
Can anyone give me a brief explanation...
谁能给我一个简单的解释...
Sure.
当然。
ORM stands for "Object to Relational Mapping" where
ORM 代表“对象到关系映射”,其中
The Objectpart is the one you use with your programming language ( python in this case )
The Relationalpart is a Relational Database Manager System ( A database that is ) there are other types of databases but the most popular is relational ( you know tables, columns, pk fk etc eg Oracle MySQL, MS-SQL )
And finally the Mappingpart is where you do a bridge between your objects and your tables.
该对象的部分是你与你的编程语言使用一个(在这种情况下蟒蛇)
该关系部分是一个关系数据库管理系统(即数据库)有其他类型的数据库,但最流行的是关系(你知道的表,列,PK FK等如Oracle的MySQL,MS-SQL)
最后,映射部分是您在对象和表之间建立桥梁的地方。
In applications where you don't use a ORM framework you do this by hand. Using an ORM framework would allow you do reduce the boilerplate needed to create the solution.
在不使用 ORM 框架的应用程序中,您可以手动执行此操作。使用 ORM 框架可以减少创建解决方案所需的样板文件。
So let's say you have this object.
所以假设你有这个对象。
class Employee:
def __init__( self, name ):
self.__name = name
def getName( self ):
return self.__name
#etc.
and the table
和桌子
create table employee(
name varcar(10),
-- etc
)
Using an ORM framework would allow you to map that object with a db record automagically and write something like:
使用 ORM 框架将允许您自动使用 db 记录映射该对象并编写如下内容:
emp = Employee("Ryan")
orm.save( emp )
And have the employee inserted into the DB.
并将员工插入数据库。
Oops it was not that brief but I hope it is simple enough to catch other articles you read.
哎呀,这不是那么简短,但我希望它足够简单,可以捕捉您阅读的其他文章。
回答by Justin Niessner
An ORM (Object Relational Mapper) is a piece/layer of software that helps map your code Objects to your database.
ORM(对象关系映射器)是一个软件层,可帮助将代码对象映射到数据库。
Some handle more aspects than others...but the purpose is to take some of the weight of the Data Layer off of the developer's shoulders.
有些处理的方面比其他的多……但目的是减轻开发人员肩上的数据层的一些负担。
Here's a brief clip from Martin Fowler (Data Mapper):
以下是 Martin Fowler(数据映射器)的简短剪辑:
Patterns of Enterprise Application Architecture Data Mappers
回答by Alex Martelli
Like all acronyms it's ambiguous, but I assume they mean object-relational mapper-- a way to cover your eyes and make believe there's no SQL underneath, but rather it's all objects;-). Not really true, of course, and not without problems -- the always colorful Jeff Atwood has described ORM as the Vietnam of CS;-). But, if you know little or no SQL, and have a pretty simple / small-scale problem, they can save you time!-)
像所有首字母缩略词一样,它是模棱两可的,但我认为它们的意思是对象关系映射器——一种遮住眼睛并让人相信下面没有 SQL 的方法,而是所有对象;-)。当然,这不是真的,也不是没有问题——总是丰富多彩的杰夫阿特伍德将 ORM 描述为CS 的越南;-)。但是,如果您对 SQL 知之甚少或根本不知道,并且有一个非常简单/小规模的问题,它们可以为您节省时间!-)
回答by Hamza Riaz
Object Model is concerned with the following three concepts Data Abstraction Encapsulation Inheritance The relational model used the basic concept of a relation or table. Object-relational mapping (OR mapping) products integrate object programming language capabilities with relational databases.
对象模型涉及以下三个概念 数据抽象 封装 继承 关系模型使用了关系或表的基本概念。对象关系映射(OR 映射)产品将对象编程语言功能与关系数据库集成在一起。