Spring|springboot实体类注解

User

package com.rzy.sbtwebdemo.model; import javax.persistence.*; import java.util.Date; @Entity @Table(name="t_users") public class User { privateint id; privateString username; privateString password; private Date regDate; privateAddress address; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public int getId() { return id; }public void setId(int id) { this.id = id; }public String getUsername() { return username; }public void setUsername(String username) { this.username = username; }public String getPassword() { return password; }public void setPassword(String password) { this.password = password; } @Column(name="reg_date") public Date getRegDate() { return regDate; }public void setRegDate(Date regDate) { this.regDate = regDate; }@ManyToOne @JoinColumn(name="address_id") public Address getAddress() { return address; }public void setAddress(Address address) { this.address = address; }@Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", regData="https://www.it610.com/article/+ regDate +", address=" + address + '}'; } }

注解 @Entity:表明User是一个实体类,它默认对应数据库中的表名是user,你也可以指定@Entity(name = "t_users"),或者@Table(name="t_users") @Id注释指定表的主键, @GeneratedValue(strategy = GenerationType.IDENTITY)自增长主键生成策略
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)

@Column注释定义了将成员属性映射到关系表中的哪一列和该列的结构信息,如@Column(name="reg_date"),将实体类regDate对应数据库中的reg_date。
@Column(name="reg_date")

@ManyToOne:多(User)对一(Address) @JoinColumn(name=“address_id”):指定Address主键
@ManyToOne @JoinColumn(name="address_id")

Address
package com.rzy.sbtwebdemo.model; import javax.persistence.*; @Entity @Table(name="t_address") public class Address { private int id; private String addressInfo; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public int getId() { return id; }public void setId(int id) { this.id = id; } @Column(name="address_info") public String getAddressInfo() { return addressInfo; }public void setAddressInfo(String addressInfo) { this.addressInfo = addressInfo; }@Override public String toString() { return "Address{" + "id=" + id + ", addressInfo='" + addressInfo + '\'' + '}'; } }

【Spring|springboot实体类注解】标记为实体类,指定其对应表t_address
@Entity @Table(name="t_address")

标记为主键,指定为自增长主键生成策略
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)

将addrss中的addressInfo对应与表address_info
@Column(name="address_info")

    推荐阅读