Java 如何使用spring、JPARepository向postgres插入多个条目
就像我有一个ArrayList, 然后我从服务中调用我的回购,有点像Java 如何使用spring、JPARepository向postgres插入多个条目,java,spring,postgresql,spring-data-jpa,Java,Spring,Postgresql,Spring Data Jpa,就像我有一个ArrayList, 然后我从服务中调用我的回购,有点像 userRepository.insertUsers(userArrayList); 这是我的存储库 @Repository public interface UserRepository extends JpaRepository<User, Long> { @Transactional @Modifying() @Query("INSERT INTO usermaster ..........")
userRepository.insertUsers(userArrayList);
这是我的存储库
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Transactional
@Modifying()
@Query("INSERT INTO usermaster ..........")
public int insertUsers(@Param("userArrayList") List<User> userArrayList);
}
@存储库
公共接口用户存储库扩展了JpaRepository{
@交易的
@修改()
@查询(“插入用户主控…………”)
公共int插入器(@Param(“userArrayList”)列表userArrayList);
}
我只需要一个查询就可以在上面批量插入此ArrayList,我找不到答案。
提前感谢。您不需要自定义方法,JpaRepository有保存方法,可以接受您的实体列表,这就足够了
userRepository.save(userArrayList);
您不需要自定义方法,JpaRepository具有接受您的实体列表的save方法,这就足够了
userRepository.save(userArrayList);
您是否尝试过使用默认方法
保存
?如下所示:userRepository.save(userArrayList)
在Spring数据存储库接口上实现时,不需要@Repository
注释。您是否尝试过使用默认方法save
?如下所示:userRepository.save(userArrayList)
在Spring数据存储库接口上实现时,不需要@Repository
注释。