使用GORM操作Oracle數據庫的技巧如下:
import (
"gorm.io/driver/oracle"
"gorm.io/gorm"
)
func main() {
dsn := "user/password@//hostname:1521/service_name"
db, err := gorm.Open(oracle.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
}
type User struct {
ID uint `gorm:"column:id;primary_key"`
Name string `gorm:"column:name"`
Age int `gorm:"column:age"`
}
db.AutoMigrate(&User{})
var users []User
db.Where("age > ?", 18).Order("id desc").Limit(10).Find(&users)
user := User{Name: "Alice", Age: 25}
db.Create(&user)
db.Model(&user).Updates(User{Name: "Bob"})
db.Delete(&user)
通過以上技巧,可以在GORM中方便地操作Oracle數據庫,實現數據的增刪改查功能。