DEV Community

Cover image for 对象关系映射(ORM)| 完整解释(定义、用法、结构和实践)
Daniela "Ingeniela" Barazarte
Daniela "Ingeniela" Barazarte

Posted on

1

对象关系映射(ORM)| 完整解释(定义、用法、结构和实践)

对象关系映射(ORM)| 完整解释(定义、用法、结构和实践)

介绍

大家好,我是 Daniela Barazarte,欢迎您阅读有关 ORM(对象关系映射)的完整说明。 这个解释以及#DetectaLaLogica 中的所有解释一样直观且简单。

如果您喜欢视频,这里是我在 YouTube 上制作的完整教程,它是西班牙语,但也有字幕:https://www.youtube.com/watch?v=XIkDO9aPX_4

理论

每个单词的定义

*对象关系映射(ORM)

  • 对象(Object):在OOP中,它是一个类的实例,具有允许其执行某些操作和操作数据的属性和方法。
  • 关系型(Relational):关系数据库是组织成通过关系互连的表的数据结构。
  • 映射:是一种在编程语言的对象和关系数据库的表之间进行转换的方法。

完整定义

它是一种用于连接关系数据库和面向对象编程语言的软件工程技术。

ORM

#检测逻辑

ORM全称为对象关系映射(Object-Relational Mapping),因为它是一种将面向对象编程语言的对象与关系数据库的表进行映射的技术。

使用

以便

开发人员不必直接编写 SQL 查询,而是可以与表示数据库中数据的编程语言中的对象进行交互。

何时何地

它广泛用于各种软件应用程序,从 Web 应用程序到桌面和移动应用程序。 它在处理大量数据并需要高效访问数据库的应用程序中特别有用。

作为

  • Hibernate:Java 的 ORM。
  • EntityFramwork:.NET/C# 的 ORM
  • Django ORM:Django 的默认 ORM。
  • Sequel:Ruby 的 ORM。
  • SQLAlchemy:Python 的 ORM。
  • Eloquent:Laravel 框架中使用的 PHP ORM。

练习

锻炼

您有一个销售电子产品的在线商店的 Web 应用程序。 您必须使用 OOP 语言存储和管理产品、客户、订单和财务交易的信息。

方法:SQL

我需要创建数据库,然后创建客户表并能够在其中插入数据...但考虑到我无法直接从网页执行此操作,它并没有那么有用

-- Create a customer:
INSERT INTO clients (Name, Email)
VALUES('client name', 'client email');

-- Read all customers:
SELECT * FROM clients;

-- Edit a client
UPDATE clients
SET Name = 'new client name', Email = 'new client email'
WHERE Id = client id;

-- Delete a client
DELETE FROM clients
WHERE Id = client id;
Enter fullscreen mode Exit fullscreen mode

方法:ORM

使用Csharp(编程语言)和实体框架(ORM)我可以使数据从网页直接进入数据库。 我将使用的数据库将是 MySql,我将需要不同的东西,但我可以突出显示此 ORM 中通常使用的 DbContext

public class MyDbContext : DbContext
     {
         // Add the connection line to the database in MySQL
         internal readonly string connectionString = "server=localhost;port=3306;database=dbclients";

         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
         {
             // Declare the use of MySQL and send the connection line and the MySQL version
             optionsBuilder.UseMySql(connectionString, new MySqlServerVersion(ServerVersion.AutoDetect(connectionString)));
         }
         // Declare the table to be used
         public DbSet<Client> Clients { get; set; }
     }
Enter fullscreen mode Exit fullscreen mode

建立连接后,我将需要进行迁移,然后我可以直接从项目开始执行某些功能。

一个例子是添加客户端函数:

// Function to add a new customer to the database
static void AddClient(MyDbContext db)
{
     // Print a message to indicate that a new customer is being added
     Console.WriteLine("\nAdding a new client...");

     // Prompt the user to enter the customer's name and email
     Console.Write("Name: ");
     string name = Console.ReadLine();
     Console.Write("Email: ");
     string email = Console.ReadLine();

     // Create a new Client object with the data entered by the user
     var newClient = new Client
     {
         Name = name,
         email = email
     };

     // Add the new customer object to the database
     db.Clients.Add(newClient);

     // Save the changes to the database
     db.SaveChanges();

     // Print a message to indicate that the new customer has been added to the database
     Console.WriteLine($"Added client: {newClient.Name} ({newClient.Email})");
}
Enter fullscreen mode Exit fullscreen mode

并且可以执行类似的读取、编辑和删除功能。

就像这样,您可以直接从应用程序与数据库交互,甚至无需使用 SQL 语言。

重要性

ORM 有几个好处:

  • 数据库抽象
  • 减少开发时间
  • 代码可移植性
  • 易于维护
  • 注入攻击的安全性
  • 工作更高效、更快

# 告别
(记住)#DetectaLaLógica:ORM 被称为对象关系映射(Object-Relational Mapping),因为它是一种将面向对象编程语言中的对象与关系数据库表进行映射的技术。

您可以在我的 GitHub 存储库(C# 语言代码)中练习此主题:https://github.com/danielabarazarte/DetectaLaLogica

非常感谢您的阅读,如果我的文章对您有帮助,您可以点赞,让更多的人看到,也可以评论您的任何问题或建议,感谢您关注我,查看更多此文章风格,谢谢<3。

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay