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);
		}
	}

}

  

  

 

上一篇:LTC 主网钱包linux安装(付款的加密货币 基于区块链技术)


下一篇:spring-boot使用tools-redis实现分布式锁