.NET轻量级ORM框架Dapper.NET的高级应用实例详解

开发 后端
在这篇文章中,我将为您提供Dapper的高级应用功能示例,每个示例都有源代码和注释。这些示例将涵盖Dapper的一些高级功能,以帮助你更好地理解如何在实际应用中使用它。

Dapper是一个轻量级的ORM(对象关系映射)库,用于.NET应用程序与数据库之间的数据访问。它允许你使用SQL查询来执行数据库操作,而不需要复杂的映射配置。在这篇文章中,我将为您提供Dapper的高级应用功能示例,每个示例都有源代码和注释。这些示例将涵盖Dapper的一些高级功能,以帮助你更好地理解如何在实际应用中使用它。

示例1:多表关联查询

Dapper允许你轻松执行多表关联查询。在这个示例中,我们将查询两个表,一个是Customers表,另一个是Orders表,并将它们关联起来。

using Dapper;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

public class Customer
{
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
}

public class Order
{
    public int OrderId { get; set; }
    public int CustomerId { get; set; }
    public decimal TotalAmount { get; set; }
}

class Program
{
    static void Main()
    {
        string connectionString = "YourConnectionStringHere";

        using IDbConnection dbConnection = new SqlConnection(connectionString);

        string query = "SELECT c.CustomerId, c.CustomerName, o.OrderId, o.TotalAmount " +
                       "FROM Customers c " +
                       "JOIN Orders o ON c.CustomerId = o.CustomerId";

        var result = dbConnection.Query<Customer, Order, Customer>(
            query,
            (customer, order) =>
            {
                customer.Orders = order;
                return customer;
            },
            splitOn: "OrderId"
        );

        foreach (var customer in result)
        {
            Console.WriteLine($"Customer ID: {customer.CustomerId}, Name: {customer.CustomerName}");
            Console.WriteLine($"Order ID: {customer.Orders.OrderId}, Total Amount: {customer.Orders.TotalAmount}");
            Console.WriteLine();
        }
    }
}

示例2:事务处理

Dapper允许你使用事务来确保一组操作要么全部成功,要么全部失败。在这个示例中,我们将演示如何在Dapper中使用事务。

using Dapper;
using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "YourConnectionStringHere";

        using IDbConnection dbConnection = new SqlConnection(connectionString);
        dbConnection.Open();

        using var transaction = dbConnection.BeginTransaction();

        try
        {
            string insertQuery = "INSERT INTO Products (Name, Price) VALUES (@Name, @Price)";
            string updateQuery = "UPDATE Customers SET CustomerName = @CustomerName WHERE CustomerId = @CustomerId";

            var product = new { Name = "ProductX", Price = 19.99 };
            var customer = new { CustomerName = "NewName", CustomerId = 1 };

            dbConnection.Execute(insertQuery, product, transaction: transaction);
            dbConnection.Execute(updateQuery, customer, transaction: transaction);

            // Commit the transaction if all operations are successful
            transaction.Commit();
            Console.WriteLine("Transaction committed.");
        }
        catch (Exception ex)
        {
            // Rollback the transaction if any operation fails
            transaction.Rollback();
            Console.WriteLine("Transaction rolled back. Error: " + ex.Message);
        }
    }
}

示例3:自定义类型映射

Dapper允许你自定义数据类型到.NET类型的映射。在这个示例中,我们将使用TypeHandler来自定义Point类型的映射。

using Dapper;
using System;
using System.Data;
using System.Data.SqlClient;
using Npgsql;
using NpgsqlTypes;

public class Point
{
    public double X { get; set; }
    public double Y { get; set; }
}

public class PointTypeHandler : SqlMapper.TypeHandler<Point>
{
    public override void SetValue(IDbDataParameter parameter, Point value)
    {
        parameter.Value = $"({value.X},{value.Y})";
        parameter.DbType = DbType.String;
    }

    public override Point Parse(object value)
    {
        if (value is string strValue)
        {
            var parts = strValue.Trim('(', ')').Split(',');
            if (parts.Length == 2 && double.TryParse(parts[0], out double x) && double.TryParse(parts[1], out double y))
            {
                return new Point { X = x, Y = y };
            }
        }
        return null;
    }
}

class Program
{
    static void Main()
    {
        SqlMapper.AddTypeHandler(new PointTypeHandler());

        string connectionString = "YourConnectionStringHere";

        using IDbConnection dbConnection = new NpgsqlConnection(connectionString);

        string query = "SELECT PointColumn FROM MyTable WHERE Id = @Id";
        var result = dbConnection.Query<Point>(query, new { Id = 1 }).FirstOrDefault();

        if (result != null)
        {
            Console.WriteLine($"X: {result.X}, Y: {result.Y}");
        }
        else
        {
            Console.WriteLine("Point not found.");
        }
    }
}

示例4:批量插入

Dapper支持批量插入数据,这对于大规模数据操作非常有用。在这个示例中,我们将演示如何批量插入多个产品记录。

using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

class Program
{
    static void Main()
    {
        string connectionString = "YourConnectionStringHere";

        using IDbConnection dbConnection = new SqlConnection(connectionString);
        dbConnection.Open();

        var products = new List<Product>
        {
            new Product { Name = "ProductA", Price = 10.99m },
            new Product { Name = "ProductB", Price = 15.99m },
            new Product { Name = "ProductC", Price = 20.99m }
        };

        string insertQuery = "INSERT INTO Products (Name, Price) VALUES (@Name, @Price)";
        int rowsAffected = dbConnection.Execute(insertQuery, products);

        Console.WriteLine($"{rowsAffected} rows inserted.");
    }
}

示例5:自定义SQL语句

