在一个学生实体类中有字段姓名,学号,性别,手机号,使用spring boot+mybatis+thymeleaf根据学号更新手机号
时间: 2023-12-26 15:03:54 浏览: 67
好的,您可以参考以下代码:
首先,在学生实体类中添加手机号字段:
```java
public class Student {
private String name;
private String studentId;
private String gender;
private String phoneNumber;
// 省略getter和setter方法
}
```
然后,在Mybatis中添加对应的Mapper接口:
```java
public interface StudentMapper {
int updatePhoneNumberByStudentId(@Param("studentId") String studentId, @Param("phoneNumber") String phoneNumber);
}
```
接下来,实现Mapper接口:
```java
@Service
public class StudentService {
private final StudentMapper studentMapper;
@Autowired
public StudentService(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
public int updatePhoneNumberByStudentId(String studentId, String phoneNumber) {
return studentMapper.updatePhoneNumberByStudentId(studentId, phoneNumber);
}
}
```
最后,在Controller中调用Service方法,实现更新手机号:
```java
@Controller
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@PostMapping("/updatePhoneNumber")
public String updatePhoneNumber(String studentId, String phoneNumber) {
studentService.updatePhoneNumberByStudentId(studentId, phoneNumber);
return "success";
}
}
```
在Thymeleaf中,您可以通过form表单提交学号和手机号到/updatePhoneNumber接口,例如:
```html
<form action="/updatePhoneNumber" method="post">
<input type="text" name="studentId" placeholder="学号">
<input type="text" name="phoneNumber" placeholder="手机号">
<button type="submit">提交</button>
</form>
```
这样就完成了根据学号更新手机号的功能。
阅读全文