1 Star 0 Fork 0

simon/gomysql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Golang mysql操作助手

特点

  • 语意化操作
  • 支持链式操作
  • 支持连接池
  • 查询结果自动转换为map
  • 支持自定义查询
  • 支持事务
  • 高性能
  • 支持闭包查询

安装

go get gitee.com/simon_git_code/gomysql  

初始化

    //数据库配置
	dbconf := map[string]string{
		"password": "123456",
		"username": "root",
		"network":  "tcp",
		"server":   "127.0.0.1",
		"port":     "3306",
		"database": "car",
		"prefix":   "c_",
	}
    // 获取查询实例
	Db := new(mysql.Db) 
	Db.DbConn = new(mysql.DbConnection)
	Db.DbConn.Connt(&dbconf)
	// 连接池配置(可选配置)
	// 设置与数据库建立连接的最大数目
	Db.DbConn.DB.SetMaxOpenConns(500)
	// 设置连接空闲的最大时间
	Db.DbConn.DB.SetConnMaxIdleTime(16)
	// 设置连接池中的最大闲置连接数
	Db.DbConn.DB.SetMaxIdleConns(10)
	// 设置连接可重用的最大时间
	Db.DbConn.DB.SetConnMaxLifetime(30 * time.Second)

查询方法说明

func (*DbQuery) Field 指定查询字段
func (*DbQuery) Field(field string) *DbQuery

语句中指定被查询的字段,支持mysql查询函数

func (*DbQuery) Where 指定查询条件,支持多个Where
func (*DbQuery) Where(wher string,v interface{})  *DbQuery

用于Select、Delete、Update等查询条件,参数wher通常为预查询语句,例如:Where("id = ?",1),其含义为字段id的值为1的条件,?为占位符,必不可少。

Example
ret,err:=Db.Db("example").Field("*").Where("id = ?",1).Find()  
解析为查询原语:  
SELECT * FROM `example` WHERE `id`= 1 

func (*DbQuery) Or

func (*DbQuery) Or(or string, v interface{})

OR条件查询

func (*DbQuery) In

func (*DbQuery) In(in string, v []interface{}) *DbQuery

IN查询,参数in 字段名,v 参数数组

Example
ret,err:=Db.Db("example").In("id",[]interface{}{1,2,3}).Field("*").Select()
解析为查询语原语:
SELECT * FROM `example` WHERE `id` IN (1,2,3)

func (q *DbQuery) OrIn

func (*DbQuery) OrIn(in string, v []interface{}) *DbQuery 

OR IN查询 参数同in查询,只是在IN查询前加OR查询

func (q *DbQuery) Join 联合查询

func (*DbQuery) Join(jType string, table string, on string) *DbQuery

联合查询

jType //参数包含LEFT JOIN(left)、RIGHT JOIN(right)、INNER JOIN(inner)。 
table //表名,在配置中如果指明了前辍,table不需要包含前辍  
on    //联合查询条件
Example
Db.Db("example e").Join("left","user u","u.id = e.uid").Field("u.name,e.id").Where("id = ?",1).Find()

自定义查询

Db.Db("example").Query("select id from example")

事务

db, err := util.GetRDB().Db("").CreateDBTx()
//写入
err:=db.TxSave(map[string]interface{}{
    "name":"simon",
})
if err!=nil {
    tx.Rollback()
} else {
    tx.Commit()
}

其他方法

名称 参数 说明
Order order 字符串 ORDER BY语句
Group group 字符串 GROUP BY语句
Size page int 默认值1, size int 页容量 分页查询
Having having 字符串 含占位符? HAVING条件语句
Find 查询一条记录
Select 查询多条记录
Save map[string]interface{} 写入数据
Update 更新数据
Del 删除记录
Query 字符串 自定义查询
Fun *ExQuery 闭包查询

获取写入成功后的自增ID

Save方法写入成功后,返回sql.Result实例。

newId,err:=result.LastInsertId()
newId即为新增的ID
MIT License Copyright (c) 2022 simon Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Mysql操作助手 展开 收起
Go
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/simon_git_code/gomysql.git
git@gitee.com:simon_git_code/gomysql.git
simon_git_code
gomysql
gomysql
master

搜索帮助