DBUtil 8.0.0-preview9
dotnet add package DBUtil --version 8.0.0-preview9
NuGet\Install-Package DBUtil -Version 8.0.0-preview9
<PackageReference Include="DBUtil" Version="8.0.0-preview9" />
<PackageVersion Include="DBUtil" Version="8.0.0-preview9" />
<PackageReference Include="DBUtil" />
paket add DBUtil --version 8.0.0-preview9
#r "nuget: DBUtil, 8.0.0-preview9"
#:package DBUtil@8.0.0-preview9
#addin nuget:?package=DBUtil&version=8.0.0-preview9&prerelease
#tool nuget:?package=DBUtil&version=8.0.0-preview9&prerelease
DBUtil(暂时仅支持 MySql)
一款轻量化操作数据库的类库,类DBHelper设计理念,比Dapper略重,比EntityFramework/FreeSql/SqlSugar轻。
特性亮点
DBUtil专为.NET开发者设计,提供简洁高效的数据库操作体验:
- 轻量级架构: 精简设计,避免过度封装带来的性能开销
- Lambda表达式支持: 原生支持Lambda语法,让代码更简洁直观
- 灵活SQL拼接: 创新的SQL片段生成功能,助力复杂查询拼接
- 完善事务管理: 丝滑般的事务控制体验,支持嵌套事务
- 分布式锁支持: 内置分布式锁机制,保障并发安全
- 树形数据处理: 原生支持CTE递归查询,轻松处理层级数据
- JSON深度支持: 强大的JSON操作能力,简化JSON数据处理
- 元数据管理: 提供完整的数据库元信息管理功能
安装配置
通过NuGet包管理器安装DBUtil核心库及对应数据库驱动:
# 安装核心库
dotnet add package DBUtil
# 根据数据库类型选择对应驱动(以MySQL为例)
dotnet add package DBUtil.Provider.MySql
# 支持的数据库类型
dotnet add package DBUtil.Provider.SqlServer # SQL Server
dotnet add package DBUtil.Provider.PostgreSQL # PostgreSQL
dotnet add package DBUtil.Provider.SQLite # SQLite
dotnet add package DBUtil.Provider.Oracle # Oracle
快速开始
1. 初始化数据库访问实例
using DBUtil;
// 创建数据库访问实例(建议使用单例模式)
var db = DBFactory.CreateDB("MySql",
"Server=127.0.0.1;Database=test;Uid=root;Pwd=123456;AllowLoadLocalInfile=true;SslMode=none;AllowPublicKeyRetrieval=True;Charset=utf8mb4;");
// 可选:注入扩展功能(ID生成、流水号、JSON扩展等)
// db.RemoveExtendFeatures(); // 移除扩展功能
// db.AddExtendFeatures(); // 注入扩展功能
2. 数据插入操作
无实体插入:
var insert = db.Insert("t_user", new[]
{
new Dictionary<string, object> { { "name", "tom" }, { "age", 20 } },
new Dictionary<string, object> { { "name", "lisa" }, { "age", 18 } },
new Dictionary<string, object> { { "name", "jim" }, { "age", 18 } },
});
// 生成SQL:insert into `t_user`(`name`,`age`) values ('tom',20),('lisa',18),('jim',18);
有实体插入:
var insert = db.Insert<PersonEntity>().SetEntity(new[]
{
new PersonEntity { Name = "jack", Age = 18, CreateTime = DateTime.Now, Sex = EnumSex.Male },
new PersonEntity { Name = "tom", Age = 20, CreateTime = DateTime.Now, Sex = EnumSex.Male },
});
// 执行插入并返回插入的数据
var inserted = await insert.ExecuteInsertedAsync();
3. 数据更新操作
Lambda表达式更新:
var update = db.Update<PersonEntity>()
.SetColumn("name", "tom")
.SetColumn(i => i.Age, i => i.Age + 1)
.SetColumn(i => i.Sex, EnumSex.Male)
.Where(i => i.Id == 1);
await update.ExecuteAffrowsAsync();
4. 数据查询操作
多种查询方式:
// 查询DataTable
var dt = await db.SelectDataTableAsync("SELECT id,name,age FROM t_user LIMIT 10");
// 查询实体列表
var users = await db.SelectModelListAsync<UserEntity>(
"SELECT id,name,age FROM t_user WHERE age > @age",
[db.CreatePara("age", 18)]
);
// 查询字典列表
var dics = await db.SelectDictionaryListAsync("SELECT id,name,age FROM t_user LIMIT 10");
// 条件查询实体
var person = await db.Select<PersonEntity>()
.Where(i => i.Id == 1)
.FirstAsync();
// 聚合查询
var stats = db.Select<PersonEntity>()
.GroupBy(i => i.Age)
.Having(i => i.Key > 18)
.ToListAsync(i => new
{
Age = i.Key,
Count = i.Length,
Names = i.Join(i => i.Name, ",")
});
5. 事务管理
await db.RunInTransactionAsync(async () =>
{
// 业务逻辑代码
await DoSomethingAsync();
// 支持嵌套事务
await db.RunInTransactionAsync(async () =>
{
await DoOtherThingAsync();
});
});
6. 分布式锁
// 使用分布式锁保护关键代码段
await db.RunInLockAsync("order.process.lock", async () =>
{
await ProcessOrderAsync();
});
7. ID与流水号生成
// 生成唯一ID
var id = await db.NewIdAsync("t_user", "id");
// 生成业务流水号
var sno = await db.NewSNOAsync("t_user", "sno", SerialFormat.CreateFast("Order_"));
// 流水号示例:Order_20250816000001
8. 批量数据导入
// 百万级数据批量导入
DataTable dt = GetDataTable(); // 准备好的数据
await db.BulkCopyAsync(dt, "t_user");
9. 树形数据查询(CTE)
// 查询树形结构数据
var tree = await db.SelectTree<AreaEntity>()
.Where(i => i.Name == "郑州")
.SetSpreedMode(EnumTreeSpreedMode.Both)
.ToListAsync();
功能特性详解
实体配置
支持通过特性注解配置实体与数据库表的映射关系:
public class UserEntity
{
[PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
[JsonMap]
public UserProfile Profile { get; set; }
[ParentId]
public int? ParentId { get; set; }
[Children]
public List<UserEntity> Children { get; set; }
}
SQL片段功能
创新的SQL片段生成器,简化复杂查询的拼接:
var sql = db.CaseSeg<UserEntity>(u => u.Status)
.WhenSeg(u => u.Status == 1).Then("已激活")
.WhenSeg(u => u.Status == 0).Then("未激活")
.ElseSeg("未知状态")
.EndAs("StatusText");
JSON操作支持
深度集成JSON操作能力:
// JSON属性包含判断
var hasPermission = user.Permissions.ContainsKey("admin");
// JSON数组操作
user.Roles.Add("new_role");
user.Roles.RemoveAt(0);
// JSON属性设置
user.Info.SetValue("last_login", DateTime.Now);
枚举类型处理
支持多种枚举存储方式:
- 数字存储:建议方式,数据库存储枚举值对应的数字
- 字符串存储:数据库存储枚举名称字符串
- JSON数组:复杂枚举场景使用JSON数组存储
性能表现
DBUtil经过优化,在以下场景表现出色:
| 场景 | 性能指标 |
|---|---|
| SQL查询→DataTable | 高速返回 |
| SQL查询→实体映射 | 高效转换 |
| Lambda解析生成SQL | 快速编译 |
| 批量插入实体 | 性能优异 |
| 表连接/子查询 | 智能优化 |
文档与支持
详细文档请访问项目docs目录:
- 实体配置
- 插入操作
- 更新操作
- 保存操作
- 删除操作
- 基础查询
- 简单查询
- 事务管理
- 会话管理
- SQL片段
- 树形查询
- JSON支持
- 分布式锁
- ID与流水号
- 枚举处理
- 元数据管理
- 批量导入
- SQL监控
- 性能测试
- 日期处理
限制说明
本库专注于数据库操作,不提供以下功能:
- CodeFirst:不提供根据实体自动创建表结构的功能
- AOP支持:暂无AOP集成计划,支持SQL执行监控
- 关系配置:不提供一对多、多对多的实体关系配置
许可证
本项目采用 MIT License 开源许可证。
贡献指南
欢迎提交Issue或Pull Request贡献代码:
- Fork本项目
- 创建特性分支
- 提交代码更改
- 发起合并请求
联系我们
- 项目地址:https://gitee.com/jackletter/DBUtil
- 问题反馈:https://gitee.com/jackletter/DBUtil/issues
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- DotNetCommon.Core (>= 8.11.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on DBUtil:
| Package | Downloads |
|---|---|
|
DBUtil.Provider.MySql
一款轻量化操作db的类库,类DBHelper设计, 比dapper略重, 比EntityFramework/freesql/sqlsugar轻, 功能列表: - 基础CURD(支持 lambda 解析); - 创新的sql片段生成, 助力复杂sql拼接; - 丝滑事务管理; - 分布式锁; - 树形查询; - 强大的json支持; - 元数据管理; |
|
|
ExcelCtr
Excel操作工具 |
|
|
my1024
Test nuget package |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 8.0.0-preview9 | 0 | 9/7/2026 |
| 8.0.0-preview8 | 0 | 9/7/2026 |
| 8.0.0-preview6 | 68 | 7/26/2026 |
| 8.0.0-preview5 | 77 | 5/21/2026 |
| 8.0.0-preview4 | 432 | 11/19/2025 |
| 8.0.0-preview2 | 192 | 9/8/2025 |
| 8.0.0-preview | 194 | 9/8/2025 |