springboot歌手详情代码,要求,点击歌手名称跳转到歌手详情页面展示歌手名称、歌手简介、歌曲数、专辑数、所有歌曲和专辑
时间: 2023-07-10 17:21:16 浏览: 66
以下是一个简单的Spring Boot歌手详情页面的代码示例:
首先,需要创建一个歌手实体类:
```java
@Entity
public class Singer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String biography;
private int songCount;
private int albumCount;
@OneToMany(mappedBy = "singer")
private List<Song> songs;
@OneToMany(mappedBy = "singer")
private List<Album> albums;
// getters and setters
}
```
然后,创建一个控制器类,用于处理请求和返回响应:
```java
@Controller
public class SingerController {
@Autowired
private SingerRepository singerRepository;
@GetMapping("/singers")
public String listSingers(Model model) {
List<Singer> singers = singerRepository.findAll();
model.addAttribute("singers", singers);
return "singer/list";
}
@GetMapping("/singers/{id}")
public String showSinger(@PathVariable Long id, Model model) {
Optional<Singer> optionalSinger = singerRepository.findById(id);
if (optionalSinger.isPresent()) {
Singer singer = optionalSinger.get();
model.addAttribute("singer", singer);
return "singer/show";
} else {
return "redirect:/singers";
}
}
}
```
在这个控制器类中,我们定义了两个请求处理方法。 `listSingers()` 方法用于列出所有歌手,而 `showSinger()` 方法用于显示特定歌手的详细信息。在 `showSinger()` 方法中,我们通过ID从数据库中获取歌手对象,并将其传递给视图。
接下来,我们需要创建两个视图,用于显示歌手列表和歌手详细信息。在 `list.html` 视图中,我们将使用Thymeleaf模板引擎来迭代所有歌手,并将它们的名称和ID链接到 `show.html` 视图:
```html
<!-- list.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>List of Singers</title>
</head>
<body>
<h1>List of Singers</h1>
<ul>
<li th:each="singer : ${singers}">
<a th:href="@{/singers/__${singer.id}__}"><span th:text="${singer.name}"></span></a>
</li>
</ul>
</body>
</html>
```
在 `show.html` 视图中,我们将使用Thymeleaf模板引擎来显示歌手的详细信息,包括名称、简介、歌曲数、专辑数、所有歌曲和专辑:
```html
<!-- show.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${singer.name}"></title>
</head>
<body>
<h1 th:text="${singer.name}"></h1>
<p th:text="${singer.biography}"></p>
<p>Songs: <span th:text="${singer.songCount}"></span></p>
<p>Albums: <span th:text="${singer.albumCount}"></span></p>
<h2>Songs:</h2>
<ul>
<li th:each="song : ${singer.songs}">
<span th:text="${song.title}"></span>
</li>
</ul>
<h2>Albums:</h2>
<ul>
<li th:each="album : ${singer.albums}">
<span th:text="${album.title}"></span>
</li>
</ul>
</body>
</html>
```
最后,我们需要创建一个 `SingerRepository` 接口,用于与数据库进行交互:
```java
public interface SingerRepository extends JpaRepository<Singer, Long> {
}
```
这是一个非常基本的Spring Boot歌手详情页面代码示例,可以根据需要进行扩展和修改。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)