@Transactional注释声明该方法是事务性操作,如果Query语句执行的时候出现问题,将会回滚到执行前的状态,DELETE和UPDATE方法必须要加@Transactional
@Modifying注释声明该方法是修改操作,select语句不用该注释,要注意的是:方法的返回值应该是int或者void,如果是Int表示更新语句所影响的行数
如下:
@Query(value = " update sorts set levels = length(sortPath)- length(replace(sortPath,',','')) - 1 where\n" + " sortPath like concat('%',:#{#sorts.newSortPath},'%') and id <> :#{#sorts.id}", nativeQuery = true) @Transactional @Modifying public int updateAllChildSortsLevels(@Param("sorts")Sorts sorts);