Skip to content

Commit 2949e09

Browse files
JeffLi1993liqiangqiang
authored andcommitted
表单校验案例
1 parent ea64704 commit 2949e09

File tree

13 files changed

+522
-0
lines changed

13 files changed

+522
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>spring.boot.core</groupId>
7+
<artifactId>chapter-4-spring-boot-validating-form-input</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>chapter-4-spring-boot-validating-form-input</name>
12+
<description>第四章表单校验案例</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.0.M4</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-validation</artifactId>
32+
</dependency>
33+
34+
<!-- Web 依赖 - 包含了 hibernate-validator 依赖-->
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-web</artifactId>
38+
</dependency>
39+
40+
<!-- 单元测试依赖 -->
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
47+
<!-- Spring Data JPA 依赖 :: 数据持久层框架 -->
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-data-jpa</artifactId>
51+
</dependency>
52+
53+
<!-- h2 数据源连接驱动 -->
54+
<dependency>
55+
<groupId>com.h2database</groupId>
56+
<artifactId>h2</artifactId>
57+
<scope>runtime</scope>
58+
</dependency>
59+
60+
<!-- 模板引擎 Thymeleaf 依赖 -->
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
64+
</dependency>
65+
66+
</dependencies>
67+
68+
<build>
69+
<plugins>
70+
<!-- Spring Boot Maven 插件 -->
71+
<plugin>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-maven-plugin</artifactId>
74+
<version>1.5.1.RELEASE</version>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
79+
<repositories>
80+
<repository>
81+
<id>spring-milestones</id>
82+
<name>Spring Milestones</name>
83+
<url>https://repo.spring.io/libs-milestone</url>
84+
<snapshots>
85+
<enabled>false</enabled>
86+
</snapshots>
87+
</repository>
88+
</repositories>
89+
90+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package spring.boot.core;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ValidatingFormInputApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ValidatingFormInputApplication.class, args);
11+
}
12+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package spring.boot.core.domain;
2+
3+
import org.hibernate.validator.constraints.NotEmpty;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.Id;
8+
import javax.validation.constraints.Max;
9+
import javax.validation.constraints.Min;
10+
import javax.validation.constraints.NotNull;
11+
import javax.validation.constraints.Size;
12+
import java.io.Serializable;
13+
14+
/**
15+
* 用户实体类
16+
* <p>
17+
* Created by bysocket on 21/07/2017.
18+
*/
19+
@Entity
20+
public class User implements Serializable {
21+
22+
/**
23+
* 编号
24+
*/
25+
@Id
26+
@GeneratedValue
27+
private Long id;
28+
29+
/**
30+
* 名称
31+
*/
32+
@NotEmpty(message = "姓名不能为空")
33+
@Size(min = 2, max = 8, message = "姓名长度必须大于 2 且小于 20 字")
34+
private String name;
35+
36+
/**
37+
* 年龄
38+
*/
39+
@NotNull(message = "年龄不能为空")
40+
@Min(value = 0, message = "年龄大于 0")
41+
@Max(value = 300, message = "年龄不大于 300")
42+
private Integer age;
43+
44+
/**
45+
* 出生时间
46+
*/
47+
@NotEmpty(message = "出生时间不能为空")
48+
private String birthday;
49+
50+
public Long getId() {
51+
return id;
52+
}
53+
54+
public void setId(Long id) {
55+
this.id = id;
56+
}
57+
58+
public String getName() {
59+
return name;
60+
}
61+
62+
public void setName(String name) {
63+
this.name = name;
64+
}
65+
66+
public Integer getAge() {
67+
return age;
68+
}
69+
70+
public void setAge(Integer age) {
71+
this.age = age;
72+
}
73+
74+
public String getBirthday() {
75+
return birthday;
76+
}
77+
78+
public void setBirthday(String birthday) {
79+
this.birthday = birthday;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
return "User{" +
85+
"id=" + id +
86+
", name='" + name + '\'' +
87+
", age=" + age +
88+
", birthday=" + birthday +
89+
'}';
90+
}
91+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package spring.boot.core.domain;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
/**
6+
* 用户持久层操作接口
7+
*
8+
* Created by bysocket on 21/07/2017.
9+
*/
10+
public interface UserRepository extends JpaRepository<User, Long> {
11+
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package spring.boot.core.service;
2+
3+
4+
import spring.boot.core.domain.User;
5+
6+
import java.util.List;
7+
8+
/**
9+
* User 业务层接口
10+
*
11+
* Created by bysocket on 24/07/2017.
12+
*/
13+
public interface UserService {
14+
15+
List<User> findAll();
16+
17+
User insertByUser(User user);
18+
19+
User update(User user);
20+
21+
User delete(Long id);
22+
23+
User findById(Long id);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package spring.boot.core.service.impl;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
import spring.boot.core.domain.User;
8+
import spring.boot.core.domain.UserRepository;
9+
import spring.boot.core.service.UserService;
10+
11+
import java.util.List;
12+
13+
/**
14+
* User 业务层实现
15+
*
16+
* Created by bysocket on 24/07/2017.
17+
*/
18+
@Service
19+
public class UserServiceImpl implements UserService {
20+
21+
private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);
22+
23+
@Autowired
24+
UserRepository userRepository;
25+
26+
@Override
27+
public List<User> findAll() {
28+
return userRepository.findAll();
29+
}
30+
31+
@Override
32+
public User insertByUser(User user) {
33+
LOGGER.info("新增用户:" + user.toString());
34+
return userRepository.save(user);
35+
}
36+
37+
@Override
38+
public User update(User user) {
39+
LOGGER.info("更新用户:" + user.toString());
40+
return userRepository.save(user);
41+
}
42+
43+
@Override
44+
public User delete(Long id) {
45+
User user = userRepository.findById(id).get();
46+
userRepository.delete(user);
47+
48+
LOGGER.info("删除用户:" + user.toString());
49+
return user;
50+
}
51+
52+
@Override
53+
public User findById(Long id) {
54+
LOGGER.info("获取用户 ID :" + id);
55+
return userRepository.findById(id).get();
56+
}
57+
}

0 commit comments

Comments
 (0)