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")
推荐阅读
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别
- jar|springboot项目打成jar包和war包,并部署(快速打包部署)
- 数据库|效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】)...
- java人生|35K 入职华为Java开发那天,我哭了(这 5 个月做的一切都值了)
- Java毕业设计项目实战篇|Java项目:在线嘿嘿网盘系统设计和实现(java+Springboot+ssm+mysql+maven)
- 微服务|微服务系列:服务发现与注册-----Eureka(面试突击!你想了解的Eureka都在这里.持续更新中......)
- java|ApplicationListener和SpringApplicationRunListener的联系
- Spring|SpringSecurity--自定义登录页面、注销登录配置
- 性能|性能工具之 Jmeter 通过 SpringBoot 工程启动
- 代码狂魔|Spring源码分析之IOC容器初始化流程