Home > Backend Development > Golang > Detailed examples of query operations of Golang GORM

Detailed examples of query operations of Golang GORM

PHPz
Release: 2023-04-03 13:50:18
Original
3752 people have browsed it

Golang is a relatively new programming language. Its birth makes the development of web applications easier. The superiority of Golang lies in its concurrency mechanism and concise language syntax. GORM is an ORM (Object Relational Mapping) library in Golang. It is similar to ORMs in other languages ​​and can implement object-oriented database operations, making database additions, deletions, modifications and queries simpler and clearer. In this article, we will mainly introduce the query operations of Golang GORM.

1. Foreword

When using Golang GORM for query operations, we need to import the GORM library first, and then set the corresponding database information, for example:

import (
    "gorm.io/gorm"
    "gorm.io/driver/mysql"
)

dsn := "root:password@tcp(127.0.0.1:3306)/database?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
Copy after login

Here we use mysql database, and set the corresponding database connection information. After importing the GORM library, we need to define the corresponding data structure, for example:

type User struct {
  ID        int
  Name      string
  Age       int
  Email     string
  CreatedAt time.Time
}
Copy after login

Here we define a User structure, including ID, Name, Age, Email and CreatedAt fields, where ID is automatically Incremental primary key field.

2. Query method

  1. Find a single record

In Golang GORM, we can use the Find method to query a record, for example:

var user User
db.Find(&user, 1) // 查询主键为1的记录
Copy after login

Here we define a user variable and use the Find method to query the records with a primary key of 1. The queried data will be saved in the user variable.

  1. Find multiple records

In Golang GORM, we can use the Find method to query multiple records, for example:

var users []User
db.Find(&users) // 查询所有的记录
Copy after login

Here we define A users slice variable and use the Find method to query all records. All queried data will be saved in the users array.

  1. Conditional query

In Golang GORM, we can use the Where method to perform conditional query, for example:

var users []User
db.Where("age > ?", 18).Find(&users) // 查询年龄大于18的所有记录
Copy after login

Here we query the age through the Where method All records greater than 18 are stored in the users slice variable.

  1. Fuzzy query

In Golang GORM, we can use the Like method to perform fuzzy query, for example:

var users []User
db.Where("name LIKE ?", "%john%").Find(&users) // 查询名字中包含“john”的记录
Copy after login

Here we use the Where method Fuzzy query, query records whose names contain "john" and save them in the users slice variable.

  1. IN conditional query

In Golang GORM, we can use the In method to perform IN conditional query, for example:

var users []User
db.Where("name IN ?", []string{"john", "mary", "bob"}).Find(&users) // 查询名字为john、mary、bob的所有记录
Copy after login

Here we use the Where method An IN conditional query was performed to query all records named john, mary, and bob, and stored in the users slice variable.

  1. BETWEEN condition query

In Golang GORM, we can use the Between method to perform BETWEEN condition query, for example:

var users []User
db.Where("age BETWEEN ? AND ?", 18, 30).Find(&users) // 查询年龄在18到30岁之间的所有记录
Copy after login

Here we use the Where method A BETWEEN conditional query was performed to query all records between the ages of 18 and 30 and save them in the users slice variable.

3. Summary

The above is the query operation method of Golang GORM. Through the query operation of Golang GORM, we can easily implement the ORM operation based on Golang, making the addition, deletion, modification and query of the database simpler and clearer. Of course, in practical applications, we still need to apply it flexibly according to specific business needs.

The above is the detailed content of Detailed examples of query operations of Golang GORM. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template