Spring Boot MyBatis Plus 学生信息管理:查询学生信息
Spring Boot MyBatis Plus 学生信息管理:查询学生信息
本文将演示使用 Spring Boot 和 MyBatis Plus 实现学生信息管理系统,并展示如何查询特定学生的详细信息。
代码示例
1. 服务接口
public interface StudentService extends IService<Student> {
}
2. 服务实现类
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public Student getById(Serializable id) {
return studentMapper.selectById(id);
}
}
3. Mapper 接口
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
}
4. 控制器
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/student/{id}")
@ResponseBody
public Student getStudentById(@PathVariable("id") Long id) {
return studentService.getById(id);
}
}
5. 查询操作
// 查询id为12的student
Student student = studentService.getById(12L);
代码说明
- StudentService 接口继承了 MyBatis Plus 的
IService接口,用于定义学生信息操作相关的接口方法。 - StudentServiceImpl 类实现了
StudentService接口,并使用@Autowired注解注入StudentMapper对象,通过调用studentMapper.selectById(id)方法实现查询操作。 - StudentMapper 接口继承了 MyBatis Plus 的
BaseMapper接口,提供了基本的 CRUD 操作,无需额外编写 SQL 语句。 - StudentController 类使用
@GetMapping注解定义一个 GET 请求,接收id参数,并通过studentService.getById(id)获取对应学生的详细信息。 - 在代码中,我们通过
studentService.getById(12L)方法获取 ID 为 12 的学生信息。
总结
本文演示了如何使用 Spring Boot 和 MyBatis Plus 实现简单的学生信息管理系统,并展示了如何查询特定学生的详细信息。MyBatis Plus 提供了强大的功能,简化了开发过程,使开发人员可以专注于业务逻辑。
原文地址: https://www.cveoy.top/t/topic/lB8l 著作权归作者所有。请勿转载和采集!