package dao import ( "time" ) type UserEntity struct { ID int64 Username string Password string Email string CreatedAt time.Time } func (UserEntity) TableName() string { return "user" } type UserDAO interface { SelectByEmail(email string)(*UserEntity, error) Save(user *UserEntity) error } type UserDAOImpl struct { } func (userDAO *UserDAOImpl) SelectByEmail(email string)(*UserEntity, error) { user := &UserEntity{} err := db.Where("email = ?", email).First(user).Error return user, err } func (userDAO *UserDAOImpl) Save(user *UserEntity) error { return db.Create(user).Error } func (userDAO *UserDAOImpl) SelectPassByUserName(username string)(*UserEntity, error) { user := &UserEntity{} err := db.Where("username = ?", username).First(user).Error return user, err } func (userDAO *UserDAOImpl) SelectPassByUserNameV2(username string)(*[]UserEntity, error) { users := make([]UserEntity,0) //err := db.Where("username = ?", username).First(user).Error row,err := db.Raw("select * from user").Rows() //row1:= db.Raw("select * from user") for row.Next(){ user := &UserEntity{} err = row.Scan(&user.ID,&user.Username,&user.Password,&user.Email,&user.CreatedAt) if err != nil{ println(err.Error()) } users = append(users,*user) } row.Close() return &users, err } func (userDAO *UserDAOImpl) SelectPassByUserNameV3(username string)(*[]UserEntity, error) { users := make([]UserEntity,0) //err := db.Where("username = ?", username).First(user).Error db.Where("username = 'aoho'").Limit(1).Offset(1).Find(&users) //row1:= db.Raw("select * from user") return &users, nil }