MyBatis Plus 错误:Invalid bound statement (not found): com.example.demo1.mapper.StudentMapper.selectById 解决方法
MyBatis Plus 错误: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' 这个语句。
可能原因
- mapper.xml 中没有定义 'selectById' 这个语句。
- mapper.xml 中定义了 'selectById',但是没有正确引入。
- 在 mapper.xml 中定义了 'selectById',但是它的 namespace 和 mapper 接口的包名不一致。
解决方法
- 确认 mapper.xml 中是否定义了 'selectById' 这个语句。
- 确认 mapper.xml 中是否正确引入了对应的 mapper 接口。
- 确认 mapper.xml 中定义的 'selectById' 的 namespace 是否和 mapper 接口的包名一致。
代码示例
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/student/{id}")
@ResponseBody
public Student getStudentById(@PathVariable("id") Long id) {
return studentService.getById(id);
}
}
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public Student getById(Serializable id) {
return studentMapper.selectById(id);
}
}
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
}
// 查询id为12的student
Student student = studentService.getById(12L);
错误分析
这个错误通常是因为 MyBatis 在运行时找不到对应的 SQL 语句。具体原因可能是:
- 你没有在
mapper.xml文件中定义selectById方法对应的 SQL 语句。 mapper.xml文件没有被正确引入到你的项目中。mapper.xml文件中的namespace属性与StudentMapper接口的包名不匹配。
解决步骤
- 确认
mapper.xml文件中是否定义了selectById方法对应的 SQL 语句。 - 确认
mapper.xml文件是否被正确引入到你的项目中。 - 确认
mapper.xml文件中的namespace属性与StudentMapper接口的包名是否匹配。
其他提示
- 确保你使用了正确的 MyBatis Plus 版本。
- 确保你正确配置了 MyBatis Plus 的配置文件。
- 如果你使用了 Spring Boot,确保你正确配置了 Spring Boot 的自动配置。
原文地址: https://www.cveoy.top/t/topic/lB8m 著作权归作者所有。请勿转载和采集!