使用Java编程和springboot框架实现对奖品表的管理功能 其中包括文件上传功能给出相应的controller层,service层,daoc层及mapper
时间: 2024-03-12 20:50:08 浏览: 86
DAOC QBar Saver and SC Gem Loader:DAOC QBar Saver和SC宝石装载机-开源
好的,我可以为您提供一个简单的实现,但请注意这只是一个示例,并不一定适用于您的具体情况。
首先,在 pom.xml 文件中添加以下依赖项:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
```
接下来,我们需要创建一个奖品类 Prize:
```java
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Prize {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String description;
private String imageUrl;
}
```
然后,我们需要创建一个 PrizeRepository 接口:
```java
public interface PrizeRepository extends JpaRepository<Prize, Long> {
}
```
接下来,我们需要创建一个 PrizeService 类:
```java
@Service
public class PrizeService {
private final PrizeRepository prizeRepository;
public PrizeService(PrizeRepository prizeRepository) {
this.prizeRepository = prizeRepository;
}
public List<Prize> getAllPrizes() {
return prizeRepository.findAll();
}
public Prize getPrizeById(Long id) {
return prizeRepository.findById(id).orElse(null);
}
public void savePrize(Prize prize) {
prizeRepository.save(prize);
}
public void deletePrize(Long id) {
prizeRepository.deleteById(id);
}
}
```
然后,我们需要创建一个 PrizeController 类:
```java
@Controller
public class PrizeController {
private final PrizeService prizeService;
public PrizeController(PrizeService prizeService) {
this.prizeService = prizeService;
}
@GetMapping("/")
public String index(Model model) {
model.addAttribute("prizes", prizeService.getAllPrizes());
return "index";
}
@GetMapping("/prize/{id}")
public String getPrize(@PathVariable Long id, Model model) {
Prize prize = prizeService.getPrizeById(id);
model.addAttribute("prize", prize);
return "prize";
}
@GetMapping("/prize/new")
public String newPrize(Model model) {
model.addAttribute("prize", new Prize());
return "new_prize";
}
@PostMapping("/prize/new")
public String createPrize(@ModelAttribute Prize prize, @RequestParam("image") MultipartFile image) throws IOException {
if (!image.isEmpty()) {
byte[] bytes = image.getBytes();
String fileName = image.getOriginalFilename();
Path path = Paths.get("uploads/" + fileName);
Files.write(path, bytes);
prize.setImageUrl(fileName);
}
prizeService.savePrize(prize);
return "redirect:/";
}
@GetMapping("/prize/edit/{id}")
public String editPrize(@PathVariable Long id, Model model) {
Prize prize = prizeService.getPrizeById(id);
model.addAttribute("prize", prize);
return "edit_prize";
}
@PostMapping("/prize/edit/{id}")
public String updatePrize(@PathVariable Long id, @ModelAttribute Prize prize, @RequestParam("image") MultipartFile image) throws IOException {
Prize existingPrize = prizeService.getPrizeById(id);
if (existingPrize != null) {
existingPrize.setName(prize.getName());
existingPrize.setDescription(prize.getDescription());
if (!image.isEmpty()) {
byte[] bytes = image.getBytes();
String fileName = image.getOriginalFilename();
Path path = Paths.get("uploads/" + fileName);
Files.write(path, bytes);
existingPrize.setImageUrl(fileName);
}
prizeService.savePrize(existingPrize);
}
return "redirect:/";
}
@GetMapping("/prize/delete/{id}")
public String deletePrize(@PathVariable Long id) {
prizeService.deletePrize(id);
return "redirect:/";
}
}
```
最后,我们需要创建相应的 HTML 模板:
index.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Prize List</title>
</head>
<body>
<h1>Prize List</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Image</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="prize : ${prizes}">
<td th:text="${prize.id}"></td>
<td th:text="${prize.name}"></td>
<td th:text="${prize.description}"></td>
<td>
<img th:src="@{uploads/} + ${prize.imageUrl}" width="100" height="100"/>
</td>
<td>
<a th:href="@{/prize/{id}(id=${prize.id})}">View</a>
<a th:href="@{/prize/edit/{id}(id=${prize.id})}">Edit</a>
<a th:href="@{/prize/delete/{id}(id=${prize.id})}">Delete</a>
</td>
</tr>
</tbody>
</table>
<a th:href="@{/prize/new}">Add New Prize</a>
</body>
</html>
```
prize.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Prize Details</title>
</head>
<body>
<h1 th:text="${prize.name}"></h1>
<img th:src="@{uploads/} + ${prize.imageUrl}" width="300" height="300"/>
<p th:text="${prize.description}"></p>
<a th:href="@{/}">Back to List</a>
</body>
</html>
```
new_prize.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>New Prize</title>
</head>
<body>
<h1>New Prize</h1>
<form th:action="@{/prize/new}" method="post" enctype="multipart/form-data">
<div>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</div>
<div>
<label for="description">Description:</label>
<textarea id="description" name="description" required></textarea>
</div>
<div>
<label for="image">Image:</label>
<input type="file" id="image" name="image">
</div>
<button type="submit">Save</button>
</form>
<a th:href="@{/}">Back to List</a>
</body>
</html>
```
edit_prize.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Edit Prize</title>
</head>
<body>
<h1>Edit Prize</h1>
<form th:action="@{/prize/edit/{id}(id=${prize.id})}" method="post" enctype="multipart/form-data">
<div>
<label for="name">Name:</label>
<input type="text" id="name" name="name" th:value="${prize.name}" required>
</div>
<div>
<label for="description">Description:</label>
<textarea id="description" name="description" required th:text="${prize.description}"></textarea>
</div>
<div>
<label for="image">Image:</label>
<input type="file" id="image" name="image">
</div>
<button type="submit">Update</button>
</form>
<a th:href="@{/}">Back to List</a>
</body>
</html>
```
这样,我们就完成了奖品表的管理功能。如果您需要更详细的代码,请告诉我。
阅读全文