Spring Boot Mybatis Plus 查询数据报错: Invalid bound statement (not found)
Spring Boot Mybatis Plus 查询数据报错: Invalid bound statement (not found)
根据你提供的代码,你使用了注解的方式进行配置,并且已经添加了 @Service 和 @Mapper 注解,应该不会出现这个问题。
可能的原因:
-
mapper.xml 文件配置错误: 虽然你使用的是注解方式,但 Mybatis Plus 仍然会根据注解生成对应的 SQL 语句,并将其保存到
mapper.xml文件中。如果mapper.xml文件配置错误,就会导致无法找到对应的语句。 -
依赖冲突: 确保你使用的 Mybatis Plus 版本与 Spring Boot 版本兼容,并且没有其他依赖冲突。
-
缓存问题: 尝试清空项目缓存,重新构建项目。
解决方法:
-
检查
mapper.xml文件: 仔细检查mapper.xml文件,确保其中的 SQL 语句定义正确,并且与注解中定义的对应。 -
检查依赖版本: 确保你使用的 Mybatis Plus 版本与 Spring Boot 版本兼容。
-
清空缓存,重新构建项目: 清空项目缓存,并重新构建项目。
-
检查依赖是否正确引入: 确保相关依赖,例如 Mybatis Plus、Mybatis、Spring Data 等,都正确引入到项目中。
如果以上步骤都没有解决问题,请提供以下信息,以便更好地分析和解决问题:
- 完整的报错信息
- 项目的依赖版本
mapper.xml文件的代码Student实体类的代码StudentMapper接口的代码StudentServiceImpl实现类的代码
示例代码:
// Controller
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/student/{id}")
@ResponseBody
public Student getStudentById(@PathVariable("id") Long id) {
return studentService.getById(id);
}
}
// Service
@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
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
}
// 查询id为12的student
Student student = studentService.getById(12L);
代码补充:
@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> {
}
注意:
- 确保
mapper.xml文件已正确配置,并与注解定义对应。 - 检查依赖版本是否兼容。
- 尝试清空缓存并重新构建项目。
- 如果问题仍然存在,请提供更多信息以便更好地分析和解决问题。
原文地址: https://www.cveoy.top/t/topic/lB8p 著作权归作者所有。请勿转载和采集!