虽然Dapper通常用于执行SQL查询,但你也可以执行自定义的SQL语句,例如存储过程或函数调用。在这个示例中,我们将演示如何执行一个存储过程。

using Dapper;
using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
   

 {
        string connectionString = "YourConnectionStringHere";

        using IDbConnection dbConnection = new SqlConnection(connectionString);

        string storedProcedure = "MyStoredProcedure";
        var parameters = new DynamicParameters();
        parameters.Add("Param1", 123);
        parameters.Add("Param2", "TestValue", DbType.String, ParameterDirection.Input, 50);

        var result = dbConnection.Query<int>(storedProcedure, parameters, commandType: CommandType.StoredProcedure).FirstOrDefault();

        Console.WriteLine($"Stored procedure result: {result}");
    }
}

示例6:自定义SQL语句执行

你可以使用Dapper的Execute方法来执行自定义的SQL语句,而不仅仅是查询。在这个示例中,我们将演示如何执行一个自定义的更新语句。

using Dapper;
using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "YourConnectionStringHere";

        using IDbConnection dbConnection = new SqlConnection(connectionString);

        string updateStatement = "UPDATE Customers SET CustomerName = @NewName WHERE CustomerId = @CustomerId";
        var parameters = new { NewName = "NewName", CustomerId = 1 };

        int rowsAffected = dbConnection.Execute(updateStatement, parameters);

        Console.WriteLine($"{rowsAffected} rows updated.");
    }
}

示例7:异步查询

Dapper支持异步查询,这对于高并发应用程序非常有用。在这个示例中,我们将演示如何使用异步方法执行查询。

using Dapper;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        string connectionString = "YourConnectionStringHere";

        using IDbConnection dbConnection = new SqlConnection(connectionString);

        string query = "SELECT * FROM Products";
        var products = await dbConnection.QueryAsync<Product>(query);

        foreach (var product in products)
        {
            Console.WriteLine($"Name: {product.Name}, Price: {product.Price}");
        }
    }
}

示例8:自定义表名

你可以使用Dapper的Table特性来指定实体类与数据库中不同表之间的映射关系。在这个示例中,我们将演示如何自定义表名。

using Dapper;
using System;
using System.Data;
using System.Data.SqlClient;

[Table("MyCustomTableName")]
public class CustomTable
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Program
{
    static void Main()
    {
        string connectionString = "YourConnectionStringHere";

        using IDbConnection dbConnection = new SqlConnection(connectionString);

        string query = "SELECT * FROM MyCustomTableName";
        var result = dbConnection.Query<CustomTable>(query);

        foreach (var item in result)
        {
            Console.WriteLine($"Id: {item.Id}, Name: {item.Name}");
        }
    }
}

示例9:自定义参数前缀

Dapper默认使用@作为参数前缀,但你可以自定义参数前缀。在这个示例中,我们将演示如何自定义参数前缀为$

using Dapper;
using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        SqlMapperExtensions.Configure("$$$"); // 设置参数前缀为 $$$

        string connectionString = "YourConnectionStringHere";

        using IDbConnection dbConnection = new SqlConnection(connectionString);

        string query = "SELECT * FROM Products WHERE Name = $$$productName";
        var result = dbConnection.Query<Product>(query, new { productName = "ProductA" });

        foreach (var product in result)
        {
            Console.WriteLine($"Name: {product.Name}, Price: {product.Price}");
        }
    }
}

示例10:查询分页

Dapper使分页查询变得容易,你可以使用LIMITOFFSET来执行分页查询。在这个示例中,我们将演示如何执行分页查询。

using Dapper;
using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "YourConnectionStringHere";

        using IDbConnection dbConnection = new SqlConnection(connectionString);

        int pageSize = 10;
        int pageNumber = 2;

        string query = "SELECT * FROM Products ORDER BY ProductId OFFSET @Offset ROWS FETCH NEXT @PageSize ROWS ONLY";
        var result = dbConnection.Query<Product>(query, new { Offset = (pageNumber - 1) * pageSize, PageSize = pageSize });

        foreach (var product in result)
        {
            Console.WriteLine($"Name: {product.Name}, Price: {product.Price}");
        }
    }
}

这些示例演示了Dapper的一些高级功能,包括多表关联查询、事务处理、自定义类型映射、批量插入、自定义SQL语句、异步查询、自定义表名、自定义参数前缀和查询分页。通过这些示例,你可以更好地了解如何在实际应用中充分利用Dapper来简化数据访问任务。

责任编辑:姜华 来源: 今日头条
相关推荐

2011-11-28 09:16:24

Javajooq框架

2012-02-01 09:16:18

Java

2012-05-21 21:34:51

iOS

2009-08-11 17:29:53

.NET远程处理框架

2023-11-16 08:34:23

.NETORM框架

2023-06-13 13:38:00

FlaskPython

2018-08-01 21:33:00

ComponentOn

2009-07-29 08:58:20

NHibernate ORM框架

2023-06-27 16:42:18

Tinygrad深度学习工具

2024-03-05 18:40:15

LiteDB数据库NoSQL

2022-08-10 12:21:07

PythonWebBottle

2020-11-11 12:13:59

JS

2010-01-21 09:08:53

.NET设计模式

2009-07-21 17:06:35

iBATIS.NET执

2009-07-24 15:07:56

ASP.NET上传文件

2009-11-12 14:55:16

ADO.NET实体框架

2009-04-28 16:18:29

.Net MicroSideShow

2010-09-15 09:18:21

ASP.NET MVC

2013-06-20 10:28:39

MVVM框架avalon架构

2011-03-02 10:03:44

SaturnJSJS
点赞
收藏

51CTO技术栈公众号