Skip to content

Mysql automatic assembly, based on gorm and gin-plus, project run automatic initialization.

License

Notifications You must be signed in to change notification settings

archine/mysql-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

     

Mysql自动装配,只需简单配置即可使用

一、🚀🚀安装

1. Go get

go get github.com/archine/[email protected]

2、Go Mod

# go.mod文件加入下面的一条
github.com/archine/mysql-starter v1.0.0
# 命令行在该项目目录下执行
go mod tidy

二、使用文档

1、配置信息

配置 描述
mysql => log_level 日志级别,支持 info、error, 默认error
mysql => url 数据库连接地址,可直接输入完整的url,这时 database 不填即视为当前为完整的url
mysql => username 账号
mysql => password 密码
mysql => database 数据库,不填的情况下说明 url 为一个完整的
mysql => max_idle 连接池最大空闲连接,默认 10
mysql => max_connect 连接池最大连接数,默认 50
mysql => max_idle_time 连接空闲时间,超过即销毁,默认 30s

示例:

mysql:
  log_level: debug
  url: 127.0.0.1:3306
  username: root
  password: root
  database: go
  max_idle: 20
  max_connect: 50
  max_idle_time: 30s

2、项目使用

在任意 Bean 中直接注入即可

package model

import (
	"errors"
	"github.com/archine/ioc"
	starter "github.com/archine/mysql-starter"
	"gorm.io/gorm"
)

type User struct {
	Id       int
	Username string
}

func (u *User) TableName() string {
	return "user"
}

type UserMapper struct {
	*starter.M
}

func (u *UserMapper) CreateBean() ioc.Bean {
	return &UserMapper{}
}

// FindOne 查询
func (u *UserMapper) FindOne(id int) *User {
	var data User
	err := u.Db.Model(data).Where("id = ?", id).Take(&data).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil
		}
		panic(err)
	}
	return &data
}

About

Mysql automatic assembly, based on gorm and gin-plus, project run automatic initialization.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages