pub struct DbPool { /* private fields */ }Expand description
数据库连接池包装器
提供了对 SQLx 连接池的高级封装,支持连接管理、查询执行和事务操作。
§示例
use baichun_framework_db::{DatabaseConfig, DbPool};
async fn example() -> Result<()> {
// 创建连接池
let config = DatabaseConfig::new()
.url("mysql://user:pass@localhost/db_name")
.max_connections(10);
let pool = DbPool::new(&config).await?;
// 执行查询
pool.execute("CREATE TABLE users (id INT PRIMARY KEY)").await?;
// 开始事务
let tx = pool.begin().await?;
// ... 执行事务操作 ...
Ok(())
}Implementations§
Source§impl DbPool
impl DbPool
Sourcepub async fn new(config: &DatabaseConfig) -> Result<Self>
pub async fn new(config: &DatabaseConfig) -> Result<Self>
创建新的数据库连接池
根据提供的配置创建一个新的连接池实例。
Sourcepub async fn begin(&self) -> Result<Transaction<'_, MySql>>
pub async fn begin(&self) -> Result<Transaction<'_, MySql>>
开始新的事务
返回一个事务对象,可以用于执行事务操作。
Sourcepub async fn execute(&self, query: &str) -> Result<MySqlQueryResult>
pub async fn execute(&self, query: &str) -> Result<MySqlQueryResult>
执行不返回结果的查询
适用于 DDL 语句或不需要返回结果的 DML 语句。
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for DbPool
impl !UnwindSafe for DbPool
impl Freeze for DbPool
impl Send for DbPool
impl Sync for DbPool
impl Unpin for DbPool
impl UnsafeUnpin for DbPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more