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', start_date datetime NOT NULL COMMENT '开始日期', end_date datetime NOT NULL COMMENT '结束日期', link varchar(500) COLLATE utf8mb4_bin NOT NULL COMMENT '广告链接', status int(1) unsigned zerofill NOT NULL COMMENT '状态,0:未启用,1:已启用,2:已结束', create_time datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', update_time datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (id) USING BTREE, KEY FK_ad_position (position_id), KEY FK_ad_image (media_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 CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB 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地址', create_time datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', update_time datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='广告媒体文件表'; CREATE TABLE ad_position ( id int(20) NOT NULL COMMENT '主键', name varchar(50) COLLATE utf8mb4_bin NOT NULL COMMENT '位置名称', description varchar(200) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '位置描述', width int(4) NOT NULL COMMENT '广告位置宽度', height int(4) NOT NULL COMMENT '广告位置高度', create_time datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', update_time datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='广告位置表'; 这三张表在进行对广告添加和修改操作时,前端传值和存储数据,用springboot项目如何实现
时间: 2023-11-29 16:06:16 浏览: 142
首先需要在Spring Boot项目中设置数据库连接,可以使用Spring Boot自带的JDBC或者MyBatis框架。然后根据需求编写对应的Controller、Service、Dao层代码。以下是一个简单的示例:
1. Controller层代码:
```java
@RestController
@RequestMapping("/advert")
public class AdvertController {
@Autowired
private AdvertService advertService;
@PostMapping("/add")
public ResponseResult addAdvert(@RequestBody AdvertDto advertDto) {
advertService.addAdvert(advertDto);
return ResponseResult.success();
}
@PostMapping("/update")
public ResponseResult updateAdvert(@RequestBody AdvertDto advertDto) {
advertService.updateAdvert(advertDto);
return ResponseResult.success();
}
// 其他接口
}
```
2. Service层代码:
```java
@Service
public class AdvertServiceImpl implements AdvertService {
@Autowired
private AdvertDao advertDao;
@Override
public void addAdvert(AdvertDto advertDto) {
Advert advert = convertToAdvert(advertDto);
advertDao.addAdvert(advert);
}
@Override
public void updateAdvert(AdvertDto advertDto) {
Advert advert = convertToAdvert(advertDto);
advertDao.updateAdvert(advert);
}
// 其他方法
private Advert convertToAdvert(AdvertDto advertDto) {
// 将AdvertDto转换为Advert对象
}
}
```
3. Dao层代码:
```java
@Repository
public class AdvertDaoImpl implements AdvertDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addAdvert(Advert advert) {
String sql = "INSERT INTO advert(name, position_id, media_id, start_date, end_date, link, status) VALUES(?,?,?,?,?,?,?)";
jdbcTemplate.update(sql, advert.getName(), advert.getPositionId(), advert.getMediaId(), advert.getStartDate(),
advert.getEndDate(), advert.getLink(), advert.getStatus());
}
@Override
public void updateAdvert(Advert advert) {
String sql = "UPDATE advert SET name=?, position_id=?, media_id=?, start_date=?, end_date=?, link=?, status=? WHERE id=?";
jdbcTemplate.update(sql, advert.getName(), advert.getPositionId(), advert.getMediaId(), advert.getStartDate(),
advert.getEndDate(), advert.getLink(), advert.getStatus(), advert.getId());
}
// 其他方法
}
```
其中,AdvertDto是一个数据传输对象,用于接收前端传来的数据。Advert是一个实体类,对应数据库表中的一条记录。以上示例仅供参考,具体实现需要根据实际情况进行调整。
阅读全文