采用Spring Boot+Thymeleaf+Mybatis技术框架开发一个球队信息管理模块的代码
时间: 2023-12-14 15:08:48 浏览: 43
以下是一个简单的球队信息管理模块的代码示例:
1. 实体类:
球队信息实体类 Team.java
```
public class Team {
private Integer id;
private String name;
// 省略getter和setter方法
}
```
球员信息实体类 Player.java
```
public class Player {
private Integer id;
private String name;
private Integer age;
private String position;
private String photoPath;
private Team team; // 球队信息
// 省略getter和setter方法
}
```
2. DAO层:
球队信息DAO接口 TeamMapper.java
```
public interface TeamMapper {
List<Team> getAllTeams();
Team getTeamById(Integer id);
void addTeam(Team team);
void updateTeam(Team team);
void deleteTeam(Integer id);
}
```
球员信息DAO接口 PlayerMapper.java
```
public interface PlayerMapper {
List<Player> getAllPlayers();
List<Player> getPlayersByTeamName(String teamName);
List<Player> getPlayersByName(String name);
Player getPlayerById(Integer id);
void addPlayer(Player player);
void updatePlayer(Player player);
void deletePlayer(Integer id);
}
```
3. Service层:
球队信息Service TeamService.java
```
@Service
public class TeamService {
@Autowired
private TeamMapper teamMapper;
public List<Team> getAllTeams() {
return teamMapper.getAllTeams();
}
public Team getTeamById(Integer id) {
return teamMapper.getTeamById(id);
}
public void addTeam(Team team) {
teamMapper.addTeam(team);
}
public void updateTeam(Team team) {
teamMapper.updateTeam(team);
}
public void deleteTeam(Integer id) {
teamMapper.deleteTeam(id);
}
}
```
球员信息Service PlayerService.java
```
@Service
public class PlayerService {
@Autowired
private PlayerMapper playerMapper;
public List<Player> getAllPlayers() {
return playerMapper.getAllPlayers();
}
public List<Player> getPlayersByTeamName(String teamName) {
return playerMapper.getPlayersByTeamName(teamName);
}
public List<Player> getPlayersByName(String name) {
return playerMapper.getPlayersByName(name);
}
public Player getPlayerById(Integer id) {
return playerMapper.getPlayerById(id);
}
public void addPlayer(Player player) {
playerMapper.addPlayer(player);
}
public void updatePlayer(Player player) {
playerMapper.updatePlayer(player);
}
public void deletePlayer(Integer id) {
playerMapper.deletePlayer(id);
}
}
```
4. Controller层:
球队信息Controller TeamController.java
```
@Controller
@RequestMapping("/team")
public class TeamController {
@Autowired
private TeamService teamService;
@GetMapping("/list")
public String list(Model model) {
List<Team> teams = teamService.getAllTeams();
model.addAttribute("teams", teams);
return "team/list";
}
@GetMapping("/add")
public String add(Model model) {
model.addAttribute("team", new Team());
return "team/edit";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable Integer id, Model model) {
Team team = teamService.getTeamById(id);
model.addAttribute("team", team);
return "team/edit";
}
@PostMapping("/save")
public String save(Team team) {
if (team.getId() == null) {
teamService.addTeam(team);
} else {
teamService.updateTeam(team);
}
return "redirect:/team/list";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable Integer id) {
teamService.deleteTeam(id);
return "redirect:/team/list";
}
}
```
球员信息Controller PlayerController.java
```
@Controller
@RequestMapping("/player")
public class PlayerController {
@Autowired
private PlayerService playerService;
@GetMapping("/list")
public String list(Model model) {
List<Player> players = playerService.getAllPlayers();
model.addAttribute("players", players);
return "player/list";
}
@GetMapping("/add")
public String add(Model model) {
List<Team> teams = teamService.getAllTeams();
model.addAttribute("player", new Player());
model.addAttribute("teams", teams);
return "player/edit";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable Integer id, Model model) {
List<Team> teams = teamService.getAllTeams();
Player player = playerService.getPlayerById(id);
model.addAttribute("player", player);
model.addAttribute("teams", teams);
return "player/edit";
}
@PostMapping("/save")
public String save(Player player, @RequestParam("file") MultipartFile file) throws IOException {
if (!file.isEmpty()) {
String photoPath = "upload/" + file.getOriginalFilename();
File dest = new File(photoPath);
file.transferTo(dest);
player.setPhotoPath(photoPath);
}
if (player.getId() == null) {
playerService.addPlayer(player);
} else {
playerService.updatePlayer(player);
}
return "redirect:/player/list";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable Integer id) {
playerService.deletePlayer(id);
return "redirect:/player/list";
}
}
```
5. Thymeleaf视图:
球队信息list.html
```
<table>
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="team : ${teams}">
<td th:text="${team.id}"></td>
<td th:text="${team.name}"></td>
<td>
<a th:href="@{/team/edit/{id}(id=${team.id})}">编辑</a>
<a th:href="@{/team/delete/{id}(id=${team.id})}">删除</a>
</td>
</tr>
</tbody>
</table>
<a th:href="@{/team/add}">添加球队</a>
```
球队信息edit.html
```
<form method="post" enctype="multipart/form-data" th:object="${team}" th:action="@{/team/save}">
<input type="hidden" th:field="*{id}">
<input type="text" th:field="*{name}">
<button type="submit">保存</button>
</form>
```
球员信息list.html
```
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>位置</th>
<th>球队</th>
<th>照片</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="player : ${players}">
<td th:text="${player.id}"></td>
<td th:text="${player.name}"></td>
<td th:text="${player.age}"></td>
<td th:text="${player.position}"></td>
<td th:text="${player.team.name}"></td>
<td><img th:src="@{|/${player.photoPath}|}" width="50" height="50"/></td>
<td>
<a th:href="@{/player/edit/{id}(id=${player.id})}">编辑</a>
<a th:href="@{/player/delete/{id}(id=${player.id})}">删除</a>
</td>
</tr>
</tbody>
</table>
<a th:href="@{/player/add}">添加球员</a>
```
球员信息edit.html
```
<form method="post" enctype="multipart/form-data" th:object="${player}" th:action="@{/player/save}">
<input type="hidden" th:field="*{id}">
<input type="text" th:field="*{name}">
<input type="text" th:field="*{age}">
<input type="text" th:field="*{position}">
<select th:field="*{team.id}">
<option th:each="team : ${teams}" th:value="${team.id}" th:text="${team.name}"></option>
</select>
<input type="file" name="file">
<button type="submit">保存</button>
</form>
```
以上是一个简单的球队信息管理模块的代码示例,具体的实现过程需要根据实际情况进行具体的编写和调试。同时,为了提高开发效率,可以使用一些开源组件,比如MyBatis Plus、EasyExcel等,来简化开发流程。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)