Building REST services with Spring
软件体系结构课程需要学习spring的这篇教程,做一下,顺便翻译一下。
开始
在spring initializer中选择下列依赖:
- web web核心
- jpa 数据库交互
- h2 数据库
将名字改为Payroll,选择合适的其余选项,下载即可。(安装好adoptjdk)。
我们将要写一个员工管理的微服务。
下方代码定义了一个 Employee
nonrest/src/main/java/payroll/Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| package payroll;
import java.util.Objects;
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;
@Entity class Employee {
private @Id @GeneratedValue Long id; private String name; private String role;
Employee() {}
Employee(String name, String role) {
this.name = name; this.role = role; }
public Long getId() { return this.id; }
public String getName() { return this.name; }
public String getRole() { return this.role; }
public void setId(Long id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setRole(String role) { this.role = role; }
@Override public boolean equals(Object o) {
if (this == o) return true; if (!(o instanceof Employee)) return false; Employee employee = (Employee) o; return Objects.equals(this.id, employee.id) && Objects.equals(this.name, employee.name) && Objects.equals(this.role, employee.role); }
@Override public int hashCode() { return Objects.hash(this.id, this.name, this.role); }
@Override public String toString() { return "Employee{" + "id=" + this.id + ", name='" + this.name + '\'' + ", role='" + this.role + '\'' + '}'; } }
|