title | keywords | description | |||||
---|---|---|---|---|---|---|---|
GORM MySQL |
|
Using GORM with MySQL database. |
This is a sample program demonstrating how to use GORM as an ORM to connect to a MySQL database with the Fiber web framework.
- Go 1.16 or higher
- MySQL database
- Go modules
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/gorm-mysql
-
Install dependencies:
go mod tidy
-
Configure the database connection in the
config.json
file:{ "DB_Username": "your_db_username", "DB_Password": "your_db_password", "DB_Name": "your_db_name", "DB_Host": "localhost", "DB_Port": "3306" }
-
Run the application:
go run main.go
-
The server will start on
http://localhost:3000
.
Method | URL | Description |
---|---|---|
GET | /hello | Returns a hello message |
GET | /allbooks | Retrieves all books |
GET | /book/:id | Retrieves a book by ID |
POST | /book | Creates a new book |
PUT | /book | Updates an existing book |
DELETE | /book | Deletes a book |
curl -X GET http://localhost:3000/allbooks
curl -X GET http://localhost:3000/book/1
curl -X POST http://localhost:3000/book -d '{"title": "New Book", "author": "Author Name"}' -H "Content-Type: application/json"
curl -X PUT http://localhost:3000/book -d '{"id": 1, "title": "Updated Book", "author": "Updated Author"}' -H "Content-Type: application/json"
curl -X DELETE http://localhost:3000/book -d '{"id": 1}' -H "Content-Type: application/json"