jpa 分布式主键
相关注解
@Target({ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(GenericGenerators.class)
public @interface GenericGenerator {
String name();
String strategy();
Parameter[] parameters() default {};
}
*************************************
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface GeneratedValue {
GenerationType strategy() default GenerationType.AUTO;
String generator() default "";
}
使用示例
@Entity
public class Teacher {
private String id;
private String name;
private Integer age;
@Id
@Column(name = "id")
@GenericGenerator(name="uuid",strategy = "org.hibernate.id.UUIDGenerator")
@GeneratedValue(generator = "uuid")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "age")
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
。。。。
}
***************************************************
controller层
@RestController
public class TeacherController {
@Autowired
private TeacherRepository teacherRepository;
@RequestMapping("/save")
public String save(){
for(int i=0;i<100;i++){
Teacher teacher=new Teacher();
teacher.setName("瓜田李下"+i);
teacher.setAge(i);
teacherRepository.save(teacher);
}
return "success";
}
}
插入结果: