user_dao.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package dao
  2. import (
  3. "time"
  4. )
  5. type UserEntity struct {
  6. ID int64
  7. Username string
  8. Password string
  9. Email string
  10. CreatedAt time.Time
  11. }
  12. func (UserEntity) TableName() string {
  13. return "user"
  14. }
  15. type UserDAO interface {
  16. SelectByEmail(email string)(*UserEntity, error)
  17. Save(user *UserEntity) error
  18. }
  19. type UserDAOImpl struct {
  20. }
  21. func (userDAO *UserDAOImpl) SelectByEmail(email string)(*UserEntity, error) {
  22. user := &UserEntity{}
  23. err := db.Where("email = ?", email).First(user).Error
  24. return user, err
  25. }
  26. func (userDAO *UserDAOImpl) Save(user *UserEntity) error {
  27. return db.Create(user).Error
  28. }
  29. func (userDAO *UserDAOImpl) SelectPassByUserName(username string)(*UserEntity, error) {
  30. user := &UserEntity{}
  31. err := db.Where("username = ?", username).First(user).Error
  32. return user, err
  33. }
  34. func (userDAO *UserDAOImpl) SelectPassByUserNameV2(username string)(*[]UserEntity, error) {
  35. users := make([]UserEntity,0)
  36. //err := db.Where("username = ?", username).First(user).Error
  37. row,err := db.Raw("select * from user").Rows()
  38. //row1:= db.Raw("select * from user")
  39. for row.Next(){
  40. user := &UserEntity{}
  41. err = row.Scan(&user.ID,&user.Username,&user.Password,&user.Email,&user.CreatedAt)
  42. if err != nil{
  43. println(err.Error())
  44. }
  45. users = append(users,*user)
  46. }
  47. row.Close()
  48. return &users, err
  49. }
  50. func (userDAO *UserDAOImpl) SelectPassByUserNameV3(username string)(*[]UserEntity, error) {
  51. users := make([]UserEntity,0)
  52. //err := db.Where("username = ?", username).First(user).Error
  53. db.Where("username = 'aoho'").Limit(1).Offset(1).Find(&users)
  54. //row1:= db.Raw("select * from user")
  55. return &users, nil
  56. }