您的位置:首页 > 财经 > 金融 > 苏州关键词排名提升_网页设计图片排版怎么设置_网络营销做得比较成功的案例_竞价广告是怎么推广的

苏州关键词排名提升_网页设计图片排版怎么设置_网络营销做得比较成功的案例_竞价广告是怎么推广的

2025/1/17 9:05:52 来源:https://blog.csdn.net/weixin_52236586/article/details/142333483  浏览:    关键词:苏州关键词排名提升_网页设计图片排版怎么设置_网络营销做得比较成功的案例_竞价广告是怎么推广的
苏州关键词排名提升_网页设计图片排版怎么设置_网络营销做得比较成功的案例_竞价广告是怎么推广的

go-orm接口原生到框架

1. 使用 Go 原生数据库 API 连接 MySQL

示例项目结构
myapp/
├── main.go
├── models/
│   └── user.go
└── go.mod
go.mod
module myappgo 1.20require (github.com/go-sql-driver/mysql v1.6.0
)
main.go
package mainimport ("database/sql""fmt""log"_ "github.com/go-sql-driver/mysql"
)func main() {// 数据库连接字符串dsn := "user:password@tcp(127.0.0.1:3306)/mydb"// 连接数据库db, err := sql.Open("mysql", dsn)if err != nil {log.Fatal(err)}defer db.Close()// 测试数据库连接if err := db.Ping(); err != nil {log.Fatal(err)}fmt.Println("Connected to the database successfully!")// 创建表createTable := `CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(100) NOT NULL,email VARCHAR(100) NOT NULL UNIQUE);`_, err = db.Exec(createTable)if err != nil {log.Fatal(err)}fmt.Println("Table created successfully!")// 插入数据_, err = db.Exec("INSERT INTO users (name, email) VALUES (?, ?)", "John Doe", "john@example.com")if err != nil {log.Fatal(err)}fmt.Println("Data inserted successfully!")// 查询数据rows, err := db.Query("SELECT id, name, email FROM users")if err != nil {log.Fatal(err)}defer rows.Close()for rows.Next() {var id intvar name, email stringif err := rows.Scan(&id, &name, &email); err != nil {log.Fatal(err)}fmt.Printf("%d: %s - %s\n", id, name, email)}if err := rows.Err(); err != nil {log.Fatal(err)}
}

2.使用 SQLX 库连接 MySQL

sqlx 是一个 Go 语言的库,它在 database/sql 基础上扩展了更多功能,提供了更简洁的接口用于处理 SQL 查询和数据映射。以下是一个使用 sqlx 连接和操作 MySQL 数据库的示例。

sqlx 示例项目结构
myapp/
├── main.go
├── models/
│   └── user.go
└── go.mod
go.mod
module myappgo 1.20require (github.com/go-sql-driver/mysql v1.6.0github.com/jmoiron/sqlx v1.4.0
)
models/user.go
package models// User 定义了 User 结构体
type User struct {ID    int64  `db:"id"`Name  string `db:"name"`Email string `db:"email"`
}
main.go
package mainimport ("fmt""log"_ "github.com/go-sql-driver/mysql""github.com/jmoiron/sqlx""myapp/models"
)func main() {// 数据库连接字符串dsn := "user:password@tcp(127.0.0.1:3306)/mydb?charset=utf8mb4&parseTime=True&loc=Local"// 连接数据库db, err := sqlx.Open("mysql", dsn)if err != nil {log.Fatal("failed to connect database:", err)}defer db.Close()fmt.Println("Connected to the database successfully!")// 创建表schema := `CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(100) NOT NULL,email VARCHAR(100) NOT NULL UNIQUE);`_, err = db.Exec(schema)if err != nil {log.Fatal("failed to create table:", err)}fmt.Println("Table created successfully!")// 插入数据user := models.User{Name: "John Doe", Email: "john@example.com"}_, err = db.NamedExec(`INSERT INTO users (name, email) VALUES (:name, :email)`, &user)if err != nil {log.Fatal("failed to insert data:", err)}fmt.Println("Data inserted successfully!")// 查询数据var users []models.Usererr = db.Select(&users, "SELECT id, name, email FROM users")if err != nil {log.Fatal("failed to query data:", err)}for _, user := range users {fmt.Printf("%d: %s - %s\n", user.ID, user.Name, user.Email)}
}

3. 使用 XORM 框架连接 MySQL

示例项目结构
myapp/
├── main.go
├── models/
│   └── user.go
└── go.mod
go.mod
module myappgo 1.20require (github.com/go-sql-driver/mysql v1.6.0xorm.io/xorm v0.7.1
)
models/user.go
package modelstype User struct {Id    int64  `xorm:"pk autoincr"`Name  string `xorm:"not null"`Email string `xorm:"unique not null"`
}
main.go
package mainimport ("fmt""log"_ "github.com/go-sql-driver/mysql""xorm.io/xorm""myapp/models"
)func main() {// 数据库连接字符串dsn := "user:password@tcp(127.0.0.1:3306)/mydb"// 连接数据库engine, err := xorm.NewEngine("mysql", dsn)if err != nil {log.Fatal(err)}defer engine.Close()// 自动同步结构if err := engine.Sync2(new(models.User)); err != nil {log.Fatal(err)}fmt.Println("Table created successfully!")// 插入数据user := &models.User{Name: "John Doe", Email: "john@example.com"}_, err = engine.Insert(user)if err != nil {log.Fatal(err)}fmt.Println("Data inserted successfully!")// 查询数据var users []models.Userif err := engine.Find(&users); err != nil {log.Fatal(err)}for _, user := range users {fmt.Printf("%d: %s - %s\n", user.Id, user.Name, user.Email)}
}

4.使用 GORM 框架连接 MySQL

以下是一个使用 GORM 框架连接和操作 MySQL 数据库的示例。GORM 是一个流行的 Go 语言 ORM 框架,可以简化与数据库的交互。

GORM 示例项目结构
myapp/
├── main.go
├── models/
│   └── user.go
└── go.mod
go.mod
module myappgo 1.20require (github.com/go-sql-driver/mysql v1.6.0gorm.io/gorm v1.24.0gorm.io/driver/mysql v1.5.0
)
models/user.go
package modelsimport ("gorm.io/gorm"
)type User struct {gorm.ModelName  string `gorm:"not null"`Email string `gorm:"unique;not null"`
}
main.go
package mainimport ("fmt""log""gorm.io/driver/mysql""gorm.io/gorm""myapp/models"
)func main() {// 数据库连接字符串dsn := "user:password@tcp(127.0.0.1:3306)/mydb?charset=utf8mb4&parseTime=True&loc=Local"// 连接数据库db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})if err != nil {log.Fatal("failed to connect database:", err)}fmt.Println("Connected to the database successfully!")// 自动迁移if err := db.AutoMigrate(&models.User{}); err != nil {log.Fatal("failed to migrate database:", err)}fmt.Println("Table created successfully!")// 插入数据user := models.User{Name: "John Doe", Email: "john@example.com"}if err := db.Create(&user).Error; err != nil {log.Fatal("failed to insert data:", err)}fmt.Println("Data inserted successfully!")// 查询数据var users []models.Userif err := db.Find(&users).Error; err != nil {log.Fatal("failed to query data:", err)}for _, user := range users {fmt.Printf("%d: %s - %s\n", user.ID, user.Name, user.Email)}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com