这是一个简单的学生成绩管理系统的需求,以下是一个基于JSP、MySQL、MyBatis和Spring Boot的简单示例代码。
数据库设计(MySQL):
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`grade` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`subject` varchar(100) NOT NULL,
`score` int(3) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_student_idx` (`student_id`),
CONSTRAINT `fk_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`)
);
实体类(Java):
// Student.java
public class Student {
private int id;
private String name;
private int grade;
// getters and setters
}
// Score.java
public class Score {
private int id;
private int studentId;
private String subject;
private int score;
// getters and setters
}
Mapper接口(MyBatis):
// StudentMapper.java
public interface StudentMapper {
Student selectStudentById(int id);
int insertStudent(Student student);
// ...
}
// ScoreMapper.java
public interface ScoreMapper {
Score selectScoreById(int id);
int insertScore(Score score);
// ...
}
服务层(Spring Boot):
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
// 学生管理方法
public Student addStudent(Student student) {
// 业务逻辑
return studentMapper.insertStudent(student);
}
// ...
}
@Service
public class ScoreService {
@Autowired
private ScoreMapper scoreMapper;
// 成绩管理方法
public Score addScore(Score score) {
// 业务逻辑
return scoreMapper.insertScore(score);
}
// ...
}
控制器(Spring Boot):
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
// 添加学生
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute Student student) {
studentService.addStudent(student);
return "