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

时间:2018-10-02 08:53:54

标签: java spring spring-boot spring-data-jpa mongorepository

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

DAO:

@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 + "]"; 
    } 
}

2 个答案:

答案 0 :(得分:0)

Your find method should look as it follows:

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

Also, your class does not declare a primary key. To make it work you should refactor your classes as it follows:

@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);
}

答案 1 :(得分:0)

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

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