校园管理系统的前后端实现代码
时间: 2023-10-26 07:03:24 浏览: 62
校园管理系统的前后端实现代码比较复杂,需要涉及多种技术和语言。以下是一个基于Java语言和MySQL数据库的简单校园管理系统的前后端实现代码示例,仅供参考:
后端代码(Java + Spring Boot + MyBatis):
1. 学生实体类
```java
public class Student {
private Integer id;
private String name;
private String gender;
private Integer age;
private String major;
// 省略getters和setters
}
```
2. 学生DAO接口
```java
public interface StudentMapper {
List<Student> findAll();
Student findById(Integer id);
void add(Student student);
void update(Student student);
void delete(Integer id);
}
```
3. 学生DAO实现类
```java
@Repository
public class StudentMapperImpl implements StudentMapper {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Student> findAll() {
List<Student> students = jdbcTemplate.query("SELECT * FROM student", new BeanPropertyRowMapper<>(Student.class));
return students;
}
@Override
public Student findById(Integer id) {
Student student = jdbcTemplate.queryForObject("SELECT * FROM student WHERE id=?", new Object[]{id}, new BeanPropertyRowMapper<>(Student.class));
return student;
}
@Override
public void add(Student student) {
jdbcTemplate.update("INSERT INTO student(name, gender, age, major) VALUES(?,?,?,?)", student.getName(), student.getGender(), student.getAge(), student.getMajor());
}
@Override
public void update(Student student) {
jdbcTemplate.update("UPDATE student SET name=?, gender=?, age=?, major=? WHERE id=?", student.getName(), student.getGender(), student.getAge(), student.getMajor(), student.getId());
}
@Override
public void delete(Integer id) {
jdbcTemplate.update("DELETE FROM student WHERE id=?", id);
}
}
```
4. 学生Service接口
```java
public interface StudentService {
List<Student> findAll();
Student findById(Integer id);
void add(Student student);
void update(Student student);
void delete(Integer id);
}
```
5. 学生Service实现类
```java
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> findAll() {
return studentMapper.findAll();
}
@Override
public Student findById(Integer id) {
return studentMapper.findById(id);
}
@Override
public void add(Student student) {
studentMapper.add(student);
}
@Override
public void update(Student student) {
studentMapper.update(student);
}
@Override
public void delete(Integer id) {
studentMapper.delete(id);
}
}
```
6. 学生Controller
```java
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("")
public List<Student> list() {
return studentService.findAll();
}
@GetMapping("/{id}")
public Student findById(@PathVariable Integer id) {
return studentService.findById(id);
}
@PostMapping("")
public void add(@RequestBody Student student) {
studentService.add(student);
}
@PutMapping("/{id}")
public void update(@PathVariable Integer id, @RequestBody Student student) {
student.setId(id);
studentService.update(student);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Integer id) {
studentService.delete(id);
}
}
```
前端代码(Vue.js):
1. 学生列表页面
```html
<template>
<div>
<h1>学生列表</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>专业</th>
</tr>
</thead>
<tbody>
<tr v-for="student in students" :key="student.id">
<td>{{ student.id }}</td>
<td>{{ student.name }}</td>
<td>{{ student.gender }}</td>
<td>{{ student.age }}</td>
<td>{{ student.major }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
students: []
}
},
mounted() {
axios.get('/students').then(response => {
this.students = response.data
})
}
}
</script>
```
2. 学生详情页面
```html
<template>
<div>
<h1>学生详情</h1>
<div>ID:{{ student.id }}</div>
<div>姓名:{{ student.name }}</div>
<div>性别:{{ student.gender }}</div>
<div>年龄:{{ student.age }}</div>
<div>专业:{{ student.major }}</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
student: {}
}
},
mounted() {
const id = this.$route.params.id
axios.get(`/students/${id}`).then(response => {
this.student = response.data
})
}
}
</script>
```
3. 添加学生页面
```html
<template>
<div>
<h1>添加学生</h1>
<form @submit.prevent="add">
<div>
<label>姓名:</label>
<input type="text" v-model="student.name" required>
</div>
<div>
<label>性别:</label>
<input type="radio" v-model="student.gender" value="男" required>男
<input type="radio" v-model="student.gender" value="女" required>女
</div>
<div>
<label>年龄:</label>
<input type="number" v-model="student.age" required>
</div>
<div>
<label>专业:</label>
<input type="text" v-model="student.major" required>
</div>
<button type="submit">添加</button>
</form>
</div>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
student: {
name: '',
gender: '',
age: '',
major: ''
}
}
},
methods: {
add() {
axios.post('/students', this.student).then(() => {
this.$router.push('/')
})
}
}
}
</script>
```
4. 修改学生页面
```html
<template>
<div>
<h1>修改学生</h1>
<form @submit.prevent="update">
<div>
<label>姓名:</label>
<input type="text" v-model="student.name" required>
</div>
<div>
<label>性别:</label>
<input type="radio" v-model="student.gender" value="男" required>男
<input type="radio" v-model="student.gender" value="女" required>女
</div>
<div>
<label>年龄:</label>
<input type="number" v-model="student.age" required>
</div>
<div>
<label>专业:</label>
<input type="text" v-model="student.major" required>
</div>
<button type="submit">保存</button>
</form>
</div>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
student: {
name: '',
gender: '',
age: '',
major: ''
}
}
},
mounted() {
const id = this.$route.params.id
axios.get(`/students/${id}`).then(response => {
this.student = response.data
})
},
methods: {
update() {
const id = this.$route.params.id
axios.put(`/students/${id}`, this.student).then(() => {
this.$router.push('/')
})
}
}
}
</script>
```
以上是一个简单的校园管理系统的前后端实现代码示例,具体实现还需要根据实际情况进行调整。
阅读全文