SpringBoot + JPA EntityManager 实现batch 增删

SpringBoot + JPA 实现batch 增删,提高效率

 

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	@Transactional
	public void saveAll(List<Student> students) {
		for (Student student : students) {
			entityManager.persist(student);
		}

	}

	@Override
	@Transactional
	public void deleteAll(List<Student> students) {
		for (Student student : students) {
			entityManager.remove(student);
		}

	}
}

  

posted on 2022-04-20 18:01  dreamstar  阅读(253)  评论(0编辑  收藏  举报