🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 案例一 find查找 [推荐] ``` type HS_USER_TREE struct { TREE_ID string DEPT_ID string DEPT_CODE string USER_ID string USER_NAME string POSITION_ID string USER_ORDER_ID string } type USER_TREE_WITH_USER struct { HS_USER_TREE `xorm:"extends"` HS_USER HS_USER `xorm:"extends"` } type HS_USER struct { USER_ID string USER_LOGIN string USER_NAME string USER_STATUS string USER_ORDER_ID string } //通过dept 获取 users func GetUserByDeptid(deptId interface{}) ([]USER_TREE_WITH_USER, int64) { trees := make([]USER_TREE_WITH_USER, 0) num, e := Saas.Table(new(HS_USER_TREE)).Join("INNER", new(HS_USER), "HS_USER_TREE.USER_ID=HS_USER.USER_ID").Where(builder.Eq{"HS_USER.USER_STATUS": "1"}).FindAndCount(&trees, &HS_USER_TREE{DEPT_ID: deptId.(string)}) if e != nil { beego.Error("通过dept 获取 users error ", e) } return trees, num } ``` ## 案例一 get 查找 [推荐] 省略其他代码,与find 相同,关键在于查询条件那个当作 `Table()`中的表名 ``` dept := new(USER_TREE_WITH_DEPT) dept.HS_USER_TREE.USER_ID = userId _, e := Saas.Table(new(HS_USER_TREE)).Join("INNER", new(HS_DEPT), "HS_DEPT.DEPT_ID=HS_USER_TREE.DEPT_ID").Get(dept) ``` ## 案例三[不推荐] 存在的问题是 所有调用 `HS_USER_TREE`都会受到`HS_USER`的影响,所以并不推荐 ``` type HS_USER_TREE struct { TREE_ID string DEPT_ID string DEPT_CODE string USER_ID string USER_NAME string POSITION_ID string USER_ORDER_ID string HS_USER HS_USER `xorm:"extends"` } type HS_USER struct { USER_ID string USER_LOGIN string USER_NAME string USER_STATUS string USER_ORDER_ID string } //通过dept 获取 users func GetUserByDeptid(deptId interface{}) ([]HS_USER_TREE, int64) { trees := make([]HS_USER_TREE, 0) //方法一 使用where num, e := Saas.Join("INNER", new(HS_USER), "HS_USER_TREE.USER_ID=HS_USER.USER_ID").In(new(HS_USER_TREE).DEPT_ID, deptId).FindAndCount(&trees) //方法二 单个表的条件 num, e := Saas.Join("INNER", new(HS_USER), "HS_USER_TREE.USER_ID=HS_USER.USER_ID").FindAndCount(&trees, &HS_USER{USER_NAME: "赵昂然"}) //方法三 多张表的条件 num, e := Saas.Join("INNER", new(HS_USER), "HS_USER_TREE.USER_ID=HS_USER.USER_ID").FindAndCount(&trees, &HS_USER_TREE{DEPT_ID: deptId.(string), HS_USER: HS_USER{USER_NAME: "赵昂然"}}) if e != nil { beego.Error("通过dept 获取 users error ", e) } return trees, num } ``` ```