我是spEL的新手,我想使用“SELECT COUNT(*)FROM Exams;”使用JPQL或spEL查询以检查数据库中的记录是否小于6(六); 这就是我所拥有的。我无法弄清楚如何去做,因为我无法在控制器中使用@Query注释。
//Problem writing spEL
<div class="form-group" th:if="${exams.count(*) < 6}" >
<form method="post" th:object="${newExam}" th:action="@{/exams}" class="inline new-item">
<label class="form-control input-sm"><input type="text" th:field="*{indexNumber}" placeholder="Index Number" autocomplete="off"/></label>
<select th:field="*{grade}" class="form-control input-sm">
<option value="" disabled="disabled">[Select Grade]</option>
<option th:each="grade : ${grades}" th:value="${grade.values}" th:text="${grade.name}">Grade</option>
</select>
<label class="form-control input-sm"><input type="text" th:field="*{courseOffered}" placeholder="CourseOffered" autocomplete="off"/></label>
<label class="form-control input-sm"><input type="text" th:field="*{examType}" placeholder="ExamType" autocomplete="off"/></label>
<label class="form-control input-sm"><input type="text" th:field="*{subject}" placeholder="Subject" autocomplete="off"/></label>
<label class="form-control input-sm datepicker"><input type="text" th:field="*{gradeYear}" placeholder="ExamYear" autocomplete="off"/></label>
<button type="submit" class="btn btn-primary">Add</button>
</form>
@Controller
public class ExamsController {
@Autowired
private ExamService examService;
@Autowired
private UserService userService;
@RequestMapping(path = "/cert_prog")
public String examsList(Model model){
Iterable<Exams> exams = examService.findAll();
model.addAttribute("exams", exams);
model.addAttribute("newExam", new Exams());
model.addAttribute("grades", Grade.values());
return "cert_prog";
}
@RequestMapping(path = "/mark", method = RequestMethod.POST)
public String toggleComplete(@RequestParam Long id) {
Exams exams = examService.findOne(id);
examService.toggleComplete(id);
return "redirect:/cert_prog";
}
@RequestMapping(path = "/exams", method = RequestMethod.POST)
public String addTask(@ModelAttribute Exams exams, Principal principal){
//User user = userService.findByUsername(principal.getName());
User user = (User)((UsernamePasswordAuthenticationToken)principal).getPrincipal();
exams.setUser(user);
//examService.increment(exams);
examService.save(exams);
return "redirect:/cert_prog";
}
}
@服务 公共类ExamServiceImpl实现ExamService {
@Autowired
private ExamsDao examsDao;
@Override
public Iterable<Exams> findAll() {
return examsDao.findAll();
}
@Override
public Exams findOne(Long id) {
return examsDao.findOne(id);
}
@Override
public void toggleComplete(Long id) {
Exams exams = examsDao.findOne(id);
exams.setComplete(!exams.isComplete());
examsDao.save(exams);
}
@Override
public void save(Exams exams) {
examsDao.save(exams);
}
@Override
public void increment(Exams exams) {
//exams.setCounter(exams.getCounter() + 1);
}
答案 0 :(得分:0)
您无需再次查询数据库。考试已经是所有考试记录的可迭代集合,不是吗?尝试
<div class="form-group" th:if="${exams.size() < 6}" >
只要Iterable对象的底层类实现了size()方法,百万美元就应该能够找到并调用它。否则,在将对象添加到模型之前,您需要转换为实现大小的集合。