使用spring的JDBCTemplate连接数据库实现学生管理系统增删改查并且在Controller层输入用户名与密码效验,结尾附带测试代码与结果
时间: 2024-04-21 15:28:56 浏览: 152
好的,我会尽力回答你的问题。以下是使用Spring的JdbcTemplate连接数据库实现学生管理系统增删改查并在Controller层输入用户名与密码进行效验的示例代码:
首先,我们需要在pom.xml文件中添加相关的依赖:
```xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
```
然后,创建一个包含以下字段的学生表:
```sql
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
department VARCHAR(50) NOT NULL
);
```
接下来,创建一个Student类来表示学生实体:
```java
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String department;
// 省略getter和setter方法
}
```
然后,创建一个StudentDao类来处理学生数据的增删改查操作:
```java
@Repository
public class StudentDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Student> getAllStudents() {
String sql = "SELECT * FROM students";
return jdbcTemplate.query(sql, (resultSet, i) -> {
Student student = new Student();
student.setId(resultSet.getInt("id"));
student.setName(resultSet.getString("name"));
student.setAge(resultSet.getInt("age"));
student.setGender(resultSet.getString("gender"));
student.setDepartment(resultSet.getString("department"));
return student;
});
}
public Student getStudentById(int id) {
String sql = "SELECT * FROM students WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, (resultSet, i) -> {
Student student = new Student();
student.setId(resultSet.getInt("id"));
student.setName(resultSet.getString("name"));
student.setAge(resultSet.getInt("age"));
student.setGender(resultSet.getString("gender"));
student.setDepartment(resultSet.getString("department"));
return student;
});
}
public void addStudent(Student student) {
String sql = "INSERT INTO students (name, age, gender, department) VALUES (?, ?, ?, ?)";
jdbcTemplate.update(sql, student.getName(), student.getAge(), student.getGender(), student.getDepartment());
}
public void updateStudent(Student student) {
String sql = "UPDATE students SET name = ?, age = ?, gender = ?, department = ? WHERE id = ?";
jdbcTemplate.update(sql, student.getName(), student.getAge(), student.getGender(), student.getDepartment(), student.getId());
}
public void deleteStudent(int id) {
String sql = "DELETE FROM students WHERE id = ?";
jdbcTemplate.update(sql, id);
}
}
```
最后,我们可以在Controller层进行用户名与密码的效验,并调用StudentDao类中的方法来进行学生数据的增删改查操作:
```java
@Controller
public class StudentController {
@Autowired
private StudentDao studentDao;
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@RequestParam String username, @RequestParam String password) {
// 根据用户名和密码进行效验,这里省略具体实现
return "redirect:/students";
}
@RequestMapping("/students")
public String getAllStudents(Model model) {
List<Student> students = studentDao.getAllStudents();
model.addAttribute("students", students);
return "students";
}
@RequestMapping("/students/{id}")
public String getStudentById(@PathVariable int id, Model model) {
Student student = studentDao.getStudentById(id);
model.addAttribute("student", student);
return "student";
}
@RequestMapping(value = "/students/add", method = RequestMethod.POST)
public String addStudent(@ModelAttribute Student student) {
studentDao.addStudent(student);
return "redirect:/students";
}
@RequestMapping(value = "/students/update", method = RequestMethod.POST)
public String updateStudent(@ModelAttribute Student student) {
studentDao.updateStudent(student);
return "redirect:/students";
}
@RequestMapping("/students/delete/{id}")
public String deleteStudent(@PathVariable int id) {
studentDao.deleteStudent(id);
return "redirect:/students";
}
}
```
最后,我们可以通过JUnit测试来验证上述代码的正确性:
```java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class StudentDaoTest {
@Autowired
private StudentDao studentDao;
@Test
public void testGetAllStudents() {
List<Student> students = studentDao.getAllStudents();
assertNotNull(students);
assertEquals(3, students.size());
}
@Test
public void testGetStudentById() {
Student student = studentDao.getStudentById(1);
assertNotNull(student);
assertEquals("张三", student.getName());
}
@Test
public void testAddStudent() {
Student student = new Student();
student.setName("李四");
student.setAge(20);
student.setGender("男");
student.setDepartment("计算机科学");
studentDao.addStudent(student);
List<Student> students = studentDao.getAllStudents();
assertNotNull(students);
assertEquals(4, students.size());
}
@Test
public void testUpdateStudent() {
Student student = new Student();
student.setId(1);
student.setName("张三三");
student.setAge(21);
student.setGender("女");
student.setDepartment("软件工程");
studentDao.updateStudent(student);
Student updatedStudent = studentDao.getStudentById(1);
assertNotNull(updatedStudent);
assertEquals("张三三", updatedStudent.getName());
assertEquals(21, updatedStudent.getAge());
assertEquals("女", updatedStudent.getGender());
assertEquals("软件工程", updatedStudent.getDepartment());
}
@Test
public void testDeleteStudent() {
studentDao.deleteStudent(1);
List<Student> students = studentDao.getAllStudents();
assertNotNull(students);
assertEquals(2, students.size());
}
}
```
以上就是使用Spring的JdbcTemplate连接数据库实现学生管理系统增删改查并在Controller层输入用户名与密码进行效验的示例代码。
阅读全文