eclipse 用jsp文件连接mysql_JSP+SSM+Mysql实现的学生成绩管理系统
时间: 2024-01-01 15:06:20 浏览: 123
eclipse基于 JSP+Mysql 做一个简单的学生成绩查询web系统-附件资源
JSP+SSM+MySQL实现学生成绩管理系统的步骤如下:
1. 首先,需要配置MySQL数据库和Tomcat服务器,这里不再赘述。
2. 创建一个Java Web项目,导入SSM框架的相关依赖。
3. 在项目的src/main/resources目录下创建一个jdbc.properties文件,用于配置数据库连接信息,例如:
```
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=123456
```
其中,jdbc.url为连接数据库的URL,student为数据库名,root为用户名,123456为密码。
4. 创建一个数据表,例如:
```
CREATE TABLE `student_score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`math_score` int(11) DEFAULT NULL,
`chinese_score` int(11) DEFAULT NULL,
`english_score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
该表用于保存学生姓名、数学成绩、语文成绩、英语成绩等信息。
5. 编写DAO层代码,用于实现数据的增删改查操作。例如,创建一个StudentScoreDAO接口和StudentScoreDAOImpl实现类,代码如下:
StudentScoreDAO.java
```
public interface StudentScoreDAO {
void add(StudentScore studentScore);
void update(StudentScore studentScore);
void delete(int id);
StudentScore getById(int id);
List<StudentScore> list();
}
```
StudentScoreDAOImpl.java
```
@Repository
public class StudentScoreDAOImpl implements StudentScoreDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(StudentScore studentScore) {
String sql = "INSERT INTO student_score(name, math_score, chinese_score, english_score) VALUES (?, ?, ?, ?)";
jdbcTemplate.update(sql, studentScore.getName(), studentScore.getMathScore(), studentScore.getChineseScore(), studentScore.getEnglishScore());
}
@Override
public void update(StudentScore studentScore) {
String sql = "UPDATE student_score SET name=?, math_score=?, chinese_score=?, english_score=? WHERE id=?";
jdbcTemplate.update(sql, studentScore.getName(), studentScore.getMathScore(), studentScore.getChineseScore(), studentScore.getEnglishScore(), studentScore.getId());
}
@Override
public void delete(int id) {
String sql = "DELETE FROM student_score WHERE id=?";
jdbcTemplate.update(sql, id);
}
@Override
public StudentScore getById(int id) {
String sql = "SELECT * FROM student_score WHERE id=?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(StudentScore.class));
}
@Override
public List<StudentScore> list() {
String sql = "SELECT * FROM student_score";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(StudentScore.class));
}
}
```
6. 编写Service层代码,用于实现业务逻辑。例如,创建一个StudentScoreService接口和StudentScoreServiceImpl实现类,代码如下:
StudentScoreService.java
```
public interface StudentScoreService {
void add(StudentScore studentScore);
void update(StudentScore studentScore);
void delete(int id);
StudentScore getById(int id);
List<StudentScore> list();
}
```
StudentScoreServiceImpl.java
```
@Service
@Transactional
public class StudentScoreServiceImpl implements StudentScoreService {
@Autowired
private StudentScoreDAO studentScoreDAO;
@Override
public void add(StudentScore studentScore) {
studentScoreDAO.add(studentScore);
}
@Override
public void update(StudentScore studentScore) {
studentScoreDAO.update(studentScore);
}
@Override
public void delete(int id) {
studentScoreDAO.delete(id);
}
@Override
public StudentScore getById(int id) {
return studentScoreDAO.getById(id);
}
@Override
public List<StudentScore> list() {
return studentScoreDAO.list();
}
}
```
7. 编写Controller层代码,用于处理HTTP请求。例如,创建一个StudentScoreController类,代码如下:
```
@Controller
@RequestMapping("/student_score")
public class StudentScoreController {
@Autowired
private StudentScoreService studentScoreService;
@RequestMapping("/list")
public String list(Model model) {
List<StudentScore> studentScores = studentScoreService.list();
model.addAttribute("studentScores", studentScores);
return "list";
}
@RequestMapping("/to_add")
public String toAdd() {
return "add";
}
@RequestMapping("/add")
public String add(StudentScore studentScore) {
studentScoreService.add(studentScore);
return "redirect:/student_score/list";
}
@RequestMapping("/to_update")
public String toUpdate(int id, Model model) {
StudentScore studentScore = studentScoreService.getById(id);
model.addAttribute("studentScore", studentScore);
return "update";
}
@RequestMapping("/update")
public String update(StudentScore studentScore) {
studentScoreService.update(studentScore);
return "redirect:/student_score/list";
}
@RequestMapping("/delete")
public String delete(int id) {
studentScoreService.delete(id);
return "redirect:/student_score/list";
}
}
```
其中,@RequestMapping注解用于匹配请求的URL,例如/list表示获取学生成绩列表,/to_add表示跳转到添加学生成绩的页面,/add表示添加学生成绩,/to_update表示跳转到修改学生成绩的页面,/update表示修改学生成绩,/delete表示删除学生成绩。
8. 编写JSP页面,用于展示数据和接收用户的输入。例如,创建一个list.jsp页面,代码如下:
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生成绩列表</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>数学成绩</th>
<th>语文成绩</th>
<th>英语成绩</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${studentScores}" var="studentScore">
<tr>
<td>${studentScore.id}</td>
<td>${studentScore.name}</td>
<td>${studentScore.mathScore}</td>
<td>${studentScore.chineseScore}</td>
<td>${studentScore.englishScore}</td>
<td>
<a href="/student_score/to_update?id=${studentScore.id}">修改</a>
<a href="/student_score/delete?id=${studentScore.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<a href="/student_score/to_add">添加</a>
</body>
</html>
```
该页面用于展示学生成绩列表,同时提供添加、修改和删除操作的链接。
9. 创建其他页面,例如添加学生成绩的页面和修改学生成绩的页面,代码类似。
10. 部署项目到Tomcat服务器,启动项目,即可访问学生成绩管理系统。
阅读全文