mysql

package
v1.0.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package mysql 提供MySQL数据库客户端的创建和管理功能 包含连接池配置、连接生命周期管理等核心功能

Index

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

func NewClient(host string, port int, username, password, database string) (*Client, error)

NewMysqlClient 创建新的MySQL客户端 参数:

  • host: MySQL服务器地址
  • port: MySQL服务器端口
  • username: 数据库用户名
  • password: 数据库密码
  • database: 数据库名称

返回:

  • *Client: MySQL客户端实例
  • error: 创建过程中的错误信息

使用默认连接池配置:

  • MaxOpenConns: 25 (最大打开连接数)
  • MaxIdleConns: 10 (最大空闲连接数)
  • ConnMaxLifetime: 1小时 (连接最大生命周期)

func NewMysqlClientWithConfig

func NewMysqlClientWithConfig(config *Config) (*Client, error)

NewMysqlClientWithConfig 使用配置创建MySQL客户端 参数:

  • config: MySQL连接配置对象,包含所有连接参数

返回:

  • *Client: MySQL客户端实例
  • error: 创建过程中的错误信息

该方法允许使用自定义配置创建客户端,包括连接池参数等高级设置

func (*Client) Close

func (c *Client) Close() error

Close 关闭数据库连接

func (*Client) Ping

func (c *Client) Ping() error

Ping 测试数据库连接

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配置

type QueryError

type QueryError struct {
	Sql  string
	Args []interface{}
	Err  error
}

QueryError 错误类型

func (*QueryError) Error

func (e *QueryError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL