用前端和后端java和数据库写一个学生管理系统
时间: 2024-03-21 21:44:34 浏览: 68
好的,这是一个较为复杂的项目,需要包含前端页面、后端 Java 代码和数据库操作。下面是一个简单的示例,仅供参考。
前端页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生管理系统</title>
<style>
table {
border-collapse: collapse;
margin: 20px 0;
}
table, th, td {
border: 1px solid black;
padding: 5px;
}
</style>
</head>
<body>
<h1>学生管理系统</h1>
<form>
<label>姓名:<input type="text" name="name"></label>
<label>年龄:<input type="number" name="age"></label>
<label>性别:<select name="gender">
<option value="male">男</option>
<option value="female">女</option>
</select></label>
<input type="button" value="添加" onclick="addStudent()">
</form>
<table id="studentTable">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</table>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
$(function() {
refreshStudentTable();
});
function addStudent() {
var name = $('input[name="name"]').val();
var age = $('input[name="age"]').val();
var gender = $('select[name="gender"]').val();
$.ajax({
url: '/addStudent',
method: 'POST',
data: {name: name, age: age, gender: gender},
success: function(result) {
if (result.code == 0) {
alert('添加成功!');
refreshStudentTable();
} else {
alert('添加失败:' + result.msg);
}
},
error: function(xhr, status, error) {
alert('添加失败:' + error);
}
});
}
function refreshStudentTable() {
$.ajax({
url: '/getStudentList',
method: 'GET',
success: function(result) {
if (result.code == 0) {
var studentTable = $('#studentTable');
studentTable.empty();
studentTable.append('<tr><th>ID</th><th>姓名</th><th>年龄</th><th>性别</th></tr>');
for (var i = 0; i < result.data.length; i++) {
var student = result.data[i];
studentTable.append('<tr><td>' + student.id + '</td><td>' + student.name + '</td><td>' + student.age + '</td><td>' + student.gender + '</td></tr>');
}
} else {
alert('获取学生信息失败:' + result.msg);
}
},
error: function(xhr, status, error) {
alert('获取学生信息失败:' + error);
}
});
}
</script>
</body>
</html>
```
后端 Java 代码(使用 Spring Boot 框架):
```java
@RestController
public class StudentController {
@Autowired
private StudentDao studentDao;
@PostMapping("/addStudent")
public Map<String, Object> addStudent(@RequestParam("name") String name,
@RequestParam("age") int age, @RequestParam("gender") String gender) {
Map<String, Object> result = new HashMap<>();
try {
Student student = new Student();
student.setName(name);
student.setAge(age);
student.setGender(gender);
studentDao.save(student);
result.put("code", 0);
} catch (Exception e) {
result.put("code", -1);
result.put("msg", e.getMessage());
}
return result;
}
@GetMapping("/getStudentList")
public Map<String, Object> getStudentList() {
Map<String, Object> result = new HashMap<>();
try {
List<Student> studentList = studentDao.findAll();
result.put("code", 0);
result.put("data", studentList);
} catch (Exception e) {
result.put("code", -1);
result.put("msg", e.getMessage());
}
return result;
}
}
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private int age;
private String gender;
// getter and setter
}
@Repository
public interface StudentDao extends JpaRepository<Student, Integer> {
}
```
数据库表结构(使用 MySQL 数据库):
```sql
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL
);
```
这只是一个简单的示例,实际项目中需要考虑更多的细节和安全性问题。
阅读全文