| Crates.io | baichun-framework-db |
| lib.rs | baichun-framework-db |
| version | 0.1.0 |
| created_at | 2025-06-12 11:39:38.485487+00 |
| updated_at | 2025-06-12 11:39:38.485487+00 |
| description | Database module for Baichun-Rust framework |
| homepage | |
| repository | https://github.com/baichun-framework/baichun-framework |
| max_upload_size | |
| id | 1709710 |
| size | 89,236 |
baichun-framework-db 是 Baichun Framework 的数据库模块,提供了一个简单而强大的数据库访问层。基于 SQLx,支持 MySQL、PostgreSQL 和 SQLite。
将以下内容添加到你的 Cargo.toml 文件中:
[dependencies]
baichun-framework-db = "0.1"
use baichun_framework_db::{DbPool, DbConfig};
// 创建数据库配置
let config = DbConfig::new()
.url("mysql://user:pass@localhost/db_name")
.max_connections(10)
.min_connections(5);
// 创建连接池
let pool = DbPool::new(config).await?;
// 执行查询
let rows = pool.execute("SELECT * FROM users WHERE id = ?", &[&1]).await?;
// 使用事务
let mut tx = pool.begin().await?;
tx.execute("INSERT INTO users (name) VALUES (?)", &[&"Alice"]).await?;
tx.execute("UPDATE users SET status = ? WHERE name = ?", &[&"active", &"Alice"]).await?;
tx.commit().await?;
use baichun_framework_db::DbConfig;
// MySQL 配置
let mysql_config = DbConfig::new()
.url("mysql://user:pass@localhost/db_name")
.max_connections(20)
.min_connections(5)
.connect_timeout(Duration::from_secs(5))
.idle_timeout(Duration::from_secs(300));
// PostgreSQL 配置
let pg_config = DbConfig::new()
.url("postgresql://user:pass@localhost/db_name")
.max_connections(20)
.min_connections(5);
// SQLite 配置
let sqlite_config = DbConfig::new()
.url("sqlite:///path/to/db.sqlite")
.max_connections(1); // SQLite 通常使用单连接
mysql:启用 MySQL 支持(默认)postgres:启用 PostgreSQL 支持sqlite:启用 SQLite 支持all:启用所有数据库支持本项目采用 MIT 许可证。详见 LICENSE 文件。