我们可以将 MongoRespository 用于 findByNameAndPassword() 这种类型的查询吗?

Can we use MongoRespository for findByNameAndPassword() this type of queries?

Optional<TicketUserDto> ticketuser=ticketUserDao.findByUserIdAndPassword(userId, password);

道:

@Repository
public interface TicketUsersDao extends MongoRepository<TicketUserDto, Serializable> {
    Optional<TicketUserDto> findByUserIdAndPassword(String userId, String password);
}

这里是 DTO 定义

@Document(collection="ticket_users") 
public class TicketUserDto { 
    private String userId; 
    private String password; 

    public String getUserId() { return userId; } 
    public void setUserId(String userId) { this.userId = userId; } 

    public String getPassword() { return password; } 
    public void setPassword(String password) { this.password = password; }

    @Override public String toString() { 
        return "TicketUserDto [userId=" + userId + 
            ", password=" + password + "]"; 
    } 
}

您的查找方法应如下所示:

Optional<TicketUserDto> findOptionalByUserIdAndPassword(String userId, String password);

此外,您的 class 没有声明主键。为了让它工作,你应该重构你的 classes 如下:

@Document(collection="ticket_users") 
public class TicketUserDto { 
    @Id
    private String userId; 
    /* the other code remains untouched */
}

@Repository
public interface TicketUsersDao extends MongoRepository<TicketUserDto, String> {
    Optional<TicketUserDto> findOptionalByUserIdAndPassword(String userId, String password);
}

终于明白了 我添加了@field 然后我得到了值

 @Field(value="userId")
private String userId;
@Field(value="password")
private String password;