用springboot+mybatis写一段代码:具体内容如下 1. 实现教师登录功能,登录成功显示学生列表页面,失败提示账号密码错误。(10不,分) 2. 完成学生表的查询功能,并实现学生分页查询功能。(10分) 3. 实现学生表修改编辑功能,要求点击修改默认显示学生信息,并且完成学号,姓名班级在提交前做非空校验。(10分) 4. 实现学生表修改保存功能,要求学号不可更改。(10分) 5. 利用Redis实现学生表查询缓存功能。(10分)
时间: 2024-03-22 11:37:41 浏览: 96
好的,以下是我的代码实现:
1. 实现教师登录功能:
Controller:
```java
@RestController
@RequestMapping("/teacher")
public class TeacherController {
@Autowired
private TeacherService teacherService;
@PostMapping("/login")
public Result login(@RequestBody Teacher teacher, HttpServletRequest request) {
boolean success = teacherService.login(teacher.getUsername(), teacher.getPassword());
if (success) {
HttpSession session = request.getSession();
session.setAttribute("username", teacher.getUsername());
return Result.success("登录成功");
} else {
return Result.error("账号密码错误");
}
}
@GetMapping("/studentList")
public Result studentList(HttpServletRequest request) {
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
if (username != null) {
List<Student> studentList = studentService.getAllStudents();
return Result.success(studentList);
} else {
return Result.error("请先登录");
}
}
}
```
Service:
```java
@Service
public class TeacherService {
@Autowired
private TeacherMapper teacherMapper;
public boolean login(String username, String password) {
Teacher teacher = teacherMapper.findTeacherByUsername(username);
if (teacher != null && teacher.getPassword().equals(password)) {
return true;
} else {
return false;
}
}
}
```
Mapper:
```java
@Mapper
public interface TeacherMapper {
@Select("SELECT * FROM teacher WHERE username=#{username}")
public Teacher findTeacherByUsername(String username);
}
```
2. 完成学生表的查询功能,并实现学生分页查询功能:
Controller:
```java
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/list")
public Result studentList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Student> studentList = studentService.getAllStudents();
PageInfo<Student> pageInfo = new PageInfo<>(studentList);
return Result.success(pageInfo);
}
@GetMapping("/search")
public Result searchStudents(@RequestParam String keyword, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Student> studentList = studentService.searchStudents(keyword);
PageInfo<Student> pageInfo = new PageInfo<>(studentList);
return Result.success(pageInfo);
}
}
```
Service:
```java
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public List<Student> getAllStudents() {
return studentMapper.getAllStudents();
}
public List<Student> searchStudents(String keyword) {
return studentMapper.searchStudents(keyword);
}
}
```
Mapper:
```java
@Mapper
public interface StudentMapper {
@Select("SELECT * FROM student")
public List<Student> getAllStudents();
@Select("SELECT * FROM student WHERE student_id=#{keyword} OR name LIKE CONCAT('%',#{keyword},'%') OR class_name LIKE CONCAT('%',#{keyword},'%')")
public List<Student> searchStudents(String keyword);
}
```
3. 实现学生表修改编辑功能:
Controller:
```java
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/get")
public Result getStudentById(@RequestParam int id) {
Student student = studentService.getStudentById(id);
return Result.success(student);
}
@PostMapping("/update")
public Result updateStudent(@RequestBody Student student) {
if (StringUtils.isEmpty(student.getStudentId()) || StringUtils.isEmpty(student.getName()) || StringUtils.isEmpty(student.getClassName())) {
return Result.error("学号、姓名和班级不能为空");
}
studentService.updateStudent(student);
return Result.success("修改成功");
}
}
```
Service:
```java
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public Student getStudentById(int id) {
return studentMapper.getStudentById(id);
}
public void updateStudent(Student student) {
studentMapper.updateStudent(student);
}
}
```
Mapper:
```java
@Mapper
public interface StudentMapper {
@Select("SELECT * FROM student WHERE id=#{id}")
public Student getStudentById(int id);
@Update("UPDATE student SET name=#{name}, class_name=#{className} WHERE id=#{id}")
public void updateStudent(Student student);
}
```
4. 实现学生表修改保存功能:
Controller:
```java
@RestController
@RequestMapping("/student")
public class StudentController {
@PostMapping("/update")
public Result updateStudent(@RequestBody Student student) {
if (StringUtils.isEmpty(student.getName()) || StringUtils.isEmpty(student.getClassName())) {
return Result.error("姓名和班级不能为空");
}
studentService.updateStudent(student);
return Result.success("修改成功");
}
}
```
Service:
```java
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public void updateStudent(Student student) {
studentMapper.updateStudent(student);
}
}
```
Mapper:
```java
@Mapper
public interface StudentMapper {
@Update("UPDATE student SET name=#{name}, class_name=#{className} WHERE id=#{id}")
public void updateStudent(Student student);
}
```
5. 利用Redis实现学生表查询缓存功能:
Controller:
```java
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/list")
public Result studentList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
String cacheKey = "student_list_" + pageNum + "_" + pageSize;
List<Student> studentList = (List<Student>) RedisUtils.get(cacheKey);
if (studentList == null) {
PageHelper.startPage(pageNum, pageSize);
studentList = studentService.getAllStudents();
PageInfo<Student> pageInfo = new PageInfo<>(studentList);
RedisUtils.set(cacheKey, studentList, 60);
}
return Result.success(studentList);
}
}
```
Service:
```java
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public List<Student> getAllStudents() {
return studentMapper.getAllStudents();
}
}
```
以上是我的代码实现,可能还有一些细节需要根据具体情况进行调整。其中 Redis 缓存的实现需要使用第三方库,如 jedis 或者 lettuce。
阅读全文