dao-role.go 700 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package dao
  2. import (
  3. "exam_system/config"
  4. "exam_system/entity"
  5. "fmt"
  6. )
  7. func FindRoleByUserId(id int) []*entity.Role {
  8. sqlStr := `
  9. SELECT
  10. r.id,
  11. r.name
  12. FROM
  13. user u
  14. LEFT JOIN user_role ur ON ur.user_id = u.id
  15. LEFT JOIN role r ON ur.role_id = r.id
  16. where u.id=?
  17. GROUP BY r.id;
  18. `
  19. var r []*entity.Role
  20. err := config.DB.Select(&r, sqlStr, id)
  21. if err != nil {
  22. fmt.Printf("get failed, err:%v\n", err)
  23. return nil
  24. }
  25. return r
  26. }
  27. func FindRoleByName(roleName string) *entity.Role {
  28. sqlStr := "select id from role where name=? limit 1"
  29. var r entity.Role
  30. err := config.DB.Get(&r, sqlStr, roleName)
  31. if err != nil {
  32. fmt.Printf("get failed, err:%v\n", err)
  33. return nil
  34. }
  35. return &r
  36. }