目录
/* @NotNull :作用于基本数据类型 * @NotEmpty 作用于集合 * @NotBlank 作用于字符串 */
- package com.zking.oa.model;
-
- import sun.plugin2.message.Message;
-
- import javax.validation.constraints.NotBlank;
- import javax.validation.constraints.NotNull;
-
- /**
- * @NotNull :作用于基本数据类型
- * @NotEmpty 作用于集合
- * @NotBlank 作用于字符串
- */
- public class Clazz {
- @NotNull(message="cid不能为空")
- private Integer cid;
- @NotBlank(message="班级不能为空")
- private String cname;
-
- @NotBlank(message="教员不能为空")
- private String cteacher;
-
- private String pic;
-
- public Clazz(Integer cid, String cname, String cteacher, String pic) {
- this.cid = cid;
- this.cname = cname;
- this.cteacher = cteacher;
- this.pic = pic;
- }
-
- public Clazz() {
- super();
- }
-
- public Integer getCid() {
- return cid;
- }
-
- public void setCid(Integer cid) {
- this.cid = cid;
- }
-
- public String getCname() {
- return cname;
- }
-
- public void setCname(String cname) {
- this.cname = cname;
- }
-
- public String getCteacher() {
- return cteacher;
- }
-
- public void setCteacher(String cteacher) {
- this.cteacher = cteacher;
- }
-
- public String getPic() {
- return pic;
- }
-
- public void setPic(String pic) {
- this.pic = pic;
- }
- }
使用:
.jsr303服务daunt校验
1.pom导入
2.在待校验的数据库对应的实体类属性打上校验标签
非空校验
3.在controller层方法上添加@Valid
注释配合前面校验标签台添加Bindingresult次对象包含了所有,校验位通过的错误信息
4可以将所有错误信息以集合方式保存并且传递到前台界面展示
pom.xml
org.hibernate hibernate-validator 6.0.7.Final
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>org.example</groupId>
- <artifactId>ssm3</artifactId>
- <version>1.0-SNAPSHOT</version>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- <maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
-
- <!--添加jar包依赖-->
- <!--1.spring 5.0.2.RELEASE相关-->
- <spring.version>5.0.2.RELEASE</spring.version>
- <!--2.mybatis相关-->
- <mybatis.version>3.4.5</mybatis.version>
- <!--mysql-->
- <mysql.version>5.1.44</mysql.version>
- <!--pagehelper分页jar依赖-->
- <pagehelper.version>5.1.2</pagehelper.version>
- <!--mybatis与spring集成jar依赖-->
- <mybatis.spring.version>1.3.1</mybatis.spring.version>
- <!--3.dbcp2连接池相关 druid-->
- <commons.dbcp2.version>2.1.1</commons.dbcp2.version>
- <commons.pool2.version>2.4.3</commons.pool2.version>
- <!--4.log日志相关-->
- <log4j2.version>2.9.1</log4j2.version>
- <!--5.其他-->
- <junit.version>4.12</junit.version>
- <servlet.version>4.0.0</servlet.version>
- <lombok.version>1.18.2</lombok.version>
- </properties>
-
- <dependencies>
- <!--1.spring相关-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>