Spring Boot 使用 MyBatis Plus 查询学生信息

本篇文章介绍如何使用 Spring Boot 和 MyBatis Plus 实现学生信息查询功能。

1. 项目结构

└── com.example.demo1
    └── service
        └── impl
            └── StudentServiceImpl.java

2. 代码示例

2.1 StudentService 接口

public interface StudentService extends IService<Student> {

}

2.2 StudentServiceImpl 服务层实现

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
        implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student getById(Serializable id) {
        return studentMapper.selectById(id);
    }
}

2.3 StudentMapper 数据层接口

@Mapper
public interface StudentMapper extends BaseMapper<Student> {

}

2.4 StudentController 控制层

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/student/{id}")
    @ResponseBody
    public Student getStudentById(@PathVariable("id") Long id) {
        return studentService.getById(id);
    }
}

2.5 查询 id 为 12 的学生信息

// 查询id为12的student
Student student = studentService.getById(12L);

3. 常见错误解决方法

3.1 报错:Invalid bound statement (not found): com.example.demo1.mapper.StudentMapper.selectById

报错提示为:Invalid bound statement (not found): com.example.demo1.mapper.StudentMapper.selectById,即没有找到 com.example.demo1.mapper.StudentMapper.selectById 这个语句。

可能原因:

  1. mapper.xml 中没有定义 selectById 这个语句。

  2. selectById 语句的配置有误。

  3. Mapper 接口没有正确引入。

解决方案:

  1. 检查 mapper.xml 中是否有对应的 selectById 语句,并且语句的配置是否正确。

  2. 检查是否正确引入了对应的 mapper 接口。

4. 总结

本文介绍了使用 Spring Boot 和 MyBatis Plus 实现学生信息查询功能的完整步骤,并对常见的错误进行了分析和解决方法。希望对大家有所帮助。

Spring Boot 使用 MyBatis Plus 查询学生信息

原文地址: https://www.cveoy.top/t/topic/lB8u 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录