Documentation
¶
Overview ¶
Package mysql 提供MySQL数据库客户端的创建和管理功能 包含连接池配置、连接生命周期管理等核心功能
Index ¶
- type Client
- func (c *Client) Close() error
- func (c *Client) Ping() error
- func (c *Client) QueryMany(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- func (c *Client) QueryManyNamed(ctx context.Context, dest interface{}, query string, params interface{}) error
- func (c *Client) QueryOne(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- type Config
- type QueryError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client MySQL客户端
func NewClient ¶
NewMysqlClient 创建新的MySQL客户端 参数:
- host: MySQL服务器地址
- port: MySQL服务器端口
- username: 数据库用户名
- password: 数据库密码
- database: 数据库名称
返回:
- *Client: MySQL客户端实例
- error: 创建过程中的错误信息
使用默认连接池配置:
- MaxOpenConns: 25 (最大打开连接数)
- MaxIdleConns: 10 (最大空闲连接数)
- ConnMaxLifetime: 1小时 (连接最大生命周期)
func NewMysqlClientWithConfig ¶
NewMysqlClientWithConfig 使用配置创建MySQL客户端 参数:
- config: MySQL连接配置对象,包含所有连接参数
返回:
- *Client: MySQL客户端实例
- error: 创建过程中的错误信息
该方法允许使用自定义配置创建客户端,包括连接池参数等高级设置
func (*Client) QueryMany ¶
func (c *Client) QueryMany(ctx context.Context, dest interface{}, query string, args ...interface{}) error
QueryMany 执行查询并将结果扫描到目标切片中,并完成自定义sql和参数 参数:
- ctx: 上下文,用于控制查询的超时和取消
- dest: 目标切片指针,必须是 *[]T 类型,其中 T 为结构体
- query: SQL 查询语句
- args: 查询参数
返回值:
- error: 查询过程中的错误,包括参数错误、数据库错误等
功能说明:
该方法支持将多行查询结果自动映射到结构体切片中。通过反射自动匹配 数据库列名与结构体字段的 db 标签,实现灵活的结果集映射。
func (*Client) QueryManyNamed ¶
func (c *Client) QueryManyNamed(ctx context.Context, dest interface{}, query string, params interface{}) error
命名传参 参数:
- ctx: 上下文
- dest: 目标切片指针,必须是 *[]T 类型,其中 T 为结构体
- query: SQL 查询语句,使用命名参数(如 :username, :id)
- params: 参数结构体,字段通过 db 标签指定参数名
返回:
- error: 查询过程中的错误,包括参数错误、数据库错误等
功能说明:
该方法支持将多行查询结果自动映射到结构体切片中,并使用命名参数。 通过反射自动匹配数据库列名与结构体字段的 db 标签,实现灵活的结果集映射。 命名参数语法为 :paramName,会自动替换为标准 SQL 的 ? 占位符。
func (*Client) QueryOne ¶
func (c *Client) QueryOne(ctx context.Context, dest interface{}, query string, args ...interface{}) error
QueryOne 执行单行查询并将结果扫描到目标结构体中,并完成自定义sql和参数 参数:
- ctx: 上下文
- dest: 目标结构体指针
- query: SQL查询语句
- args: 查询参数
返回:
- error: 查询过程中的错误,包括参数错误、数据库错误等
功能说明:
该方法支持将单行查询结果自动映射到结构体中。通过反射自动匹配 数据库列名与结构体字段的 db 标签,实现灵活的结果集映射。
type Config ¶
type Config struct {
Host string
Port int
Username string
Password string
Database string
// 可选配置
MaxOpenConns int // 最大打开连接数
MaxIdleConns int // 最大空闲连接数
ConnMaxLifetime time.Duration // 连接最大生命周期
}
Config MySQL配置
Click to show internal directories.
Click to hide internal directories.