请将上一个回答中的四张表的orm对象在springboot的项目中使用java注解演示出来
时间: 2024-03-03 19:48:03 浏览: 64
好的,以下是在 Spring Boot 项目中使用 Java 注解定义四张表的 ORM 对象示例:
1. 广告表 `advert` 的 ORM 对象:
```java
@Entity
@Table(name = "advert")
public class Advert {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "start_date")
private Date startDate;
@Column(name = "end_date")
private Date endDate;
@Column(name = "link")
private String link;
@Column(name = "status")
private Integer status;
@ManyToOne
@JoinColumn(name = "position_id")
private AdPosition position;
@ManyToOne
@JoinColumn(name = "media_id")
private MediaFile mediaFile;
// 省略 getter 和 setter 方法
}
```
2. 广告位置表 `ad_position` 的 ORM 对象:
```java
@Entity
@Table(name = "ad_position")
public class AdPosition {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "width")
private Integer width;
@Column(name = "height")
private Integer height;
// 省略 getter 和 setter 方法
}
```
3. 广告媒体表 `media_file` 的 ORM 对象:
```java
@Entity
@Table(name = "media_file")
public class MediaFile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "type")
private Integer type;
@Column(name = "url")
private String url;
@Column(name = "create_time")
private Date createTime;
@Column(name = "update_time")
private Date updateTime;
// 省略 getter 和 setter 方法
}
```
4. 广告点击表 `advert_click` 的 ORM 对象:
```java
@Entity
@Table(name = "advert_click")
public class AdvertClick {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne
@JoinColumn(name = "advert_id")
private Advert advert;
@Column(name = "click_time")
private Date clickTime;
@Column(name = "click_ip")
private String clickIp;
// 省略 getter 和 setter 方法
}
```
这里使用了 `@Entity` 注解来标记实体类,`@Table` 注解来指定对应的数据库表名称,`@Id` 注解来标记主键字段,`@GeneratedValue` 注解用于指定主键生成策略,`@Column` 注解用于指定字段对应的数据库列名,`@ManyToOne` 注解用于指定多对一关联关系,`@JoinColumn` 注解用于指定外键列名。
阅读全文