在spring jpa中从多个表中获取数据

时间:2017-02-04 06:02:51

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

到目前为止,我正在使用它如下所示从多个表中获取数据:

@RequestMapping(path = "/get_data_on_login", method = RequestMethod.GET)
public ResponseEntity get_data_on_login(@RequestParam(value="username") String username) throws Exception {
    List<TopicBean> topics = topicService.findAllTopics();
    UserInfo role = userInfoService.findRoleByUsername(username);
    return new ResponseEntity(new LoginDataBean( topics, role.getRole(), role.getUserImage()),  HttpStatus.OK);
}

但随着从更多数量的表中获取数据的需求增加,它变得越来越复杂。

我正在寻找的是获取这样的数据,但我不确定这是否正确:

public interface LoginRepository extends JpaRepository<LoginDataBean, Long>{

    List<LoginDataBean> SearchByTopicBeanAndCommentBeanAndCommentLikes(TopicBean topicBean, CommentBean commentBean, CommentLikes commentLikes);

}

我有这样的LoginDataBean:

public class LoginDataBean {

    @Autowired
    private final List<TopicBean> topics;

    private String userImage;

    private String role;

    public LoginDataBean(List<TopicBean> topics, String role, String userImage){
        this.topics = topics;
        this.role = role;
        this.userImage = userImage;
    }

    public String getRole(){
        return role;
    }

    public String userImage(){
        return userImage;
    }

    public List<TopicBean> getTopics() {
        return topics;
    }
}

0 个答案:

没有答案