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' 这个语句。

可能原因

  1. mapper.xml 中没有定义 'selectById' 这个语句。
  2. mapper.xml 中定义了 'selectById',但是没有正确引入。
  3. 在 mapper.xml 中定义了 'selectById',但是它的 namespace 和 mapper 接口的包名不一致。

解决方法

  1. 确认 mapper.xml 中是否定义了 'selectById' 这个语句。
  2. 确认 mapper.xml 中是否正确引入了对应的 mapper 接口。
  3. 确认 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 接口的包名不匹配。

解决步骤

  1. 确认 mapper.xml 文件中是否定义了 selectById 方法对应的 SQL 语句。
  2. 确认 mapper.xml 文件是否被正确引入到你的项目中。
  3. 确认 mapper.xml 文件中的 namespace 属性与 StudentMapper 接口的包名是否匹配。

其他提示

  • 确保你使用了正确的 MyBatis Plus 版本。
  • 确保你正确配置了 MyBatis Plus 的配置文件。
  • 如果你使用了 Spring Boot,确保你正确配置了 Spring Boot 的自动配置。
MyBatis Plus 错误:Invalid bound statement (not found): com.example.demo1.mapper.StudentMapper.selectById 解决方法

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

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