SpringDataJpa的批量 保存 修改 操作

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

原文地址:https://www.cnblogs.com/matd/p/10613213.html