如何将Spring MVC,Hibernate,Hibernate Validator一起使用?

时间:2018-12-05 21:24:15

标签: java spring hibernate

我想在我的项目中使用spring mvc-hibernate-hibernate验证器。但是在课堂上我应用了注释,验证和mysql注释是重叠的。通常会使用这两者。还是以其他方式完成?

@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
Integer id;

@Column(name="firstname")
@NotBlank(message="Name mustn't be empty.")
@Size(max=20,min=2,message="Your name must be between 2-20.")
@Pattern(regexp ="([a-zA-Z]+)",message="You can't enter number or symols or Turkish caracter.")
String firstname;

@Column(name="lastname")
@NotBlank(message="Last name mustn't be empty.")
@Size(max=20,min=2,message="Your lastname must be between 2-20.")
@Pattern(regexp ="([a-zA-Z]+)",message="You can't enter number or symols or Turkish caracter.")
String lastname;

@Column(name="date")
@NotBlank
@DateTimeFormat(pattern="yyyy-mm-dd")
@DateValid
String date;

1 个答案:

答案 0 :(得分:0)

您可以一起使用spring mvc,hibernate,hibernate验证程序,并且没有冲突。有一个名为JHipster的好项目,您可以了解如何使用这些技术来学习。

  

我们是否会使用这么多注释来造成代码混乱?

这是jhipster用户类别:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;

@NotNull
@Pattern(regexp = Constants.LOGIN_REGEX)
@Size(min = 1, max = 50)
@Column(length = 50, unique = true, nullable = false)
private String login;

@JsonIgnore
@NotNull
@Size(min = 60, max = 60)
@Column(name = "password_hash", length = 60, nullable = false)
private String password;

@Size(max = 50)
@Column(name = "first_name", length = 50)
private String firstName;

@Size(max = 50)
@Column(name = "last_name", length = 50)
private String lastName;

@Email
@Size(min = 5, max = 254)
@Column(length = 254, unique = true)
private String email;

@NotNull
@Column(nullable = false)
private boolean activated = false;

通常在Java中有两种配置类型

  • 注释
  • XML配置

有关更多信息,请访问annotation vs xml