使用jdbctemplate +sprinfMvc实现学生表student的增删改查
时间: 2024-02-03 17:01:59 浏览: 75
学生表的增删改查
使用JdbcTemplate结合Spring MVC可以更加方便地实现学生表student的增删改查。以下是一个简单的示例,演示如何使用JdbcTemplate和Spring MVC实现学生表的增删改查。
首先,你需要在你的项目中引入Spring JDBC和Spring Web MVC依赖。假设你使用Maven管理你的项目,你可以在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
接下来,你需要配置数据库连接。在Spring Boot项目中,你可以在`application.properties`或`application.yml`文件中添加以下配置:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/db_name
spring.datasource.username=db_username
spring.datasource.password=db_password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
然后,你可以创建一个`Student`类来表示学生实体:
```java
public class Student {
private int id;
private String name;
private int age;
// 省略构造方法、getter和setter方法
}
```
接下来,你可以创建一个`StudentController`类来处理学生表的增删改查请求:
```java
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("/list")
public String getAllStudents(Model model) {
String sql = "SELECT * FROM student";
List<Student> students = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
model.addAttribute("students", students);
return "studentList";
}
@GetMapping("/{id}")
public String getStudentById(@PathVariable int id, Model model) {
String sql = "SELECT * FROM student WHERE id=?";
Student student = jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Student.class));
model.addAttribute("student", student);
return "studentDetail";
}
@GetMapping("/add")
public String showAddForm(Model model) {
Student student = new Student();
model.addAttribute("student", student);
return "addStudentForm";
}
@PostMapping("/add")
public String addStudent(@ModelAttribute("student") Student student) {
String sql = "INSERT INTO student(name, age) VALUES (?, ?)";
jdbcTemplate.update(sql, student.getName(), student.getAge());
return "redirect:/student/list";
}
@GetMapping("/{id}/edit")
public String showEditForm(@PathVariable int id, Model model) {
String sql = "SELECT * FROM student WHERE id=?";
Student student = jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Student.class));
model.addAttribute("student", student);
return "editStudentForm";
}
@PostMapping("/{id}/edit")
public String updateStudent(@PathVariable int id, @ModelAttribute("student") Student student) {
String sql = "UPDATE student SET name=?, age=? WHERE id=?";
jdbcTemplate.update(sql, student.getName(), student.getAge(), id);
return "redirect:/student/list";
}
@GetMapping("/{id}/delete")
public String deleteStudent(@PathVariable int id) {
String sql = "DELETE FROM student WHERE id=?";
jdbcTemplate.update(sql, id);
return "redirect:/student/list";
}
}
```
在上述代码中,`StudentController`类使用JdbcTemplate来执行SQL语句,处理学生表的增删改查请求,并通过Spring MVC的注解来定义不同的请求处理方法。
最后,你可以创建相应的视图文件(例如`studentList.jsp`、`studentDetail.jsp`、`addStudentForm.jsp`等),用于展示学生列表、学生详情和表单页面。
以上是一个简单的示例,你可以根据你的实际需求进行适当的修改和扩展。希望对你有所帮助!
阅读全文