springboot实现首页轮播图并在另一页面对轮播图进行增删改查后首页轮播图更新的代码
时间: 2024-04-16 09:20:40 浏览: 131
springboot增删查改功能实现
以下是使用Spring Boot实现首页轮播图并在另一页面对轮播图进行增删改查后首页轮播图更新的代码示例:
1. 创建数据库表结构:
```sql
CREATE TABLE `biz_slideshow` (
`id` int(11) NOT NULL COMMENT 'id',
`img_url` varchar(255) DEFAULT NULL COMMENT '图片地址',
`sort` int(11) DEFAULT NULL COMMENT '显示顺序',
`type` varchar(2) DEFAULT NULL COMMENT '类型(扩展不同模块拥有轮播图:首页、商品、个人信息)',
`description` varchar(255) DEFAULT NULL COMMENT '图片描述(扩展)',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='轮播图信息表';
```
2. 创建实体类 Slideshow.java:
```java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Slideshow {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String imgUrl;
private Integer sort;
private String type;
private String description;
private LocalDateTime createTime;
// 省略构造方法、getter和setter
}
```
3. 创建轮播图数据访问接口 SlideshowRepository.java:
```java
import org.springframework.data.jpa.repository.JpaRepository;
public interface SlideshowRepository extends JpaRepository<Slideshow, Long> {
// 可自定义查询方法,如根据类型查询轮播图
}
```
4. 创建轮播图服务类 SlideshowService.java:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SlideshowService {
private final SlideshowRepository slideshowRepository;
@Autowired
public SlideshowService(SlideshowRepository slideshowRepository) {
this.slideshowRepository = slideshowRepository;
}
public List<Slideshow> getAllSlideshows() {
return slideshowRepository.findAll();
}
public Slideshow saveSlideshow(Slideshow slideshow) {
return slideshowRepository.save(slideshow);
}
public void deleteSlideshow(Long id) {
slideshowRepository.deleteById(id);
}
// 可根据需要添加其他操作方法,如更新轮播图
}
```
5. 创建控制器类 SlideshowController.java:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/slideshow")
public class SlideshowController {
private final SlideshowService slideshowService;
@Autowired
public SlideshowController(SlideshowService slideshowService) {
this.slideshowService = slideshowService;
}
@GetMapping("/")
public List<Slideshow> getAllSlideshows() {
return slideshowService.getAllSlideshows();
}
@PostMapping("/")
public Slideshow saveSlideshow(@RequestBody Slideshow slideshow) {
return slideshowService.saveSlideshow(slideshow);
}
@DeleteMapping("/{id}")
public void deleteSlideshow(@PathVariable Long id) {
slideshowService.deleteSlideshow(id);
}
// 可根据需要添加其他操作方法,如更新轮播图
}
```
6. 在首页页面中使用轮播图数据:
```html
<!-- 在页面中显示轮播图 -->
<div id="slideshow">
<!-- 使用轮播图数据渲染 -->
<div th:each="slideshow : ${slideshows}">
<img th:src="${slideshow.imgUrl}" alt="Slideshow Image">
<p th:text="${slideshow.description}"></p>
</div>
</div>
```
7. 在另一页面中对轮播图进行增删改查:
```html
<!-- 添加轮播图 -->
<form action="/slideshow/" method="post">
<input type="text" name="imgUrl" placeholder="图片地址">
<input type="number" name="sort" placeholder="显示顺序">
<input type="text" name="type" placeholder="类型">
<input type="text" name="description" placeholder="图片描述">
<button type="submit">添加轮播图</button>
</form>
<!-- 删除轮播图 -->
<form action="/slideshow/{id}" method="post">
<input type="hidden" name="_method" value="delete">
<button type="submit">删除轮播图</button>
</form>
<!-- 更新轮播图 -->
<form action="/slideshow/{id}" method="post">
<input type="hidden" name="_method" value="put">
<input type="text" name="imgUrl" placeholder="图片地址">
<input type="number" name="sort" placeholder="显示顺序">
<input type="text" name="type" placeholder="类型">
<input type="text" name="description" placeholder="图片描述">
<button type="submit">更新轮播图</button>
</form>
<!-- 查询轮播图 -->
<a href="/slideshow/">查询轮播图</a>
```
阅读全文