日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

jpa的批量修改_SpringDataJpa的批量 保存 修改 操作

發布時間:2025/3/21 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jpa的批量修改_SpringDataJpa的批量 保存 修改 操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SpringDataJpa進行修改數據庫操作有兩種方式:

一、調用保存實體的方法

1、保存一個實體:repository.save(T entity)

2、保存多個實體:repository.save(Iterable 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 {

// @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 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 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操作。

總結

以上是生活随笔為你收集整理的jpa的批量修改_SpringDataJpa的批量 保存 修改 操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。