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外键依赖),并在有实现类public interface MyBaseMapper<T> extends Mapper<T>, MySqlMapper<T>,MyInsertListMapper<T> {} public interface AdvertMapper extends MyBaseMapper<Advert> {},如何往advert表添加数据
时间: 2023-09-28 13:05:40 浏览: 105
Flowserve-Advert Guidelines(9p)_大厂VI品牌视觉标准_外企业品牌手册.pdf
可以按照以下步骤来编写ORM对象并实现往advert表添加数据的操作:
1. 编写Advert、AdPosition和MediaFile的ORM对象:
```java
@Entity
@Table(name = "advert")
public class Advert {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false)
private String name;
@ManyToOne
@JoinColumn(name = "position_id")
private AdPosition position;
@ManyToOne
@JoinColumn(name = "media_id")
private MediaFile media;
@Column(nullable = false)
private String link;
@Column(nullable = false)
private Integer status;
// 省略getter和setter方法
}
@Entity
@Table(name = "ad_position")
public class AdPosition {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false)
private String name;
@OneToMany(mappedBy = "position")
private List<Advert> adverts;
// 省略getter和setter方法
}
@Entity
@Table(name = "media_file")
public class MediaFile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Integer type;
@Column(nullable = false)
private String url;
@OneToMany(mappedBy = "media")
private List<Advert> adverts;
// 省略getter和setter方法
}
```
2. 编写AdvertMapper接口,继承自MyBaseMapper,并添加添加广告数据的方法:
```java
public interface AdvertMapper extends MyBaseMapper<Advert> {
// 添加广告数据的方法
@Modifying
@Query(value = "INSERT INTO advert(name, position_id, media_id, link, status) " +
"VALUES(:name, :positionId, :mediaId, :link, :status)", nativeQuery = true)
int addAdvert(@Param("name") String name, @Param("positionId") Integer positionId,
@Param("mediaId") Integer mediaId, @Param("link") String link,
@Param("status") Integer status);
}
```
3. 在添加数据的地方,注入AdvertMapper,并调用addAdvert方法来添加广告数据:
```java
@Service
public class AdvertService {
@Autowired
private AdvertMapper advertMapper;
public void addAdvert(String name, Integer positionId, Integer mediaId, String link, Integer status) {
advertMapper.addAdvert(name, positionId, mediaId, link, status);
}
}
```
以上是使用JPA外键依赖的方式往advert表添加数据的一个示例。需要注意的是,由于使用了自定义的SQL语句,所以需要在addAdvert方法上添加@Modifying和@Query注解,并设置nativeQuery为true。同时,需要在SQL语句中使用表名和列名,而不是ORM对象的实体名和属性名。
阅读全文