JPA EntityManager批量保存/删除

Posted dream_heheda

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPA EntityManager批量保存/删除相关的知识,希望对你有一定的参考价值。

实体类

import java.math.BigDecimal;
import java.util.Date;
  
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
    
import lombok.Data;
    
@Data
@Entity
@Table(name = "tb_student",
       indexes =  @Index(name = "index_tb_student", columnList = "classNo,studentName"),
                   @Index(name = "index_student_studentName", columnList = "studentName"),
                   @Index(name = "index_student_studentAge", columnList = "studentAge") )
public class Student 
      
    @Id
    private String studentId;
    
    @Column
    private String studentName;
    
    @Column
    private Integer studentAge;
    
    @Column
    private String classNo;
      
    @Column(precision = 18, scale = 4)
    private BigDecimal height;
      
    @Column
    private Date lastModiftyDate;
      
    @Column
    private String classId; //手动维护tb_class表和tb_student之间的关系
    

Service层

import java.util.List;
 
public interface StudentService 
 
    void saveAll(List<Student> students);
 
    void deleteAll(List<Student> students);
     
import java.util.List;
 
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
 
import org.springframework.stereotype.Service;
 
@Service
public class StudentServiceImpl implements StudentService 
 
    @PersistenceContext
    private EntityManager entityManager;
 
    @Override
    public void saveAll(List<Student> students) 
        for (Student student : students) 
            entityManager.persist(student);
        
 
    
 
    @Override
    public void deleteAll(List<Student> students) 
        for (Student student : students) 
            entityManager.remove(student);
        
    
 

以上是关于JPA EntityManager批量保存/删除的主要内容,如果未能解决你的问题,请参考以下文章

Spring Data JPA : 批量增删

Spring Data JPA : 批量增删

Spring Data JPA : 批量增删

使用 JPA EntityManager 进行批量插入

删除操作的JPA和DAO实现

Spring Boot-------JPA——EntityManager构建通用DAO