1,下载gin依赖
go get - u github. com/ gin- gonic/ gin
2,代码演示
2.1,完成GlobalVariabel.go保存全局变量
2.2,完成 MysqlConf.go
package config
import (
"go_gorm/src/config"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"time"
)
func MysqlConf ( ) ( mysqlDB * gorm. DB ) {
configInfo : = config. InitConf( )
db, _ : = gorm. Open( mysql. New( mysql. Config{
DriverName : "" ,
ServerVersion : "" ,
DSN : configInfo. MysqlUrl,
DSNConfig : nil,
Conn : nil,
SkipInitializeWithVersion : false ,
DefaultStringSize : 174 ,
DefaultDatetimePrecision : nil,
DisableWithReturning : false ,
DisableDatetimePrecision : false ,
DontSupportRenameIndex : false ,
DontSupportRenameColumn : false ,
DontSupportForShareClause : false ,
DontSupportNullAsDefaultValue : false ,
} ) , & gorm. Config{
SkipDefaultTransaction : false ,
FullSaveAssociations : false ,
Logger : nil,
NowFunc : nil,
DryRun : false ,
PrepareStmt : false ,
DisableAutomaticPing : false ,
DisableForeignKeyConstraintWhenMigrating : true ,
DisableNestedTransaction : false ,
AllowGlobalUpdate : false ,
QueryFields : false ,
CreateBatchSize : 0 ,
ClauseBuilders : nil,
ConnPool : nil,
Dialector : nil,
Plugins : nil,
} )
sqlDB, _: = db. DB ( )
sqlDB. SetMaxIdleConns( 10 )
sqlDB. SetMaxOpenConns( 100 )
sqlDB. SetConnMaxLifetime( time. Hour)
return db
}
2.3,完成 ResultBase.go
package base
type ResultBase struct {
Flag bool
Res interface { }
}
2.4,完成 TestUser.go
package rto
import "gorm.io/gorm"
type TestUser struct {
gorm. Model
Name string `json: "name" form: "name" `
Age uint8 `json: "age" form: "age" `
}
2.5,完成 TestUserRto.go
package rto
type TestUserRto struct {
Id uint `json: "id" form: "id" `
Name string `json: "name" form: "name" `
Age uint `json: "age" form: "age" `
}
2.6,完成 TestUserVo.go
package vo
import "time"
type TestUserVo struct {
Id int64 `json: "id" `
Created_at time. Time `json: "createdAt" `
Updated_at time. Time `json: "updatedAt" `
Name string `json: "name" `
Age uint8 `json: "age" `
}
2.7,完成 TestUserService.go
package service
import (
"errors"
"github.com/gin-gonic/gin"
"go_gorm/src/demo04_crud_gin/entity/base"
"go_gorm/src/demo04_crud_gin/entity/rto"
"go_gorm/src/demo04_crud_gin/entity/vo"
"go_gorm/src/entity/variable"
"gorm.io/gorm"
)
type TestUserService struct {
}
func ( testUserService TestUserService ) QueryUser ( c * gin. Context) {
rto : = & rto. TestUserRto{ }
c. ShouldBind( & rto)
User : = [ ] vo. TestUserVo{ }
res : = base. ResultBase{ }
dbRes : = variable. GLOBAL_DB. Table( "test_users" ) . Where ( "name like ?" , "%" + rto. Name+ "%" ) . Find ( & User )
if ( errors. Is( dbRes. Error, gorm. ErrRecordNotFound) ) {
res. Flag = false
res. Res = "没查到数据"
c. JSON ( 200 , res)
} else {
res. Flag = true
res. Res = User
c. JSON ( 200 , res)
}
}
func ( testUserService TestUserService ) AddUser ( c * gin. Context) {
rto : = & [ ] rto. TestUser{ }
c. ShouldBind( & rto)
res : = base. ResultBase{ }
dbres : = variable. GLOBAL_DB. Create( & rto)
if ( dbres. Error != nil) {
res. Flag = false
res. Res = dbres. Error
c. JSON ( 200 , res)
}
res. Flag = true
res. Res = dbres. RowsAffected
c. JSON ( 200 , res)
}
func ( testUserService TestUserService ) UpdateUser ( c * gin. Context) {
rto : = & rto. TestUserRto{ }
c. ShouldBind( & rto)
res : = base. ResultBase{ }
dbres : = variable. GLOBAL_DB.
Table( "test_users" ) .
Where ( "id = ?" , rto. Id) .
Updates ( rto)
if ( dbres. Error != nil) {
res. Flag = false
res. Res = dbres. Error
c. JSON ( 200 , res)
} else {
res. Flag = true
res. Res = "更新成功"
c. JSON ( 200 , res)
}
}
2.8,完成 TestUserController.go
package controller
import (
"github.com/gin-gonic/gin"
"go_gorm/src/demo04_crud_gin/service"
)
func TestUserController ( engine * gin. Engine) {
testUserController : = engine. Group( "/testUser" )
{
testUserController. POST ( "/queryUser" , service. TestUserService{ } . QueryUser)
testUserController. POST ( "/addUser" , service. TestUserService{ } . AddUser)
testUserController. POST ( "/updateUser" , service. TestUserService{ } . UpdateUser)
}
}
2.9,完成 main.go
package main
import (
"github.com/gin-gonic/gin"
"go_gorm/src/demo04_crud_gin/config"
"go_gorm/src/demo04_crud_gin/controller"
"go_gorm/src/entity/variable"
)
func main ( ) {
var db = config. MysqlConf( ) ;
variable. GLOBAL_DB = db
router : = gin. Default( ) ;
controller. TestUserController( router)
router. Run( ":8081" )
}