【问题标题】:How to fetch data from mysql within given condition using spring MVC如何使用spring MVC在给定条件下从mysql获取数据
【发布时间】:2018-12-20 07:34:21
【问题描述】:

这是我的服务实现方法:

@Override
    public List<Question> getQuestion(Long questionId, int page, int records, String organizationId) throws InvalidIdException {

        Organization existingOrganization=organizationRepository.findOne(siteUtil.getDecryptedId(organizationId));
        Pageable pageInfo=new PageRequest(page-1,records);
        Page<com.oAssessment.entity.Question> existingQuestions=questionRepository.findAllByOrganizationAndDeleted
                (existingOrganization,false,pageInfo);
        List<Question> existingQuestion = new ArrayList<>();
        if(existingQuestions.hasContent()) {
            Iterator<com.oAssessment.entity.Question> existingQustionIterator = existingQuestions.iterator();
            while(existingQustionIterator.hasNext()) {
                com.oAssessment.entity.Question newQuestion = existingQustionIterator.next();
                Question question = new Question();
                if(newQuestion.getPkQuestionId()!=null) {
                    question.setSerialNo(newQuestion.getSerialNo());
                    question.setQuestionText(newQuestion.getText());
                    question.setDifLevel(newQuestion.getDifLevel());
                    question.setTypeOfquestion(newQuestion.getTypeOfQuestion());
                    existingQuestion.add(question);
                }
            }
        }
        return existingQuestion;
    }

而 jpa 存储库就像

@Repository
public interface QuestionRepository extends JpaRepository<Question, Long> {

    Page<Question> findAllByOrganizationAndDeleted(Organization existingOrganization, boolean b, Pageable pageInfo);

}

由此,所有数据都来自数据库。但我只想设置一些条件来过滤掉数据。就像我想从给定主题的数据库中获取一些问题一样,毫无疑问,像这样的问题难度级别。但是数据应该随机来自数据库。请帮帮我。

【问题讨论】:

    标签: mysql spring-mvc jpa spring-data-jpa repository


    【解决方案1】:

    如果您想过滤掉Spring Data 中的数据,您可以使用SpecificationsQuery by ExampleQuerydsl Extension,我建议您使用Specifications,因为我认为它更强大。您可以查看Spring Data JPA - Reference Documentation 了解更多信息。

    对于随机排序数据,我认为您不能在 Spring Data 中这样做,因为 AFAIK 并非所有数据库都具有该功能。你先找到你过滤的问题的总数,然后随机查询一个页面怎么样?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 2019-09-10
      相关资源
      最近更新 更多