CREATE TABLE `advert` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(100) COLLATE utf8mb4_bin NOT NULL COMMENT '广告名称', `position_id` int(20) NOT NULL COMMENT '广告位置ID', `media_id` int(20) NOT NULL COMMENT '广告图片ID', `link` varchar(500) COLLATE utf8mb4_bin NOT NULL COMMENT '广告链接', `status` int(1) unsigned zerofill NOT NULL COMMENT '状态,0:未启用,1:已启用,2:已结束', PRIMARY KEY (`id`) USING BTREE, KEY `FK_ad_image` (`media_id`), KEY `FK_ad_position` (`position_id`), CONSTRAINT `FK_ad_image` FOREIGN KEY (`media_id`) REFERENCES `media_file` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `FK_ad_position` FOREIGN KEY (`position_id`) REFERENCES `ad_position` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='广告表'; CREATE TABLE `ad_position` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(50) COLLATE utf8mb4_bin NOT NULL COMMENT '位置名称', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='广告位置表'; CREATE TABLE `media_file` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(30) COLLATE utf8mb4_bin NOT NULL COMMENT '媒体文件名称', `type` tinyint(1) NOT NULL COMMENT '媒体文件类型:0-图片,1-视频', `url` varchar(500) COLLATE utf8mb4_bin NOT NULL COMMENT '媒体文件URL地址', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='广告媒体文件表'; springboot项目用java写出三张表的orm对象(使用jpa外键依赖)
时间: 2023-08-12 11:23:50 浏览: 192
Flowserve-Advert Guidelines(9p)_大厂VI品牌视觉标准_外企业品牌手册.pdf
请看下面的代码:
Advert.java
```
@Entity
@Table(name = "advert")
public class Advert {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name", nullable = false)
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "position_id")
private AdPosition adPosition;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "media_id")
private MediaFile mediaFile;
@Column(name = "link", nullable = false)
private String link;
@Column(name = "status", nullable = false)
private Integer status;
// Getters and setters
}
```
AdPosition.java
```
@Entity
@Table(name = "ad_position")
public class AdPosition {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name", nullable = false)
private String name;
@OneToMany(mappedBy = "adPosition")
private List<Advert> adverts;
// Getters and setters
}
```
MediaFile.java
```
@Entity
@Table(name = "media_file")
public class MediaFile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "type", nullable = false)
private Integer type;
@Column(name = "url", nullable = false)
private String url;
@OneToMany(mappedBy = "mediaFile")
private List<Advert> adverts;
// Getters and setters
}
```
这里使用了 JPA 的注解来定义实体类,并使用外键依赖。其中,Advert 实体类使用了 @ManyToOne 注解,表示多个广告对应一个广告位置和一个媒体文件;AdPosition 和 MediaFile 实体类使用了 @OneToMany 注解,表示一个广告位置和一个媒体文件对应多个广告。注意,AdPosition 和 MediaFile 实体类中的 adverts 属性需要使用 mappedBy 属性来指定关联属性名,以建立关联关系。
阅读全文