关于Jpa的一些常见

jpa操作可分为两种

1.使用@query注解自己写SQL语句

需要注意,要使用数据库中的原生操作语句时需要加上 nativeQuery = true ;

e.g.  

@Query(value = "SELECT * FROM branch_user where is_teacher = '1'",nativeQuery = true)
1.1进行update/delete操作时需要额外加上@Modifying, 并且要在方法上添加 @Transactional,
否则会报
 
 

Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException

e.g.

@Transactional
@PostMapping("/branch/branchStudent/attendance/update")
public YsResponse  attendanceUpdate(@RequestParam("studentId")Long studentId,
    @RequestParam("attendanceStatus")Integer attendanceStatus)
 
 
@Modifying
@Query("update BranchStudentAttendance  set attendanceStatus = ?2  where studentId = ?1")
int updateByStudentId(Long studentId,Integer attendanceStatus);
2.也可直接保存对象完成操作 
 
 


 

猜你喜欢

转载自blog.csdn.net/weixin_41917449/article/details/80909661