Spring Boot 增加刪除修改 批量


1.批量刪除

 a.自定義Repositoy中寫

 前台處理https://blog.csdn.net/yhflyl/article/details/81557670
首先前台先要獲取所有的要刪除數據的ID,並將ID拼接成字符串 例如: 2,3,4,5,然后通過GET請求返送到后台。

后台處理
控制器接收
/**
* @function 批量刪除
* @param stu_id
* @return
*/
@GetMapping("/del_stu")
@ResponseBody
public Msg batch_del_stu(@RequestParam("stu_id") String stu_id){
// 接收包含stuId的字符串,並將它分割成字符串數組
String[] stuList = stu_id.split(",");
// 將字符串數組轉為List<Intger> 類型
List<Integer> LString = new ArrayList<Integer>();
for(String str : stuList){
LString.add(new Integer(str));
}
// 調用service層的批量刪除函數
studentsService.deleteBatch(LString);
return Msg.success().add("數組", LString);
}


service層
@Override
public void deleteBatch(List<Integer> stuList) {
// 第一種批量刪除方法,是通過spring data中繼承JpaRepository接口后,通過關鍵字拼接的方法進行刪除,刪除時候是通過ID一條一條刪除的
// studentsRepository.deleteStudentsByStuIdIn(stuList);
// 第二種批量刪除方法, 是通過自定義JPQL語句進行刪除,使用的是 where stuId in ()的操作
studentsRepository.deleteBatch(stuList);
}

Repository接口層
public interface StudentsRepository extends Repository<Students, Integer>, JpaRepository<Students, Integer> {

/**
* @function 自定義JPQL
* @param ids
*/
// 注意更新和刪除是需要加事務的, 並且要加上 @Modify的注解
@Modifying
@Transactional
@Query("delete from Students s where s.stuId in (?1)")
void deleteBatch(List<Integer> ids);

// 這個是通過spring data拼接關鍵字進行的操作
void deleteStudentsByStuIdIn(List<Integer> ids);

}


附加
@Modifying注解

1、在@Query注解中編寫JPQL實現DELETE和UPDATE操作時候必須加上@Modifying注解,通知Spring Data這是一個delete或者updata操作

2、 updata和delete操作需要使用事務,此時需要定義service層,在service方法上添加事務操作

3、 注意JPQL不支持insert操作

@Query 如果在注解中添加 nativeQuery=true 是支持原生SQL查詢

b.通過JpaRepository 或者CrudRepository 自帶的

 

 

 

 

 

可以將前台的ids封裝為entitys 使用這些自帶的

如下

 

 

 

 

 

 

2.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中。(本例因測試學習,寫到了Repository中)  

 

Spring Data Jpa 更新操作

 

第一步,通過Repository對象把實體根據ID查詢出來

第二部,往查出來的實體對象進行set各個字段

第三步,通過Repository接口的save方法進行保存

 

 

保存和更新方式(已知兩種)

  • 第一種是通過@Query和@Modify注解進行更新,自己可在@Query注解的HQL或SQL片段中指定更新的字段
  • 第二種是通過ById查詢出來並進行設值,最后進行保存更新操作

 

3.新增:

看日志,JPA是先把所有的數據全查出來了,如果數據庫有就更新,沒有就新增。https://www.cnblogs.com/blog5277/p/10661096.html

saveall()   save()  就可以

不能寫insert 語句  jpa不支持

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM