Files
learn-golang/Web开发/03go-gorm-demo/transaction_savepoint.go
liumangmang b010f82221 feat(auth): 添加完整的用户认证API项目
- 实现用户注册、登录、JWT令牌认证功能
- 集成Gin、GORM、Viper、Zap等框架
- 添加密码加密、数据库操作、中间件等完整功能
- 配置多环境支持、日志轮转、CORS处理
- 创建完整的项目结构和配置文件体系
2025-12-30 18:00:42 +08:00

38 lines
722 B
Go

package main
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
//type User struct {
// ID uint `gorm:"primaryKey"`
// Name string `gorm:"size:100"`
// Email string `gorm:"size:100;unique"`
//}
func main() {
db, _ := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
db.AutoMigrate(&User{})
db.Transaction(func(tx *gorm.DB) error {
tx.Create(&User{Name: "Alice", Email: "alice@example.com"})
// 创建 SavePoint
tx.SavePoint("sp1")
tx.Create(&User{Name: "Bob", Email: "bob@example.com"})
// 回滚到 SavePoint
tx.RollbackTo("sp1")
return nil
})
// 检查结果
var count int64
db.Model(&User{}).Count(&count)
fmt.Printf("Total users: %d (should be 1, only Alice)\n", count)
}