@Transactional注釋聲明該方法是事務(wù)性操作,如果Query語句執(zhí)行的時(shí)候出現(xiàn)問題,將會(huì)回滾到執(zhí)行前的狀態(tài),DELETE和UPDATE方法必須要加@Transactional
@Modifying注釋聲明該方法是修改操作,select語句不用該注釋,要注意的是:方法的返回值應(yīng)該是int或者void,如果是Int表示更新語句所影響的行數(shù)
如下:
@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);