SpringBoot Jpa 批量插入同一个对象

1.@Entity类实现 Cloneable 接口并重写 clone()方法

@Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();

    }

@Entiry

public class Student implements  Cloneable{

     @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;

    private String name;

    private int classid;

//SetAndGet method

 @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    } 

}

2.调用clone()方法

for(String [] name:names){

    Student stu = student.clone();

    stu.setName(name);

    studentRepository.save(stu); 

}

新方法:https://blog.csdn.net/SZStudy/article/details/81735635

 

你可能感兴趣的:(SpringBoot Jpa 批量插入同一个对象)