SpringDataJpa進行修改數據庫操作有兩種方式:
一、調用保存實體的方法
1、保存一個實體:repository.save(T entity)
2、保存多個實體:repository.save(Iterable<T> entitys)
3、保存一個實體並立即刷新更改:repository.saveAndFlush(T entity)
注意事項:保存對象時需要確定 PRIMARY KEY和唯一索引。否則會報出“Duplicate entry '1-2-0' for key”這樣的錯誤。
修改對象時,也使用如上方法,但需要確定PRIMARY KEY,如果PRIMARY KEY不存在,則是添加操作。
二、@Query注解(寫JPQL語句)
JPQL( Java 持久性查詢語言)JPQL 和 SQL 的主要區別在於,前者處理JPA 實體、屬性,后者直接在數據庫空間內對表、列、行等關系數據進行處理。
JPQL解釋:https://blog.csdn.net/qq_33746131/article/details/56479226
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
Repositoryk中@Query寫JPQL語句:@Query("JPQL語句")
例1 修改操作
@Modifying
@Transactional
@Query("update CityStationGoods csg set csg.isOnsale = ?2 where csg.id = ?1")
int updateOnSaleState(int id, Boolean isOnsale);
例2 使用參數下標
@Modifying
@Transactional
@Query("delete from GoodsActivity ga where ga.activityId = ?1")
void deleteByActivityId(Integer activityId);
例3 使用參數名
@Modifying
@Transactional
@Query("delete from GoodsActivity ga where ga.activityId = :id")
void deleteByActivityId(@Param(value = "id")Integer activityId);
Repositoryk中@Query寫SQL語句:@Query(value="SQL語句",nativeQuery = true)
例1
@Query(value = "SELECT IFNULL(SUM(num),0) FROM shopping_cart WHERE member_id =?1", nativeQuery = true)
int getCartNum(Integer memberId);
注意事項:查詢時不需要@Modifying注解。@Modifying:指示方法應被視為修改查詢。
@Transactional注解:在update或delete時,需要事務提交。如果不寫Transactional無法將修改后的操作保存到數據庫中。該注解可以寫在Service或Repository中。
In findByIdIn(Collection<?> c) where id in (?)
試驗了一下,可以滿足我的需求。先貼代碼
package com.yd.lipstick.dao.write;
import com.yd.lipstick.entity.Position;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
@Repository
public interface PositionWriteDao extends JpaRepository<Position,Long> {
// @Modifying
// @Transactional
// @Query(value = "update Position p set p.status=2 where p.deviceId=?1 and p.positionId in (?2)")
// int update(String deviceId, Collection<String> collection);
@Modifying
@Transactional
@Query(value = "update Position p set p.status=2 where p.deviceId=:deviceId and p.positionId in (:collection)")
int update(@Param("deviceId") String deviceId, @Param("collection") Collection<String> collection);
}
貼出來的兩種update實現的功能是一樣的。
第一種使用的是索引參數:索引值從1開始,查詢中"?X"個數需要與方法定義的參數個數相一致,並且順序也要一致。
注釋:上面代碼中的?1,?2表示參數的占位符,需要和方法中所傳遞的參數順序一致。X是從1開始。
第二種使用的是命名參數(推薦使用此方式):可以定義好參數名,賦值時使用@Param("參數名"),而不用管順序。
注釋:上面代碼中:devideId ,:collection 表示為參數命名,方法中所傳遞的參數使用@Param注解標識命名參數。這種方式不用管參數的順序。
@Modifying注解
1、在@Query注解中編寫JPQL實現DELETE和UPDATE操作的時候必須加上@modifying注解,以通知Spring Data 這是一個DELETE或UPDATE操作。
2、UPDATE或者DELETE操作需要使用事務,此時需要 定義Service層,在Service層的方法上添加事務操作。
3、注意JPQL不支持INSERT操作。