Java SpringBoot-html表显示过期数据
我有SpringBoot项目,我正在使用JPA。 我的一个控制器返回一个页面“myclass.html”,上面有一个表Java SpringBoot-html表显示过期数据,java,spring,spring-boot,transactions,Java,Spring,Spring Boot,Transactions,我有SpringBoot项目,我正在使用JPA。 我的一个控制器返回一个页面“myclass.html”,上面有一个表 @Secured("ROLE_USER") @Controller public class UserController { @RequestMapping("/myClass") public String myClass(Model model) { model.addAttribute("students", studentService.findCu
@Secured("ROLE_USER")
@Controller
public class UserController {
@RequestMapping("/myClass")
public String myClass(Model model) {
model.addAttribute("students", studentService.findCurrentStudents());
return "user/myclass";
}
}
studentService做了两件事(通过在UserRepository上调用两个单独的方法):
- 它会更新STUDENTS表,根据实际日期进行一些更改
- 它返回学生表
@Override
public List<Student> findCurrentStudents() {
studentRepository.updateByUserAndDate(currentUserId(), new Date());
return studentRepository.findCurrentStudentsByUser(currentUserId());
}
@覆盖
公共列表findCurrentStudents(){
updateByUserAndDate(currentUserId(),new Date());
return studentRepository.findCurrentStudentsByUser(currentUserId());
}
以及存储库:
@Repository
public interface StudentRepository extends CrudRepository<Student, Long> {
@Query(value="SELECT * FROM Students WHERE user_id = :currentUserId, nativeQuery = true)
List<Student> findCurrentStudentsByUser(@Param("userId") Integer currentUserId);
@Transactional
@Modifying
@Query(value="UPDATE Students SET last_review = :newDate WHERE user_id = :userId", nativeQuery = true)
void updateByUserAndDate(@Param("userId")Long id, @Param("newDate") Date date);
@存储库
公共界面StudentRepository扩展了crudepository{
@查询(value=“SELECT*FROM Students,其中user\u id=:currentUserId,nativeQuery=true)
列出FindCurrentStudentsByser(@Param(“userId”)整数currentUserId);
@交易的
@修改
@查询(value=“updatestudents SET last\u review=:newDate其中user\u id=:userId”,nativeQuery=true)
void updateByUserAndDate(@Param(“userId”)长id,@Param(“newDate”)Date;
是否需要将它们封装到单个事务中:是:与数据库的每次交互都应该在一个事务中完成。使用@Transactional
注释您的服务或控制器。不知道存储库的代码无助于解释问题。我已添加了存储库的代码。不幸的是@事务性注释并没有解决这个问题(我在顶部的服务实现中添加了注释),但希望存储库的代码能够向您解释。您是否尝试过在浏览器的开发人员工具中禁用缓存?是的,很遗憾,我已经禁用了缓存。