用java语言编写一个基于Springboot框架的奖学金评定管理信息系统
时间: 2024-06-09 07:11:24 浏览: 119
好的,下面是基于Spring Boot框架的奖学金评定管理信息系统的简单实现。
首先,需要安装和配置Java、Maven和Spring Boot环境。
1. 创建Maven项目
使用Maven命令行或者IDEA等工具创建一个Maven项目,设置groupId和artifactId等信息。
2. 引入依赖
在pom.xml文件中添加Spring Boot相关依赖。
```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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
</dependencies>
```
3. 配置数据源
在application.properties文件中设置数据库连接信息。
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/scholarship?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=create-drop
```
4. 创建实体类
在src/main/java/com/example/scholarship/entity目录下创建Scholarship实体类。
```java
@Entity
@Table(name = "scholarship")
public class Scholarship {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String type;
private Double amount;
// getter and setter
}
```
5. 创建数据访问接口
在src/main/java/com/example/scholarship/repository目录下创建ScholarshipRepository接口。
```java
@Repository
public interface ScholarshipRepository extends JpaRepository<Scholarship, Long> {
}
```
6. 创建控制器
在src/main/java/com/example/scholarship/controller目录下创建ScholarshipController控制器。
```java
@Controller
@RequestMapping("/scholarship")
public class ScholarshipController {
@Autowired
private ScholarshipRepository scholarshipRepository;
@GetMapping("/list")
public String list(Model model) {
List<Scholarship> scholarships = scholarshipRepository.findAll();
model.addAttribute("scholarships", scholarships);
return "list";
}
@GetMapping("/create")
public String create(Model model) {
Scholarship scholarship = new Scholarship();
model.addAttribute("scholarship", scholarship);
return "edit";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Long id, Model model) {
Scholarship scholarship = scholarshipRepository.findById(id).orElse(null);
model.addAttribute("scholarship", scholarship);
return "edit";
}
@PostMapping("/save")
public String save(@ModelAttribute("scholarship") Scholarship scholarship) {
scholarshipRepository.save(scholarship);
return "redirect:/scholarship/list";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Long id) {
scholarshipRepository.deleteById(id);
return "redirect:/scholarship/list";
}
}
```
7. 创建视图模板
在src/main/resources/templates目录下创建list.html和edit.html两个视图模板。
list.html
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Scholarship List</title>
</head>
<body>
<h2>Scholarship List</h2>
<table border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Type</td>
<td>Amount</td>
<td>Actions</td>
</tr>
<tr th:each="scholarship : ${scholarships}">
<td th:text="${scholarship.id}"></td>
<td th:text="${scholarship.name}"></td>
<td th:text="${scholarship.type}"></td>
<td th:text="${scholarship.amount}"></td>
<td>
<a th:href="@{/scholarship/edit/{id}(id=${scholarship.id})}">Edit</a>
<a th:href="@{/scholarship/delete/{id}(id=${scholarship.id})}">Delete</a>
</td>
</tr>
</table>
<br>
<a href="/scholarship/create">Create Scholarship</a>
</body>
</html>
```
edit.html
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Edit Scholarship</title>
</head>
<body>
<h2>Edit Scholarship</h2>
<form th:action="@{/scholarship/save}" th:object="${scholarship}" method="post">
<input type="hidden" th:field="*{id}">
<div>
<label for="name">Name:</label>
<input type="text" th:field="*{name}" id="name">
</div>
<div>
<label for="type">Type:</label>
<input type="text" th:field="*{type}" id="type">
</div>
<div>
<label for="amount">Amount:</label>
<input type="number" th:field="*{amount}" id="amount">
</div>
<div>
<button type="submit">Save</button>
<a href="/scholarship/list">Cancel</a>
</div>
</form>
</body>
</html>
```
8. 运行程序
使用Maven命令行或者IDEA等工具启动Spring Boot应用程序,访问http://localhost:8080/scholarship/list即可看到奖学金列表页面。
9. 测试
在浏览器中访问http://localhost:8080/scholarship/create,填写奖学金信息并提交,然后返回奖学金列表页面,可以看到新增的奖学金信息已经显示在列表中了。
以上就是基于Spring Boot框架的奖学金评定管理信息系统的简单实现。
阅读全文