package appservice
import (
"gormdemo/models"
"sync"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
const stopTimeout = time.Second * 10
const dsn = "root:123456@tcp(192.168.3.10:3306)/go_testdb?charset=utf8mb4&parseTime=True&loc=Local"
var (
once sync.Once
db *gorm.DB
)
func openDbConnection() *gorm.DB {
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
sqlDB, err := db.DB()
sqlDB.SetMaxIdleConns(10) //空闲连接数
sqlDB.SetMaxOpenConns(100)//最大连接数
sqlDB.SetConnMaxLifetime(time.Minute)
if err != nil {
panic("连接数据库失败!")
}
return db
}
func init() {
db = openDbConnection()
// migration
db.AutoMigrate(&models.User{})
}
// 创建用户
func Create(user *models.User) {
if user == nil {
panic("用户不存在!")
} else {
result := db.Create(user)
if result.Error != nil && result.RowsAffected == 0 {
panic("添加失败:" + result.Error.Error())
}
}
}
// 更新用户
func Update(user *models.User) bool {
if user == nil {
panic("用户不存在!")
} else {
// 查最新的用户
var existUser models.User
result := db.Where("email = ?", *user.Email).First(&existUser)
if result.RowsAffected > 0 {
existUser.Name = user.Name
existUser.Age = user.Age
existUser.Email = user.Email
existUser.Birthday = user.Birthday
db.Save(&existUser)
} else {
panic("用户不存在!")
}
}
return true
}
// 获取用户
func Get(id uint) *models.User {
var user models.User
db.First(&user, id)
return &user
}
// 删除用户
func Delete(id uint) bool {
db.Delete(&models.User{}, id)
return true
}
// 获取所有用户
func GetUserAll() *[]models.User {
var users []models.User
result := db.Select([]string{}).Find(&users)
if result.RowsAffected > 0 {
return &users
}
return nil
}