golang實(shí)現(xiàn)動態(tài)路由的項(xiàng)目實(shí)踐
一、動態(tài)路由
1.結(jié)構(gòu)體(數(shù)據(jù)庫的定義)
包含了角色數(shù)據(jù)庫、菜單數(shù)據(jù)庫、角色和菜單關(guān)系表。
type Role struct {
gorm.Model
Rolename string `json:"rolename"`
Authority string `json:"authority"`
Order int `json:"order" gorm:"column:order"`
Status bool `json:"status"`
Menus []Menu `json:"menus" gorm:"many2many:role_menu_table"`
Remark string `json:"remark"`
}type Menu struct {
gorm.Model
ParentID uint `json:"parentid" gorm:"column:parentid"`
Path string `json:"path"`
Name string `json:"name"`
Component string `json:"component"`
Sort int `json:"sort"`
Meta `json:"meta"`
Children []Menu `json:"children" gorm:"-"`
Roles []Role `json:"rolse" gorm:"many2many:role_menu_table"`
}
type Meta struct {
ActiveName string `json:"activeName" gorm:"comment:高亮菜單"`
KeepAlive bool `json:"keepalive" gorm:"comment:是否緩存"` // 是否緩存
DefaultMenu bool `json:"defaultmenu" gorm:"comment:是否是基礎(chǔ)路由(開發(fā)中)"` // 是否是基礎(chǔ)路由(開發(fā)中)
Title string `json:"title" gorm:"comment:菜單名"` // 菜單名
Icon string `json:"icon" gorm:"comment:菜單圖標(biāo)"` // 菜單圖標(biāo)
CloseTab bool `json:"closeTab" gorm:"comment:自動關(guān)閉tab"` // 自動關(guān)閉tab
}type RoleMenu struct {
MenuId string `json:"menuid" gorm:"colume:menuid"`
RoleId string `json:"roleid" gorm:"colume:roleid"`
}2.預(yù)加載preload
var role Role
err = db.Preload("Menus").Find(&role,roleid).Error
if err != nil {
fmt.Println("Error:", err)
return
}else{
fmt.Printf("Role's Menus: %+v\n", role.Menus)
}
Preload("Menus"):在查詢 Role 時,預(yù)加載 Menus 字段,即查詢出 Role 對應(yīng)的所有 Menu 數(shù)據(jù)。通過這種方式,可以避免在訪問 role.Menus 時,再次觸發(fā)數(shù)據(jù)庫查詢,出現(xiàn) N+1 查詢問題。
3.添加關(guān)聯(lián)的方法
var role models.Role
role.ID = reqRole.ID
role.Rolename = reqRole.Rolename
role.Authority = reqRole.Authority
role.Order = reqRole.Order
role.Status = reqRole.Status
role.Menus = menus
role.Remark = reqRole.Remark
if err := config.DB.Create(&role).Error; err != nil {
return err
}創(chuàng)建新用戶時,用戶Menus字段為要添加的路由(從數(shù)據(jù)庫中查詢出來的),然后直接create即可。創(chuàng)建之后,數(shù)據(jù)庫中不會顯示Menus字段,但是role_menu_table會自動添加關(guān)聯(lián)。
默認(rèn)情況下,Updates 方法只更新主表的數(shù)據(jù),不會自動更新關(guān)聯(lián)關(guān)系,因?yàn)?nbsp;Menus 是通過 many2many 關(guān)系維護(hù)的,因此需要顯式操作來同步 Menus 和 role_menu_table 的關(guān)聯(lián)數(shù)據(jù):
if err := config.DB.Model(&role).Association("Menus").Replace(menus); err != nil {
return err
}刪除時,要先刪除關(guān)聯(lián)。First時也要Preload,否則會clear失敗導(dǎo)致最終刪除失敗
//找到實(shí)例并刪除
if err := tx.Preload("Menus").First(&role, id).Error; err != nil {
return err
}
//刪除關(guān)聯(lián)Menus
if len(role.Menus) > 0 {
if err := tx.Model(&role).Association("Menus").Clear(); err != nil {
return err
}
}
//刪除實(shí)例
if err := tx.Unscoped().Delete(&role).Error; err != nil {
return err
}到此這篇關(guān)于golang實(shí)現(xiàn)動態(tài)路由的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)golang 動態(tài)路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用go-kit組件進(jìn)行服務(wù)注冊與發(fā)現(xiàn)和健康檢查的操作
這篇文章主要介紹了利用go-kit組件進(jìn)行服務(wù)注冊與發(fā)現(xiàn)和健康檢查的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
Go語言 channel如何實(shí)現(xiàn)歸并排序中的merge函數(shù)詳解
這篇文章主要給大家介紹了關(guān)于Go語言 channel如何實(shí)現(xiàn)歸并排序中merge函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02
十個Golang開發(fā)中應(yīng)該避免的錯誤總結(jié)
Go是一種靜態(tài)類型的、并發(fā)的、垃圾收集的編程語言,由谷歌開發(fā)。開發(fā)人員在編寫Go代碼時總會有一些常見的錯誤,下面是Go語言中需要避免的十大壞錯誤,希望對大家有所幫助2023-03-03

