生成代码:用java设计一个应用程序,该应用程序用于输入、修改、删除;读取某高中毕业班的考试成绩,教师角色可以对数据库中的数据进行输入,修改,删除;学生角色只能查找某个学生的所有成绩,输入学号之后即可展示该学号下所有科目的成绩及总成绩。
时间: 2024-03-23 15:36:38 浏览: 17
为了方便你理解,我将代码分为几个部分,并附上简要注释。注意,由于篇幅限制,以下代码并没有考虑细节和异常情况的处理,仅供参考。
1. 数据库设计
```
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`class` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`subject` varchar(50) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_score_student` (`student_id`),
CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
2. 后端逻辑
使用 Spring Boot 框架实现后端逻辑,以下是主要代码:
```
@RestController
@RequestMapping("/api")
public class ScoreController {
@Autowired
private StudentRepository studentRepository;
@Autowired
private ScoreRepository scoreRepository;
// 教师角色:添加学生
@PostMapping("/students")
public Student createStudent(@RequestBody Student student) {
return studentRepository.save(student);
}
// 教师角色:添加学生成绩
@PostMapping("/scores")
public Score createScore(@RequestBody Score score) {
return scoreRepository.save(score);
}
// 教师角色:删除学生
@DeleteMapping("/students/{id}")
public void deleteStudent(@PathVariable Long id) {
studentRepository.deleteById(id);
}
// 教师角色:删除学生成绩
@DeleteMapping("/scores/{id}")
public void deleteScore(@PathVariable Long id) {
scoreRepository.deleteById(id);
}
// 教师角色:修改学生成绩
@PutMapping("/scores/{id}")
public Score updateScore(@PathVariable Long id, @RequestBody Score updatedScore) {
Score score = scoreRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Score", "id", id));
score.setSubject(updatedScore.getSubject());
score.setScore(updatedScore.getScore());
return scoreRepository.save(score);
}
// 学生角色:获取某个学生的所有成绩
@GetMapping("/students/{id}/scores")
public List<Score> getScoresByStudentId(@PathVariable Long id) {
return scoreRepository.findByStudentId(id);
}
}
```
3. 前端界面
使用 Thymeleaf 模板引擎实现前端界面,以下是主要代码:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>学生成绩查询</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>学生成绩查询</h1>
<form>
<label>请输入学号:</label>
<input type="text" id="studentId" name="studentId" required>
<button type="button" onclick="query()">查询</button>
</form>
<table>
<thead>
<tr>
<th>学科</th>
<th>成绩</th>
</tr>
</thead>
<tbody id="scoreList"></tbody>
<tfoot>
<tr>
<td>总成绩</td>
<td id="totalScore"></td>
</tr>
</tfoot>
</table>
<script>
function query() {
var studentId = $('#studentId').val();
$.ajax({
url: '/api/students/' + studentId + '/scores',
success: function(scores) {
var totalScore = 0;
$('#scoreList').html('');
$.each(scores, function(index, score) {
totalScore += score.score;
$('#scoreList').append('<tr><td>' + score.subject + '</td><td>' + score.score + '</td></tr>');
});
$('#totalScore').text(totalScore);
},
error: function() {
alert('查询失败,请检查学号是否正确!');
}
});
}
</script>
</body>
</html>
```
以上是一个简单的实现示例,具体实现还需要考虑更多细节和异常情况的处理。